001: /*
002: * Copyright (c) 1998-2000 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.xtpdoc;
031:
032: import com.caucho.vfs.Path;
033: import com.caucho.vfs.Vfs;
034: import com.caucho.xml.LooseHtml;
035: import com.caucho.xml.QElement;
036: import com.caucho.xml.QName;
037: import com.caucho.xml.XmlPrinter;
038:
039: import org.xml.sax.SAXException;
040:
041: import java.io.IOException;
042: import java.io.OutputStream;
043:
044: public class LooseToStrictHtml {
045: private static void renameSections(org.w3c.dom.Node node, int level) {
046: org.w3c.dom.NodeList nodeList = node.getChildNodes();
047:
048: for (int i = 0; i < nodeList.getLength(); i++) {
049: int this Level = level;
050:
051: org.w3c.dom.Node child = nodeList.item(i);
052:
053: if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
054: org.w3c.dom.Element subElement = (org.w3c.dom.Element) child;
055:
056: if (subElement.getTagName().equals("section")) {
057: ((QElement) subElement).setName(new QName("s"
058: + level));
059:
060: this Level = level + 1;
061: } else if (subElement.getTagName().equals("defun")) {
062: ((QElement) subElement).setName(new QName("s"
063: + level));
064: subElement.setAttribute("type", "defun");
065:
066: this Level = level + 1;
067: } else if (subElement.getTagName().equals("faq")) {
068: ((QElement) subElement).setName(new QName("s"
069: + level));
070: subElement.setAttribute("type", "faq");
071:
072: this Level = level + 1;
073: }
074: }
075:
076: renameSections(child, this Level);
077: }
078: }
079:
080: private static org.w3c.dom.Element getXTPDocumentElement(
081: org.w3c.dom.Node node) {
082: org.w3c.dom.NodeList nodeList = node.getChildNodes();
083:
084: for (int i = 0; i < nodeList.getLength(); i++) {
085: org.w3c.dom.Node child = nodeList.item(i);
086:
087: if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
088: org.w3c.dom.Element subElement = (org.w3c.dom.Element) child;
089:
090: if (subElement.getTagName().equals("document"))
091: return subElement;
092: }
093:
094: org.w3c.dom.Element subdocument = getXTPDocumentElement(child);
095:
096: if (subdocument != null)
097: return subdocument;
098: }
099:
100: return null;
101: }
102:
103: public static org.w3c.dom.Element looseToStrictHtml(Path path)
104: throws IOException, SAXException {
105: if (!path.exists())
106: throw new IOException(path + " does not exist");
107:
108: org.w3c.dom.Document document = new LooseHtml()
109: .parseDocument(path);
110:
111: renameSections(document.getDocumentElement(), 1);
112:
113: return getXTPDocumentElement(document.getDocumentElement());
114: }
115:
116: public static void main(String[] args) throws Exception {
117: if (args.length == 0) {
118: System.err
119: .println("usage: "
120: + LooseToStrictHtml.class.getName()
121: + " <xtp file>");
122: System.exit(1);
123: }
124:
125: Path path = Vfs.lookup(args[0]);
126:
127: org.w3c.dom.Element xtpDocument = looseToStrictHtml(path);
128:
129: OutputStream fileOut = path.openWrite();
130:
131: XmlPrinter printer = new XmlPrinter(fileOut);
132:
133: printer.printXml(xtpDocument);
134:
135: fileOut.close();
136: }
137: }
|