001: package de.schlund.pfixcore.util;
002:
003: import java.io.BufferedReader;
004: import java.io.File;
005: import java.io.FileReader;
006: import java.util.Iterator;
007: import java.util.Set;
008: import java.util.TreeSet;
009: import java.util.regex.Matcher;
010: import java.util.regex.Pattern;
011:
012: import javax.xml.parsers.DocumentBuilderFactory;
013:
014: import org.apache.log4j.xml.DOMConfigurator;
015: import org.w3c.dom.Document;
016: import org.w3c.dom.Element;
017: import org.w3c.dom.Node;
018: import org.w3c.dom.NodeList;
019:
020: import de.schlund.pfixxml.config.GlobalConfigurator;
021: import de.schlund.pfixxml.resources.FileResource;
022: import de.schlund.pfixxml.resources.ResourceUtil;
023: import de.schlund.pfixxml.util.Xml;
024:
025: /**
026: * DelLang.java
027: *
028: * @author <a href="mailto:jtl@schlund.de">Jens Lautenbacher</a>
029: * @version 1.0
030: */
031:
032: public class DelLang {
033: private final static DocumentBuilderFactory dbfac = DocumentBuilderFactory
034: .newInstance();
035: //private String docroot;
036: private Pattern pattern = Pattern.compile("^\\s*$");
037: int onelangcount = 0;
038: int multilangcount = 0;
039:
040: static {
041: dbfac.setNamespaceAware(true);
042: }
043:
044: public DelLang(String docroot) {
045: // this.docroot = docroot;
046: }
047:
048: public static void main(String[] args) throws Exception {
049: String docrt = args[0];
050: String files = args[1];
051: if (files == null || docrt == null) {
052: System.err
053: .println("Usage: java DelLang DOCROOT includefilelist");
054: System.exit(0);
055: }
056: GlobalConfigurator.setDocroot(docrt);
057: DelLang dellang = new DelLang(docrt);
058: DOMConfigurator.configure("core/conf/generator_quiet.xml");
059: dellang.transform(files);
060: }
061:
062: public void transform(String files) throws Exception {
063:
064: BufferedReader input = new BufferedReader(new FileReader(files));
065: Set<FileResource> inames = new TreeSet<FileResource>();
066: String line;
067:
068: while ((line = input.readLine()) != null) {
069: inames.add(ResourceUtil.getFileResourceFromDocroot(line
070: .substring(2)));
071: }
072: input.close();
073:
074: Document doc;
075:
076: for (Iterator<FileResource> i = inames.iterator(); i.hasNext();) {
077: FileResource path = i.next();
078:
079: System.out.print(path + ":");
080: doc = Xml.parseMutable(path);
081: handleDoc(doc);
082:
083: File out = File.createTempFile("temp", ".TEMPFILE");
084: out.getParentFile().mkdirs();
085: Xml.serialize(doc, out, false, true);
086:
087: ResourceUtil.copy(
088: ResourceUtil.getFileResource(out.toURI()), path);
089: out.delete();
090: System.out.println("");
091: }
092: System.out.println("Multilang: " + multilangcount
093: + " Only default lang: " + onelangcount);
094: }
095:
096: public void handleDoc(Document doc) {
097:
098: Element root = doc.getDocumentElement();
099: // System.out.println("NN: " + root.getNodeName());
100: NodeList rootchildren = root.getChildNodes();
101: int localmultilangcount = 0;
102:
103: for (int i = 0; i < rootchildren.getLength(); i++) {
104: Node rootchild = rootchildren.item(i);
105: if (rootchild.getNodeType() == Node.ELEMENT_NODE
106: && rootchild.getNodeName().equals("part")) {
107: String partname = ((Element) rootchild)
108: .getAttribute("name");
109: NodeList partchildren = rootchild.getChildNodes();
110: for (int j = 0; j < partchildren.getLength(); j++) {
111: Node partchild = partchildren.item(j);
112: if (partchild.getNodeType() == Node.ELEMENT_NODE
113: && partchild.getNodeName()
114: .equals("product")) {
115: String productname = ((Element) partchild)
116: .getAttribute("name");
117: NodeList prodchildren = partchild
118: .getChildNodes();
119: int count = prodchildren.getLength();
120: boolean multilang = false;
121: // First of all we check if there is any other lang node than "<lang name="default">".
122: for (int k = 0; k < count; k++) {
123: Node tmp = prodchildren.item(k);
124: if (tmp.getNodeType() == Node.ELEMENT_NODE) {
125: if (tmp.getNodeName().equals("lang")) {
126: Element tmpelem = (Element) tmp;
127: if (!tmpelem.getAttribute("name")
128: .equals("default")) {
129: multilang = true;
130: }
131: } else {
132: System.out
133: .println("*** Wrong element "
134: + tmp.getNodeName()
135: + " under part/product "
136: + partname
137: + "/"
138: + productname);
139: System.exit(1);
140: }
141: }
142: }
143: if (multilang) {
144: multilangcount++;
145: localmultilangcount++;
146: } else {
147: onelangcount++;
148: }
149: Element theme = doc.createElement("theme");
150: theme.setAttribute("name",
151: ((Element) partchild)
152: .getAttribute("name"));
153:
154: Element pfxlang = null;
155: if (multilang) {
156: pfxlang = doc
157: .createElement("pfx:langselect");
158: theme.appendChild(pfxlang);
159: }
160:
161: for (int k = 0; k < count; k++) {
162: Node prodchild = prodchildren.item(0); // Stupid stuff...
163: partchild.removeChild(prodchild);
164: if (prodchild.getNodeType() == Node.ELEMENT_NODE
165: && !multilang) {
166: NodeList langchildren = prodchild
167: .getChildNodes();
168: int langcount = langchildren
169: .getLength();
170: for (int l = 0; l < langcount; l++) {
171: Node langchild = langchildren
172: .item(0);
173: prodchild.removeChild(langchild);
174: theme.appendChild(langchild);
175: }
176: } else if (!multilang) {
177: if ((k == 0 || k == (count - 1))
178: && prodchild.getNodeType() == Node.TEXT_NODE) {
179: Matcher matcher = pattern
180: .matcher(prodchild
181: .getNodeValue());
182: if (!matcher.matches()) {
183: System.out
184: .println("==>"
185: + partname
186: + "/"
187: + productname
188: + ":"
189: + prodchild
190: .getNodeValue());
191: theme.appendChild(prodchild);
192: }
193: }
194: } else if (prodchild.getNodeType() == Node.ELEMENT_NODE) {
195: Element langinst = doc
196: .createElement("pfx:lang");
197: langinst.setAttribute("name",
198: ((Element) prodchild)
199: .getAttribute("name"));
200: NodeList langchildren = prodchild
201: .getChildNodes();
202: int langcount = langchildren
203: .getLength();
204: for (int l = 0; l < langcount; l++) {
205: Node langchild = langchildren
206: .item(0); //
207: prodchild.removeChild(langchild);
208: langinst.appendChild(langchild);
209: }
210: pfxlang.appendChild(langinst);
211: } else {
212: pfxlang.appendChild(prodchild);
213: }
214:
215: }
216: rootchild.replaceChild(theme, partchild);
217: }
218: }
219: }
220: }
221: System.out.print(" (" + localmultilangcount + ") ");
222: }
223:
224: }
|