001: package de.schlund.pfixcore.util;
002:
003: import java.io.File;
004: import java.util.HashMap;
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import javax.xml.parsers.DocumentBuilderFactory;
009:
010: import org.w3c.dom.Document;
011: import org.w3c.dom.Element;
012: import org.w3c.dom.Node;
013: import org.w3c.dom.NodeList;
014:
015: import de.schlund.pfixxml.IncludeDocument;
016: import de.schlund.pfixxml.IncludeDocumentFactory;
017: import de.schlund.pfixxml.config.GlobalConfigurator;
018: import de.schlund.pfixxml.resources.ResourceUtil;
019: import de.schlund.pfixxml.util.XPath;
020: import de.schlund.pfixxml.util.Xml;
021:
022: /**
023: * Cleanup.java
024: *
025: *
026: * Created: Tue Apr 29 14:40:15 2003
027: *
028: * @author <a href="mailto:jtl@schlund.de">Jens Lautenbacher</a>
029: * @version 1.0
030: */
031:
032: public class Cleanup {
033: private final static String CLEANUP = "Cleanup.xml";
034: private final static DocumentBuilderFactory dbfac = DocumentBuilderFactory
035: .newInstance();
036: static {
037: dbfac.setNamespaceAware(true);
038: }
039: private HashMap<String, Document> changed = new HashMap<String, Document>();
040:
041: public Cleanup() {
042: }
043:
044: public static void main(String[] args) throws Exception {
045: String pwd = new File(".").getCanonicalPath();
046: GlobalConfigurator.setDocroot(pwd);
047: Cleanup cleanup = new Cleanup();
048: cleanup.clean();
049: }
050:
051: private void clean() throws Exception {
052: Document input = Xml.parseMutable(new File(CLEANUP));
053: Element root = input.getDocumentElement();
054: NodeList nl = root.getChildNodes();
055: for (int j = 0; j < nl.getLength(); j++) {
056: if (nl.item(j) instanceof Element) {
057: Element clean = (Element) nl.item(j);
058: String name = clean.getNodeName();
059: if (name.equals("clean")) {
060: String type = clean.getAttribute("type");
061: String path = clean.getAttribute("path");
062: String part = clean.getAttribute("part");
063: String theme = clean.getAttribute("theme");
064:
065: if (!(path.indexOf("projects/common/txt") > 0)
066: && !(path.indexOf("projects/core/txt") > 0)) {
067: Document doc = (Document) changed.get(path);
068: if (doc == null
069: && (type.equals("part") || type
070: .equals("theme"))) {
071: IncludeDocument incdoc = IncludeDocumentFactory
072: .getInstance()
073: .getIncludeDocument(
074: null,
075: ResourceUtil
076: .getFileResourceFromDocroot(path),
077: true);
078: doc = incdoc.getDocument();
079: System.out.println(doc.hashCode());
080: doc.getDocumentElement().removeAttribute(
081: "incpath");
082: changed.put(path, doc);
083: }
084: if (type.equals("part")) {
085: cleanPart(doc, path, part);
086: } else if (type.equals("theme")) {
087: cleanTheme(doc, path, part, theme);
088: } else if (type.equals("file")) {
089: cleanFile(path);
090: } else {
091: System.out
092: .println("ERROR! type is " + type);
093: }
094: } else {
095: System.out.println("Ignoring " + path);
096: }
097: } else {
098: System.out.println("No clean element! " + name);
099: }
100: }
101: }
102:
103: for (Iterator<String> i = changed.keySet().iterator(); i
104: .hasNext();) {
105: String path = i.next();
106: Document doc = changed.get(path);
107: System.out.println("Saving " + path);
108:
109: Xml.serialize(doc, path, false, true);
110: }
111: }
112:
113: private void cleanPart(Document doc, String path, String part)
114: throws Exception {
115: System.out.println("Killing " + path + " => " + part);
116: List<Node> nl = XPath.select(doc,
117: "/include_parts/part[@name = '" + part + "']");
118: if (nl.size() == 1) {
119: Element partel = (Element) nl.get(0);
120: Node parent = partel.getParentNode();
121: parent.removeChild(partel);
122: } else {
123: System.out.println("More than one matching part " + path
124: + "/" + part);
125: }
126: }
127:
128: private void cleanTheme(Document doc, String path, String part,
129: String theme) throws Exception {
130: System.out.println("Killing " + path + " => " + part + " => "
131: + theme);
132: List<Node> nl = XPath.select(doc,
133: "/include_parts/part[@name = '" + part
134: + "']/theme[@name = '" + theme + "']");
135: if (nl.size() == 1) {
136: Element themeel = (Element) nl.get(0);
137: Node parent = themeel.getParentNode();
138: parent.removeChild(themeel);
139: } else {
140: System.out.println("More than one matching theme " + path
141: + "/" + part + "/" + theme);
142: }
143: }
144:
145: private void cleanFile(String path) throws Exception {
146: System.out.println("Removing file " + path);
147: new File(path).delete();
148: }
149: }
|