KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > xhtml > XHTMLConverter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.wizards.xhtml;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18 import java.io.StringWriter JavaDoc;
19 import java.util.Stack JavaDoc;
20
21 import org.apache.lucene.demo.html.HTMLParser;
22 import org.apache.lucene.demo.html.HTMLParserConstants;
23 import org.apache.lucene.demo.html.Token;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IProgressMonitor;
28
29 public class XHTMLConverter {
30
31     public static final int XHTML_STRICT = 0;
32     public static final int XHTML_TRANSITIONAL = 1;
33     public static final int XHTML_FRAMESET = 2;
34     private static final String JavaDoc[] XHTML_DOCTYPES = new String JavaDoc[3];
35     static {
36         XHTML_DOCTYPES[XHTML_STRICT] =
37             "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"; //$NON-NLS-1$
38
XHTML_DOCTYPES[XHTML_TRANSITIONAL] =
39             "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"; //$NON-NLS-1$
40
XHTML_DOCTYPES[XHTML_FRAMESET] =
41             "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">"; //$NON-NLS-1$
42
}
43     private static final String JavaDoc XHTML_DEFAULT_DOCTYPE = XHTML_DOCTYPES[XHTML_TRANSITIONAL];
44     private static final String JavaDoc XMLNS = "xmlns"; //$NON-NLS-1$
45
private static final String JavaDoc XMLNS_LOC = "http://www.w3.org/1999/xhtml"; //$NON-NLS-1$
46

47     private int fDoctype;
48     
49     public XHTMLConverter(int docType) {
50         fDoctype = docType;
51     }
52     
53     public void setType(int docType) {
54         fDoctype = docType;
55     }
56     
57     public void convert(IFile htmlIFile, IProgressMonitor monitor) throws CoreException {
58         if (!htmlIFile.exists())
59             return;
60         File JavaDoc htmlFile = new File JavaDoc(htmlIFile.getLocation().toString());
61         
62         StringWriter JavaDoc writer = new StringWriter JavaDoc();
63         PrintWriter JavaDoc pwriter = new PrintWriter JavaDoc(writer);
64         write(htmlFile, pwriter);
65         monitor.worked(1);
66         pwriter.flush();
67         pwriter.close();
68         
69         writer.flush();
70         try {
71             modifyFile(htmlIFile, writer, monitor);
72             writer.close();
73         } catch (IOException JavaDoc e) {
74         }
75     }
76     
77     private void write(File JavaDoc file, PrintWriter JavaDoc pw) {
78         try {
79             Stack JavaDoc tagStack = new Stack JavaDoc();
80             HTMLParser parser = new HTMLParser(file);
81             pw.println(getDoctypeString(fDoctype));
82             XHTMLTag htmlTag = grabNextTag(parser, "<html", pw); //$NON-NLS-1$
83
// fill in any remaning attributes the html tag had
84
convertTagContents(parser, htmlTag);
85             htmlTag.addAttribute(XMLNS, XMLNS_LOC);
86             htmlTag.write(pw);
87             tagStack.push(htmlTag);
88             
89             Token token = parser.getNextToken();
90             while (isValid(token)) {
91                 switch (token.kind) {
92                 case HTMLParserConstants.TagName:
93                     XHTMLTag tag = new XHTMLTag(token.image, fDoctype);
94                     convertTagContents(parser, tag);
95                     if (tag.isClosingTag()) {
96                         // Closinsg tag encountered:
97
// - pop a tag from the stack and close it
98
if (tagStack.isEmpty())
99                             break;
100                         XHTMLTag topStack = (XHTMLTag)tagStack.pop();
101                         topStack.writeClosed(pw);
102                         break;
103                     }
104                     if (!tag.isEmptyTag())
105                         // Non-empty tags get pushed on the stack for closing
106
tagStack.push(tag);
107                     
108                     tag.write(pw);
109                     break;
110                 default:
111                     pw.print(token.image);
112                 }
113                 token = parser.getNextToken();
114             }
115             
116             // close all remaining tags
117
while (!tagStack.isEmpty()) {
118                 XHTMLTag topStack = (XHTMLTag)tagStack.pop();
119                 topStack.writeClosed(pw);
120             }
121         } catch (FileNotFoundException JavaDoc e) {
122         }
123     }
124     
125     private void modifyFile(IFile htmlFile, StringWriter JavaDoc writer, IProgressMonitor monitor) throws CoreException, IOException JavaDoc {
126         // set new contents
127
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(writer.toString().getBytes());
128         htmlFile.setContents(bais, IResource.KEEP_HISTORY | IResource.FORCE, monitor);
129         bais.close();
130         monitor.worked(1);
131     }
132     
133     private XHTMLTag grabNextTag(HTMLParser parser, String JavaDoc tag, PrintWriter JavaDoc pw) {
134         Token token = parser.getNextToken();
135         while (isValid(token)) {
136             if (token.kind == HTMLParserConstants.TagName && token.image.equalsIgnoreCase(tag))
137                 return new XHTMLTag(token.image, fDoctype);
138             
139             // print out all comments on the way to tag
140
if (isCommentToken(token)) {
141                 while (isCommentToken(token)) {
142                     pw.print(token.image);
143                     token = parser.getNextToken();
144                 }
145                 pw.println();
146             } else
147                 token = parser.getNextToken();
148         }
149         return null;
150     }
151     
152     private boolean isCommentToken(Token token) {
153         int kind = token.kind;
154         return token != null && (
155                 kind == HTMLParserConstants.Comment1 ||
156                 kind == HTMLParserConstants.Comment2 ||
157                 kind == HTMLParserConstants.CommentText1 ||
158                 kind == HTMLParserConstants.CommentText2 ||
159                 kind == HTMLParserConstants.CommentEnd1 ||
160                 kind == HTMLParserConstants.CommentEnd2);
161     }
162     
163     private void convertTagContents(HTMLParser parser, XHTMLTag tag) {
164         if (tag == null)
165             return;
166         Token token = parser.getNextToken();
167         while (isValid(token) && token.kind != HTMLParserConstants.TagEnd) {
168             tag.eatToken(token);
169             token = parser.getNextToken();
170         }
171         tag.expandLeftoverAttribute();
172         // last token read is either invalid or a TagEnd - we don't care about either
173
}
174     
175     private boolean isValid(Token token) {
176         return token != null && token.kind != HTMLParserConstants.EOF;
177     }
178     
179     private String JavaDoc getDoctypeString(int version) {
180         if (version != XHTML_FRAMESET &&
181                 version != XHTML_STRICT &&
182                 version != XHTML_FRAMESET)
183             return XHTML_DEFAULT_DOCTYPE;
184         return XHTML_DOCTYPES[version];
185     }
186 }
187
Popular Tags