001: /* Copyright 2004 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021:
022: package org.jasig.portal.utils;
023:
024: import java.io.File;
025: import java.io.FileFilter;
026: import java.io.FileOutputStream;
027: import java.io.IOException;
028: import java.io.OutputStream;
029: import java.io.UnsupportedEncodingException;
030:
031: import nu.xom.Attribute;
032: import nu.xom.Builder;
033: import nu.xom.DocType;
034: import nu.xom.Document;
035: import nu.xom.Element;
036: import nu.xom.NodeFactory;
037: import nu.xom.Nodes;
038: import nu.xom.ParsingException;
039: import nu.xom.Serializer;
040: import nu.xom.Text;
041:
042: /**
043: * <p>
044: * This class converts standard Sun JavaDoc to well-formed
045: * XHTML. It requires the use of John Cowan's TagSoup.
046: * </p>
047: *
048: * @author Elliotte Rusty Harold
049: * @author George Lindholm (modified for uPortal)
050: * @version 1.0
051: *
052: */
053: class XHTMLJavaDoc {
054:
055: private static Builder builder = new Builder(
056: new org.ccil.cowan.tagsoup.Parser(), false,
057: new HTMLFixFactory());
058:
059: private static class HTMLFilter implements FileFilter {
060:
061: public boolean accept(File pathname) {
062: if (pathname.getName().endsWith(".html"))
063: return true;
064: if (pathname.isDirectory())
065: return true;
066: return false;
067: }
068:
069: }
070:
071: public static void main(String[] args) {
072:
073: try {
074: File indir = new File(args[0]);
075: process(indir);
076: } catch (Exception ex) {
077: ex.printStackTrace();
078: }
079:
080: }
081:
082: private static void process(File indir) {
083:
084: FileFilter htmlfilter = new HTMLFilter();
085: if (indir.exists() && indir.isDirectory()) {
086: File[] files = indir.listFiles(htmlfilter);
087: for (int i = 0; i < files.length; i++) {
088: File f = files[i];
089: if (f.isDirectory()) {
090: process(f);
091: } else {
092: try {
093: Document doc = builder.build(f);
094: DocType doctype = new DocType("html",
095: "-//W3C//DTD XHTML 1.0 Frameset//EN",
096: "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-frameset.dtd");
097: doc.setDocType(doctype);
098: Attribute en = new Attribute("lang", "en-US");
099: Attribute xmlen = new Attribute("xml:lang",
100: "http://www.w3.org/XML/1998/namespace",
101: "en-US");
102: Element root = doc.getRootElement();
103: root.addAttribute(en);
104: root.addAttribute(xmlen);
105: Attribute version = root
106: .getAttribute("version");
107: if (version != null)
108: root.removeAttribute(version);
109: Element body = root.getFirstChildElement(
110: "body", "http://www.w3.org/1999/xhtml");
111: Element frameset = root.getFirstChildElement(
112: "frameset",
113: "http://www.w3.org/1999/xhtml");
114: if (frameset != null && body != null) {
115: root.removeChild(body);
116: }
117: Serializer serializer = new HTMLSerializer(
118: new FileOutputStream(f));
119: if (false) {
120: serializer.setIndent(1);
121: serializer.setMaxLength(512);
122: }
123: serializer.write(doc);
124: serializer.flush();
125: } catch (ParsingException ex) {
126: ex.printStackTrace();
127: } catch (IOException ex) {
128: ex.printStackTrace();
129: }
130: }
131: }
132: } else {
133: System.err.println("Could not locate source directory: "
134: + indir);
135: }
136:
137: }
138:
139: private static class HTMLFixFactory extends NodeFactory {
140:
141: public Nodes finishMakingElement(Element element) {
142:
143: if (element.getLocalName().equals("i")) {
144: element.setLocalName("span");
145: element.addAttribute(new Attribute("style",
146: "font-style: italic"));
147: } else if (element.getLocalName().equals("b")) {
148: element.setLocalName("span");
149: element.addAttribute(new Attribute("style",
150: "font-weight: bold"));
151: }
152:
153: return new Nodes(element);
154:
155: }
156:
157: }
158:
159: private static class HTMLSerializer extends Serializer {
160:
161: HTMLSerializer(OutputStream out)
162: throws UnsupportedEncodingException {
163: super (out, "ISO-8859-1");
164: }
165:
166: protected void writeXMLDeclaration() {
167: }
168:
169: protected void writeEmptyElementTag(Element element)
170: throws IOException {
171: super.writeStartTag(element);
172: super.writeEndTag(element);
173: }
174: }
175:
176: }
|