xref: /AOO41X/main/xml2cmp/source/finder/dependy.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include "dependy.hxx"
30*cdf0e10cSrcweir #include <iostream>
31*cdf0e10cSrcweir #include "../support/syshelp.hxx"
32*cdf0e10cSrcweir #include "../support/list.hxx"
33*cdf0e10cSrcweir #include "../xcd/xmltree.hxx"
34*cdf0e10cSrcweir #include "../xcd/parse.hxx"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir Simstr 				ShortName(const Simstr & i_rService);
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir Service::Service( const char * i_sName )
43*cdf0e10cSrcweir 	:	sName(i_sName)
44*cdf0e10cSrcweir 		// aImplementations
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir }
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir ServiceInfo &
49*cdf0e10cSrcweir Service::AddImplementation( const char * i_sLibrary )
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir 	ServiceInfo * ret = new ServiceInfo(i_sLibrary);
52*cdf0e10cSrcweir 	aImplementations.push_back(ret);
53*cdf0e10cSrcweir 	return *ret;
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir ServiceInfo::ServiceInfo( const char * i_sLibrary )
57*cdf0e10cSrcweir 	:	sImplementingLibrary(i_sLibrary)
58*cdf0e10cSrcweir 		// aNeededServices
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir }
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir void
63*cdf0e10cSrcweir ServiceInfo::AddDependency( const char * i_sNeededService )
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir 	aNeededServices.push_back(i_sNeededService);
66*cdf0e10cSrcweir }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir DependencyFinder::DependencyFinder()
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir DependencyFinder::~DependencyFinder()
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir void
77*cdf0e10cSrcweir DependencyFinder::GatherData( const char * i_sSearchDirectory )
78*cdf0e10cSrcweir {
79*cdf0e10cSrcweir 	List<Simstr> aFiles;
80*cdf0e10cSrcweir 	GatherFileNames( aFiles, i_sSearchDirectory );
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 	for ( unsigned i = 0; i < aFiles.size(); ++i )
83*cdf0e10cSrcweir 	{
84*cdf0e10cSrcweir 		ReadFile( aFiles[i].str() );
85*cdf0e10cSrcweir 	}
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir void
89*cdf0e10cSrcweir DependencyFinder::FindNeededServices( StringVector &		o_rLibraries,
90*cdf0e10cSrcweir 									  StringVector &		o_rServices,
91*cdf0e10cSrcweir 									  const Simstr &		i_rService )
92*cdf0e10cSrcweir {
93*cdf0e10cSrcweir 	Map_Services::const_iterator itService = aServices.find(i_rService);
94*cdf0e10cSrcweir 	if ( itService == aServices.end() )
95*cdf0e10cSrcweir 	{
96*cdf0e10cSrcweir 		std::cerr << "Error: Service \""
97*cdf0e10cSrcweir 				  << i_rService.str()
98*cdf0e10cSrcweir 				  << "\" not found."
99*cdf0e10cSrcweir 				  << std::endl;
100*cdf0e10cSrcweir 		return ;
101*cdf0e10cSrcweir 	}
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 	aResult_Libraries.erase( aResult_Libraries.begin(), aResult_Libraries.end() );
104*cdf0e10cSrcweir 	aResult_Services.erase( aResult_Services.begin(), aResult_Services.end() );
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir //	const ServiceInfo & rSInfo = (*itService).second->FirstImplementation();
107*cdf0e10cSrcweir 	Add2Result( *(*itService).second );
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 	for ( std::set< Simstr >::const_iterator il = aResult_Libraries.begin();
110*cdf0e10cSrcweir 		  il != aResult_Libraries.end();
111*cdf0e10cSrcweir 		  ++il )
112*cdf0e10cSrcweir 	{
113*cdf0e10cSrcweir 		o_rLibraries.push_back(*il);
114*cdf0e10cSrcweir 	}
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 	for ( std::set< Simstr >::const_iterator is = aResult_Services.begin();
117*cdf0e10cSrcweir 		  is != aResult_Services.end();
118*cdf0e10cSrcweir 		  ++is )
119*cdf0e10cSrcweir 	{
120*cdf0e10cSrcweir 		o_rServices.push_back(*is);
121*cdf0e10cSrcweir 	}
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir void
125*cdf0e10cSrcweir DependencyFinder::ReadFile(  const char * i_sFilename )
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir 	ModuleDescription	aModule;
128*cdf0e10cSrcweir 	X2CParser			aParser(aModule);
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 	if ( !aParser.Parse(i_sFilename) )
131*cdf0e10cSrcweir 	{
132*cdf0e10cSrcweir 		std::cerr << "Error: File \""
133*cdf0e10cSrcweir 			 << i_sFilename
134*cdf0e10cSrcweir 			 << "\" could not be parsed."
135*cdf0e10cSrcweir 			 << std::endl;
136*cdf0e10cSrcweir 		return;
137*cdf0e10cSrcweir 	}
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 	// GetResults:
140*cdf0e10cSrcweir 	Simstr sModule = aModule.ModuleName();
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 	List < const MultipleTextElement* > aImplServices;
143*cdf0e10cSrcweir 	List < const MultipleTextElement* > aNeededServices;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 	aModule.Get_SupportedServices(aImplServices);
146*cdf0e10cSrcweir 	aModule.Get_ServiceDependencies(aNeededServices);
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 	unsigned nImplServicesSize = aImplServices.size();
149*cdf0e10cSrcweir 	unsigned nNeededServicesSize = aNeededServices.size();
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 	for ( unsigned i = 0; i < nImplServicesSize; ++i )
152*cdf0e10cSrcweir 	{
153*cdf0e10cSrcweir 		const MultipleTextElement & rImpl = *aImplServices[i];
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 		unsigned nImplDataSize = rImpl.Size();
156*cdf0e10cSrcweir 		for ( unsigned di = 0; di < nImplDataSize; ++di )
157*cdf0e10cSrcweir 		{
158*cdf0e10cSrcweir 			Simstr sService = ShortName(rImpl.Data(di));
159*cdf0e10cSrcweir 			Service * pService = aServices[sService];
160*cdf0e10cSrcweir 			if (pService == 0)
161*cdf0e10cSrcweir 			{
162*cdf0e10cSrcweir 				pService = new Service(rImpl.Data(di));
163*cdf0e10cSrcweir                 aServices[sService] = pService;
164*cdf0e10cSrcweir 			}
165*cdf0e10cSrcweir 			ServiceInfo & rSInfo = pService->AddImplementation(sModule);
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 			for ( unsigned n = 0; n < nNeededServicesSize; ++n )
168*cdf0e10cSrcweir 			{
169*cdf0e10cSrcweir 				unsigned nNeededDataSize = aNeededServices[n]->Size();
170*cdf0e10cSrcweir 				for ( unsigned dn = 0; dn < nNeededDataSize; ++dn )
171*cdf0e10cSrcweir 				{
172*cdf0e10cSrcweir 					if (! aNeededServices[n]->Data(dn).is_no_text())
173*cdf0e10cSrcweir 						rSInfo.AddDependency( ShortName(aNeededServices[n]->Data(dn)) );
174*cdf0e10cSrcweir 				}	// end for dn
175*cdf0e10cSrcweir 			}	// end for n
176*cdf0e10cSrcweir 		}	//	end for di
177*cdf0e10cSrcweir 	}	// end for i
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir void
181*cdf0e10cSrcweir DependencyFinder::Add2Result( const Service & i_rService )
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir 	const ServiceInfo & rSInfo = i_rService.FirstImplementation();
184*cdf0e10cSrcweir 	aResult_Libraries.insert(rSInfo.Library());
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 	const ServiceInfo::List_NeededServices & rNeededs
187*cdf0e10cSrcweir 			= rSInfo.NeededServices();
188*cdf0e10cSrcweir 	for ( StringVector::const_iterator it = rNeededs.begin();
189*cdf0e10cSrcweir 		  it != rNeededs.end();
190*cdf0e10cSrcweir 		  ++it )
191*cdf0e10cSrcweir 	{
192*cdf0e10cSrcweir 		std::pair< std::set< Simstr >::iterator, bool > aInsertResult
193*cdf0e10cSrcweir 				= aResult_Services.insert(*it);
194*cdf0e10cSrcweir 		if (aInsertResult.second)
195*cdf0e10cSrcweir 		{	// Needed service not yet known
196*cdf0e10cSrcweir 			Map_Services::const_iterator itFound = aServices.find(*it);
197*cdf0e10cSrcweir 			if ( itFound == aServices.end() )
198*cdf0e10cSrcweir 			{
199*cdf0e10cSrcweir 				std::cerr << "Needed service \""
200*cdf0e10cSrcweir 						  << (*it).str()
201*cdf0e10cSrcweir 						  << "\" not found,"
202*cdf0e10cSrcweir 						  << std::endl;
203*cdf0e10cSrcweir 			}
204*cdf0e10cSrcweir 			else
205*cdf0e10cSrcweir 			{
206*cdf0e10cSrcweir 				Add2Result( *(*itFound).second );
207*cdf0e10cSrcweir 			}
208*cdf0e10cSrcweir 		} 	// endif (! aInsertResult.second)
209*cdf0e10cSrcweir 	} 	// end for (it)
210*cdf0e10cSrcweir }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir Simstr
215*cdf0e10cSrcweir ShortName(const Simstr & i_rService)
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir 	const char * pStart = i_rService.str();
218*cdf0e10cSrcweir 	const char * pEnd = strchr(pStart,' ');
219*cdf0e10cSrcweir 	if (pEnd != 0)
220*cdf0e10cSrcweir 		return Simstr(pStart, 0, int(pEnd-pStart));
221*cdf0e10cSrcweir 	else
222*cdf0e10cSrcweir 		return i_rService;
223*cdf0e10cSrcweir }
224*cdf0e10cSrcweir 
225