1 11 package org.eclipse.pde.internal.ui.wizards.xhtml; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.File ; 15 import java.io.FileNotFoundException ; 16 import java.io.IOException ; 17 import java.io.PrintWriter ; 18 import java.io.StringWriter ; 19 import java.util.Stack ; 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 [] XHTML_DOCTYPES = new String [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\">"; 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\">"; 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\">"; } 43 private static final String XHTML_DEFAULT_DOCTYPE = XHTML_DOCTYPES[XHTML_TRANSITIONAL]; 44 private static final String XMLNS = "xmlns"; private static final String XMLNS_LOC = "http://www.w3.org/1999/xhtml"; 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 htmlFile = new File (htmlIFile.getLocation().toString()); 61 62 StringWriter writer = new StringWriter (); 63 PrintWriter pwriter = new PrintWriter (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 e) { 74 } 75 } 76 77 private void write(File file, PrintWriter pw) { 78 try { 79 Stack tagStack = new Stack (); 80 HTMLParser parser = new HTMLParser(file); 81 pw.println(getDoctypeString(fDoctype)); 82 XHTMLTag htmlTag = grabNextTag(parser, "<html", pw); 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 if (tagStack.isEmpty()) 99 break; 100 XHTMLTag topStack = (XHTMLTag)tagStack.pop(); 101 topStack.writeClosed(pw); 102 break; 103 } 104 if (!tag.isEmptyTag()) 105 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 while (!tagStack.isEmpty()) { 118 XHTMLTag topStack = (XHTMLTag)tagStack.pop(); 119 topStack.writeClosed(pw); 120 } 121 } catch (FileNotFoundException e) { 122 } 123 } 124 125 private void modifyFile(IFile htmlFile, StringWriter writer, IProgressMonitor monitor) throws CoreException, IOException { 126 ByteArrayInputStream bais = new ByteArrayInputStream (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 tag, PrintWriter 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 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 } 174 175 private boolean isValid(Token token) { 176 return token != null && token.kind != HTMLParserConstants.EOF; 177 } 178 179 private String 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 |