001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019: package de.schlund.pfixcore.util;
020:
021: import java.io.BufferedReader;
022: import java.io.File;
023: import java.io.FileReader;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.TreeSet;
027:
028: import org.apache.log4j.xml.DOMConfigurator;
029: import org.w3c.dom.Document;
030: import org.w3c.dom.Element;
031: import org.w3c.dom.Node;
032: import org.w3c.dom.NodeList;
033:
034: import de.schlund.pfixxml.config.GlobalConfigurator;
035: import de.schlund.pfixxml.resources.DocrootResource;
036: import de.schlund.pfixxml.resources.ResourceUtil;
037: import de.schlund.pfixxml.targets.AuxDependency;
038: import de.schlund.pfixxml.targets.AuxDependencyFactory;
039: import de.schlund.pfixxml.targets.AuxDependencyFile;
040: import de.schlund.pfixxml.targets.AuxDependencyImage;
041: import de.schlund.pfixxml.targets.AuxDependencyInclude;
042: import de.schlund.pfixxml.targets.DependencyType;
043: import de.schlund.pfixxml.targets.TargetDependencyRelation;
044: import de.schlund.pfixxml.targets.TargetGenerator;
045: import de.schlund.pfixxml.util.Xml;
046:
047: /**
048: * CheckIncludes.java
049: *
050: *
051: * Created: Tue Apr 8 15:42:11 2003
052: *
053: * @author <a href="mailto:jtl@schlund.de">Jens Lautenbacher</a>
054: * @version $Id: CheckIncludes.java 3368 2008-02-21 14:08:40Z smarsching $
055: */
056:
057: public class CheckIncludes {
058: // private static final String XPATH = "/include_parts/part/theme";
059:
060: private HashMap<String, TargetGenerator> generators = new HashMap<String, TargetGenerator>();
061: private TreeSet<DocrootResource> includefilenames = new TreeSet<DocrootResource>();
062: private TreeSet<DocrootResource> imagefilenames = new TreeSet<DocrootResource>();
063: private TreeSet<AuxDependency> unavail = new TreeSet<AuxDependency>();
064: private TreeSet<AuxDependency> includes;
065: private String pwd;
066: private String outfile;
067:
068: static {
069: // dbfac.setNamespaceAware(true);
070: }
071:
072: public CheckIncludes(String pwd, String outfile, File allprj,
073: File allincs, File allimgs) throws Exception {
074: this .pwd = pwd;
075: this .outfile = outfile;
076: String line;
077: BufferedReader input;
078:
079: input = new BufferedReader(new FileReader(allincs));
080: while ((line = input.readLine()) != null) {
081: // line = new File(pwd + line).getCanonicalPath();
082: includefilenames.add(ResourceUtil
083: .getFileResourceFromDocroot(line));
084: }
085: input.close();
086:
087: input = new BufferedReader(new FileReader(allimgs));
088: while ((line = input.readLine()) != null) {
089: // line = new File(pwd + line).getCanonicalPath();
090: imagefilenames.add(ResourceUtil
091: .getFileResourceFromDocroot(line));
092: }
093: input.close();
094:
095: input = new BufferedReader(new FileReader(allprj));
096: while ((line = input.readLine()) != null) {
097: // line = pwd + line;
098: TargetGenerator gen = new TargetGenerator(ResourceUtil
099: .getFileResourceFromDocroot(line));
100: generators.put(pwd + line, gen);
101: }
102: input.close();
103:
104: includes = AuxDependencyFactory.getInstance()
105: .getAllAuxDependencies();
106: for (Iterator<AuxDependency> i = includes.iterator(); i
107: .hasNext();) {
108: AuxDependency aux = i.next();
109: unavail.add(aux);
110: }
111: }
112:
113: public void doCheck() throws Exception {
114: Document doc = Xml.createDocument();
115:
116: Element check_root = doc.createElement("checkresult");
117: check_root.setAttribute("xmlns:ixsl",
118: "http://www.w3.org/1999/XSL/Transform");
119: check_root.setAttribute("xmlns:pfx",
120: "http://www.schlund.de/pustefix/core");
121: check_root.setAttribute("xmlns:hlp",
122: "http://www.schlund.de/pustefix/onlinehelp");
123: Element files_root = doc.createElement("includefiles");
124: Element images_root = doc.createElement("images");
125: Element prj_root = doc.createElement("projects");
126: doc.appendChild(check_root);
127: check_root.appendChild(files_root);
128: check_root.appendChild(images_root);
129: check_root.appendChild(prj_root);
130:
131: checkForUnusedIncludes(doc, files_root);
132: checkForUnusedImages(doc, images_root);
133:
134: checkForUnavailableIncludes(doc, prj_root);
135:
136: Xml.serialize(doc, outfile, true, true);
137: }
138:
139: public static void main(String[] args) throws Exception {
140: String output = args[0];
141: String allprjarg = args[1];
142: String allincarg = args[2];
143: String allimgarg = args[3];
144:
145: String dir = new File(".").getCanonicalPath() + "/";
146:
147: DOMConfigurator
148: .configure(dir + "core/conf/generator_quiet.xml");
149: GlobalConfigurator.setDocroot(dir);
150:
151: CheckIncludes instance = new CheckIncludes(dir, output,
152: new File(allprjarg), new File(allincarg), new File(
153: allimgarg));
154: instance.doCheck();
155:
156: }
157:
158: private void checkForUnavailableIncludes(Document result,
159: Element res_root) throws Exception {
160: for (Iterator<String> i = generators.keySet().iterator(); i
161: .hasNext();) {
162: String depend = i.next();
163: TargetGenerator gen = generators.get(depend);
164: TreeSet<AuxDependency> deps = TargetDependencyRelation
165: .getInstance().getProjectDependencies(gen);
166: if (deps == null) {
167: return;
168: }
169:
170: for (Iterator<AuxDependency> j = deps.iterator(); j
171: .hasNext();) {
172: AuxDependency aux = j.next();
173: if (!unavail.contains(aux)
174: || aux.getType().equals(DependencyType.FILE)
175: || aux.getType().equals(DependencyType.TARGET)) {
176: j.remove();
177: }
178: }
179:
180: if (!deps.isEmpty()) {
181: Element prj_elem = result.createElement("project");
182: String name = depend.substring(pwd.length());
183: prj_elem.setAttribute("name", name);
184: res_root.appendChild(prj_elem);
185:
186: for (Iterator<AuxDependency> j = deps.iterator(); j
187: .hasNext();) {
188: AuxDependency aux = j.next();
189: Element elem = result.createElement("MISSING");
190: prj_elem.appendChild(elem);
191: elem.setAttribute("type", aux.getType().toString());
192: String path = ((AuxDependencyFile) aux).getPath()
193: .getRelativePath();
194: if (aux.getType().equals(DependencyType.TEXT)) {
195: AuxDependencyInclude a = (AuxDependencyInclude) aux;
196: elem.setAttribute("path", path);
197: elem.setAttribute("part", a.getPart());
198: elem.setAttribute("theme", a.getTheme());
199: } else if (aux.getType().equals(
200: DependencyType.IMAGE)) {
201: AuxDependencyImage a = (AuxDependencyImage) aux;
202: elem.setAttribute("path", a.getPath()
203: .getRelativePath());
204: }
205: }
206: }
207: }
208: }
209:
210: private void checkForUnusedImages(Document result, Element res_root)
211: throws Exception {
212: for (Iterator<DocrootResource> i = imagefilenames.iterator(); i
213: .hasNext();) {
214: DocrootResource img = i.next();
215:
216: Element res_image = result.createElement("image");
217: res_image.setAttribute("name", img.getRelativePath());
218:
219: res_root.appendChild(res_image);
220:
221: AuxDependency aux = AuxDependencyFactory.getInstance()
222: .getAuxDependencyImage(img);
223: if (!includes.contains(aux)) {
224: res_image.setAttribute("UNUSED", "true");
225: continue;
226: } else {
227: unavail.remove(aux);
228: }
229: }
230: }
231:
232: private void checkForUnusedIncludes(Document result,
233: Element res_root) throws Exception {
234: for (Iterator<DocrootResource> i = includefilenames.iterator(); i
235: .hasNext();) {
236: DocrootResource path = i.next();
237: Document doc;
238:
239: Element res_incfile = result.createElement("incfile");
240: res_root.appendChild(res_incfile);
241: res_incfile.setAttribute("name", path.getRelativePath());
242:
243: try {
244: doc = Xml.parseMutable(path);
245: } catch (Exception e) {
246: Element error = result.createElement("ERROR");
247: res_incfile.appendChild(error);
248: error.setAttribute("cause", e.getMessage());
249: continue;
250: }
251:
252: Element root = doc.getDocumentElement();
253: if (!root.getNodeName().equals("include_parts")) {
254: Element error = result.createElement("ERROR");
255: res_incfile.appendChild(error);
256: error.setAttribute("cause",
257: "not an include file (root != include_parts)");
258: continue;
259: }
260:
261: NodeList nl = root.getChildNodes();
262: for (int j = 0; j < nl.getLength(); j++) {
263: if (nl.item(j) instanceof Element) {
264: Element partelem = (Element) nl.item(j);
265:
266: if (!partelem.getNodeName().equals("part")) {
267: Element error = result.createElement("ERROR");
268: res_incfile.appendChild(error);
269: error.setAttribute("cause",
270: "invalid node in include file (child of root != part): "
271: + path.toURI().getPath()
272: .substring(1) + "/"
273: + partelem.getNodeName());
274: continue;
275: }
276:
277: Element res_part = result.createElement("part");
278: res_incfile.appendChild(res_part);
279: res_part.setAttribute("name", partelem
280: .getAttribute("name"));
281:
282: NodeList prodchilds = partelem.getChildNodes();
283: for (int k = 0; k < prodchilds.getLength(); k++) {
284: if (prodchilds.item(k) instanceof Element) {
285: Element themeelem = (Element) prodchilds
286: .item(k);
287: if (!themeelem.getNodeName()
288: .equals("theme")) {
289: Element error = result
290: .createElement("ERROR");
291: res_part.appendChild(error);
292: error.setAttribute("cause",
293: "invalid node in part (child of part != theme): "
294: + path.toURI()
295: .getPath()
296: .substring(1)
297: + "/"
298: + partelem
299: .getNodeName()
300: + "/"
301: + themeelem
302: .getNodeName());
303: continue;
304: }
305:
306: String part = partelem.getAttribute("name");
307: String theme = themeelem
308: .getAttribute("name");
309:
310: Element res_theme = result
311: .createElement("theme");
312: res_part.appendChild(res_theme);
313: res_theme.setAttribute("name", theme);
314:
315: AuxDependency aux = AuxDependencyFactory
316: .getInstance()
317: .getAuxDependencyInclude(path,
318: part, theme);
319: if (!includes.contains(aux)) {
320: res_theme
321: .setAttribute("UNUSED", "true");
322: continue;
323: } else {
324: unavail.remove(aux);
325: }
326:
327: NodeList langchilds = themeelem
328: .getChildNodes();
329: for (int l = 0; l < langchilds.getLength(); l++) {
330: if (langchilds.item(l) instanceof Element) {
331: Element langelem = (Element) langchilds
332: .item(l);
333: Node res_lang = result.importNode(
334: langelem, true);
335: res_theme.appendChild(res_lang);
336: }
337: }
338: }
339: }
340: }
341: }
342: }
343: }
344:
345: } // CheckIncludes
|