KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xtpdoc > LooseToStrictHtml


1 /*
2  * Copyright (c) 1998-2000 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Emil Ong
28  */

29
30 package com.caucho.xtpdoc;
31
32 import com.caucho.vfs.Path;
33 import com.caucho.vfs.Vfs;
34 import com.caucho.xml.LooseHtml;
35 import com.caucho.xml.QElement;
36 import com.caucho.xml.QName;
37 import com.caucho.xml.XmlPrinter;
38
39 import org.xml.sax.SAXException JavaDoc;
40
41 import java.io.IOException JavaDoc;
42 import java.io.OutputStream JavaDoc;
43
44 public class LooseToStrictHtml {
45   private static void renameSections(org.w3c.dom.Node JavaDoc node, int level)
46   {
47     org.w3c.dom.NodeList JavaDoc nodeList = node.getChildNodes();
48
49     for (int i = 0; i < nodeList.getLength(); i++) {
50       int thisLevel = level;
51
52       org.w3c.dom.Node JavaDoc child = nodeList.item(i);
53
54       if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
55         org.w3c.dom.Element JavaDoc subElement = (org.w3c.dom.Element JavaDoc) child;
56
57         if (subElement.getTagName().equals("section")) {
58           ((QElement) subElement).setName(new QName("s" + level));
59
60           thisLevel = level + 1;
61         }
62         else if (subElement.getTagName().equals("defun")) {
63           ((QElement) subElement).setName(new QName("s" + level));
64           subElement.setAttribute("type", "defun");
65
66           thisLevel = level + 1;
67         }
68         else if (subElement.getTagName().equals("faq")) {
69           ((QElement) subElement).setName(new QName("s" + level));
70           subElement.setAttribute("type", "faq");
71
72           thisLevel = level + 1;
73         }
74       }
75
76       renameSections(child, thisLevel);
77     }
78   }
79
80   private
81   static org.w3c.dom.Element JavaDoc getXTPDocumentElement(org.w3c.dom.Node JavaDoc node)
82   {
83     org.w3c.dom.NodeList JavaDoc nodeList = node.getChildNodes();
84
85     for (int i = 0; i < nodeList.getLength(); i++) {
86       org.w3c.dom.Node JavaDoc child = nodeList.item(i);
87
88       if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
89         org.w3c.dom.Element JavaDoc subElement = (org.w3c.dom.Element JavaDoc) child;
90
91         if (subElement.getTagName().equals("document"))
92           return subElement;
93       }
94
95       org.w3c.dom.Element JavaDoc subdocument = getXTPDocumentElement(child);
96       
97       if (subdocument != null)
98         return subdocument;
99     }
100
101     return null;
102   }
103
104   public static org.w3c.dom.Element JavaDoc looseToStrictHtml(Path path)
105     throws IOException JavaDoc,SAXException JavaDoc
106   {
107     if (! path.exists())
108       throw new IOException JavaDoc(path + " does not exist");
109
110     org.w3c.dom.Document JavaDoc document = new LooseHtml().parseDocument(path);
111
112     renameSections(document.getDocumentElement(), 1);
113
114     return getXTPDocumentElement(document.getDocumentElement());
115   }
116
117   public static void main(String JavaDoc []args)
118     throws Exception JavaDoc
119   {
120     if (args.length == 0) {
121       System.err.println("usage: " + LooseToStrictHtml.class.getName() +
122                          " <xtp file>");
123       System.exit(1);
124     }
125
126     Path path = Vfs.lookup(args[0]);
127
128     org.w3c.dom.Element JavaDoc xtpDocument = looseToStrictHtml(path);
129     
130     OutputStream JavaDoc fileOut = path.openWrite();
131
132     XmlPrinter printer = new XmlPrinter(fileOut);
133
134     printer.printXml(xtpDocument);
135
136     fileOut.close();
137   }
138 }
139
Popular Tags