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:
020: package de.schlund.pfixxml.targets;
021:
022: import java.io.IOException;
023: import java.util.HashMap;
024: import java.util.HashSet;
025: import java.util.Iterator;
026: import java.util.Set;
027: import java.util.TreeSet;
028:
029: import org.apache.log4j.Logger;
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Element;
032: import org.w3c.dom.NodeList;
033:
034: import de.schlund.pfixxml.resources.DocrootResource;
035: import de.schlund.pfixxml.resources.FileResource;
036: import de.schlund.pfixxml.resources.ResourceUtil;
037: import de.schlund.pfixxml.util.Xml;
038:
039: /**
040: * AuxDependencyManager.java
041: *
042: *
043: * Created: Tue Jul 17 12:24:15 2001
044: *
045: * @author <a href="mailto: jtl@schlund.de">Jens Lautenbacher</a>
046: *
047: *
048: */
049:
050: public class AuxDependencyManager {
051: private final static Logger LOG = Logger
052: .getLogger(AuxDependencyManager.class);
053: private final static String DEPAUX = "depaux";
054: private TargetImpl target;
055:
056: public final static AuxDependency root = AuxDependencyFactory
057: .getInstance().getAuxDependencyRoot();
058:
059: public AuxDependencyManager(Target target) {
060: this .target = (TargetImpl) target;
061: }
062:
063: public synchronized void tryInitAuxdepend() throws Exception {
064: FileResource auxfile = ResourceUtil.getFileResource(target
065: .getTargetGenerator().getDisccachedir(), target
066: .getTargetKey()
067: + ".aux");
068: if (auxfile.exists() && auxfile.canRead() && auxfile.isFile()) {
069: Document doc = Xml.parseMutable(auxfile);
070: NodeList auxdeps = doc.getElementsByTagName(DEPAUX);
071: if (auxdeps.getLength() > 0) {
072: for (int j = 0; j < auxdeps.getLength(); j++) {
073: String type = ((Element) auxdeps.item(j))
074: .getAttribute("type");
075: String path_attr = ((Element) auxdeps.item(j))
076: .getAttribute("path");
077: DocrootResource path = "".equals(path_attr) ? null
078: : ResourceUtil
079: .getFileResourceFromDocroot(path_attr);
080: String part = ((Element) auxdeps.item(j))
081: .getAttribute("part");
082: String product = ((Element) auxdeps.item(j))
083: .getAttribute("product");
084: String parent_attr = ((Element) auxdeps.item(j))
085: .getAttribute("parent_path");
086: DocrootResource parent_path = ""
087: .equals(parent_attr) ? null : ResourceUtil
088: .getFileResourceFromDocroot(parent_attr);
089: String parent_part = ((Element) auxdeps.item(j))
090: .getAttribute("parent_part");
091: String parent_product = ((Element) auxdeps.item(j))
092: .getAttribute("parent_product");
093: String target_attr = ((Element) auxdeps.item(j))
094: .getAttribute("target");
095:
096: DependencyType thetype = DependencyType
097: .getByTag(type);
098: if (thetype == DependencyType.TEXT) {
099: addDependencyInclude(path, part, product,
100: parent_path, parent_part,
101: parent_product);
102: } else if (thetype == DependencyType.IMAGE) {
103: addDependencyImage(path, parent_path,
104: parent_part, parent_product);
105: } else if (thetype == DependencyType.TARGET) {
106: addDependencyTarget(target_attr);
107: }
108: }
109: }
110: }
111: }
112:
113: private AuxDependency getParentDependency(
114: DocrootResource parent_path, String parent_part,
115: String parent_theme) {
116: AuxDependency parent = null;
117:
118: if (parent_part != null && parent_part.equals(""))
119: parent_part = null;
120: if (parent_theme != null && parent_theme.equals(""))
121: parent_theme = null;
122:
123: if (parent_path != null && parent_part != null
124: && parent_theme != null) {
125: LOG.debug("*** Found another AuxDependency as Parent...");
126: parent = AuxDependencyFactory.getInstance()
127: .getAuxDependencyInclude(parent_path, parent_part,
128: parent_theme);
129: } else if (parent_path == null && parent_part == null
130: && parent_theme == null) {
131: parent = root;
132: }
133:
134: if (parent != null) {
135: return parent;
136: } else {
137: throw new IllegalArgumentException(
138: "Mixed null and non-null values for parent arguments!");
139: }
140: }
141:
142: public synchronized void addDependencyInclude(DocrootResource path,
143: String part, String theme, DocrootResource parent_path,
144: String parent_part, String parent_theme) {
145: if (path == null || part == null || theme == null) {
146: throw new IllegalArgumentException(
147: "Null pointer is not allowed here");
148: }
149:
150: AuxDependency child = null;
151: AuxDependency parent = null;
152:
153: if (part != null && part.equals(""))
154: part = null;
155: if (theme != null && theme.equals(""))
156: theme = null;
157: LOG.info("Adding Dependency of type 'text' to Target '"
158: + target.getFullName() + "':");
159: LOG.info("*** ["
160: + path.getRelativePath()
161: + "]["
162: + part
163: + "]["
164: + theme
165: + "]["
166: + ((parent_path == null) ? "null" : parent_path
167: .getRelativePath()) + "][" + parent_part + "]["
168: + parent_theme + "]");
169:
170: child = AuxDependencyFactory.getInstance()
171: .getAuxDependencyInclude(path, part, theme);
172: parent = getParentDependency(parent_path, parent_part,
173: parent_theme);
174:
175: TargetDependencyRelation.getInstance().addRelation(parent,
176: child, target);
177: }
178:
179: public synchronized void addDependencyImage(DocrootResource path,
180: DocrootResource parent_path, String parent_part,
181: String parent_theme) {
182: if (path == null) {
183: throw new IllegalArgumentException(
184: "Null pointer is not allowed here");
185: }
186:
187: AuxDependency child = null;
188: AuxDependency parent = null;
189:
190: LOG.info("Adding Dependency of type 'text' to Target '"
191: + target.getFullName() + "':");
192: LOG.info("*** ["
193: + path.getRelativePath()
194: + "]["
195: + ((parent_path == null) ? "null" : parent_path
196: .getRelativePath()) + "][" + parent_part + "]["
197: + parent_theme + "]");
198:
199: child = AuxDependencyFactory.getInstance()
200: .getAuxDependencyImage(path);
201: parent = getParentDependency(parent_path, parent_part,
202: parent_theme);
203:
204: TargetDependencyRelation.getInstance().addRelation(parent,
205: child, target);
206: }
207:
208: public synchronized void addDependencyFile(DocrootResource path) {
209: if (path == null) {
210: throw new IllegalArgumentException(
211: "Null pointer is not allowed here");
212: }
213:
214: AuxDependency child = null;
215:
216: LOG.info("Adding Dependency of type 'text' to Target '"
217: + target.getFullName() + "':");
218: LOG.info("*** [" + path.getRelativePath() + "]");
219:
220: child = AuxDependencyFactory.getInstance()
221: .getAuxDependencyFile(path);
222:
223: TargetDependencyRelation.getInstance().addRelation(root, child,
224: target);
225: }
226:
227: public synchronized void addDependencyTarget(String targetkey) {
228: if (target == null) {
229: throw new IllegalArgumentException(
230: "Null pointer is not allowed here");
231: }
232:
233: AuxDependency child = null;
234:
235: LOG.info("Adding Dependency of type 'text' to Target '"
236: + target.getFullName() + "':");
237: LOG.info("*** [" + target.getTargetKey() + "]");
238:
239: child = AuxDependencyFactory.getInstance()
240: .getAuxDependencyTarget(target.getTargetGenerator(),
241: targetkey);
242:
243: TargetDependencyRelation.getInstance().addRelation(root, child,
244: target);
245: }
246:
247: public synchronized void reset() {
248: TargetDependencyRelation.getInstance().resetRelation(target);
249: }
250:
251: /**
252: * Returns the highest (= newest) timestamp of all aux dependencies
253: * (include parts, images, files) managed through this manager.
254: * This does NOT include any aux targets.
255: *
256: * @return Timestamp of latest change in any dependency
257: */
258: public long getMaxTimestamp() {
259: Set<AuxDependency> allaux = TargetDependencyRelation
260: .getInstance().getDependenciesForTarget(target);
261: long max = 0;
262:
263: if (allaux != null) {
264: for (Iterator<AuxDependency> i = allaux.iterator(); i
265: .hasNext();) {
266: AuxDependency aux = i.next();
267: if (aux.getType() != DependencyType.TARGET) {
268: max = Math.max(max, aux.getModTime());
269: }
270: }
271: }
272: return max;
273: }
274:
275: public synchronized void saveAuxdepend() throws IOException {
276: LOG.info("===> Trying to save aux info of Target '"
277: + target.getTargetKey() + "'");
278:
279: FileResource path = ResourceUtil.getFileResource(target
280: .getTargetGenerator().getDisccachedir(), target
281: .getTargetKey()
282: + ".aux");
283: FileResource dir = path.getParentAsFileResource();
284:
285: // Make sure parent directory is existing (for leaf targets)
286: if (dir != null) {
287: dir.mkdirs();
288: }
289:
290: HashMap<AuxDependency, HashSet<AuxDependency>> parentchild = TargetDependencyRelation
291: .getInstance().getParentChildMapForTarget(target);
292:
293: Document auxdoc = Xml.createDocument();
294: Element rootelem = auxdoc.createElement("aux");
295:
296: auxdoc.appendChild(rootelem);
297:
298: if (parentchild != null) {
299: for (Iterator<AuxDependency> i = parentchild.keySet()
300: .iterator(); i.hasNext();) {
301: AuxDependency parent = i.next();
302: DocrootResource parent_path = null;
303: String parent_part = null;
304: String parent_theme = null;
305:
306: if (parent != root) {
307: AuxDependencyInclude aux = (AuxDependencyInclude) parent;
308: parent_path = aux.getPath();
309: parent_part = aux.getPart();
310: parent_theme = aux.getTheme();
311: }
312:
313: HashSet<AuxDependency> children = parentchild
314: .get(parent);
315:
316: for (Iterator<AuxDependency> j = children.iterator(); j
317: .hasNext();) {
318: AuxDependency aux = j.next();
319: DependencyType type = aux.getType();
320:
321: if (type.isDynamic()) {
322: Element depaux = auxdoc.createElement(DEPAUX);
323: rootelem.appendChild(depaux);
324: depaux.setAttribute("type", type.getTag());
325: if (aux.getType() == DependencyType.TEXT) {
326: AuxDependencyInclude a = (AuxDependencyInclude) aux;
327: depaux.setAttribute("path", a.getPath()
328: .getRelativePath());
329: depaux.setAttribute("part", a.getPart());
330: depaux
331: .setAttribute("product", a
332: .getTheme());
333: if (parent_path != null)
334: depaux.setAttribute("parent_path",
335: parent_path.getRelativePath());
336: if (parent_part != null)
337: depaux.setAttribute("parent_part",
338: parent_part);
339: if (parent_theme != null)
340: depaux.setAttribute("parent_product",
341: parent_theme);
342: } else if (aux.getType() == DependencyType.IMAGE) {
343: AuxDependencyImage a = (AuxDependencyImage) aux;
344: depaux.setAttribute("path", a.getPath()
345: .getRelativePath());
346: if (parent_path != null)
347: depaux.setAttribute("parent_path",
348: parent_path.getRelativePath());
349: if (parent_part != null)
350: depaux.setAttribute("parent_part",
351: parent_part);
352: if (parent_theme != null)
353: depaux.setAttribute("parent_product",
354: parent_theme);
355: } else if (aux.getType() == DependencyType.TARGET) {
356: Target target = ((AuxDependencyTarget) aux)
357: .getTarget();
358: depaux.setAttribute("target", target
359: .getTargetKey());
360: }
361: }
362: }
363: }
364: }
365: Xml.serialize(auxdoc, path, true, true);
366: }
367:
368: public TreeSet<AuxDependency> getChildren() {
369: HashMap<AuxDependency, HashSet<AuxDependency>> parentchild = TargetDependencyRelation
370: .getInstance().getParentChildMapForTarget(target);
371:
372: TreeSet<AuxDependency> retval = new TreeSet<AuxDependency>();
373:
374: if (parentchild != null && parentchild.get(root) != null) {
375: retval.addAll(parentchild.get(root));
376: }
377:
378: return retval;
379: }
380:
381: }// AuxDependencyManager
|