1 7 8 package sample; 9 10 import java.io.BufferedReader ; 11 import java.io.IOException ; 12 import java.io.PrintWriter ; 13 import java.io.StringReader ; 14 import java.io.StringWriter ; 15 16 import org.cyberneko.html.HTMLConfiguration; 17 import org.cyberneko.html.filters.DefaultFilter; 18 import org.cyberneko.html.filters.Identity; 19 import org.cyberneko.html.filters.Writer; 20 21 import org.apache.xerces.util.XMLAttributesImpl; 22 import org.apache.xerces.xni.Augmentations; 23 import org.apache.xerces.xni.QName; 24 import org.apache.xerces.xni.XMLAttributes; 25 import org.apache.xerces.xni.XMLLocator; 26 import org.apache.xerces.xni.XMLString; 27 import org.apache.xerces.xni.XNIException; 28 import org.apache.xerces.xni.parser.XMLDocumentFilter; 29 import org.apache.xerces.xni.parser.XMLInputSource; 30 31 67 public class Script 68 extends DefaultFilter { 69 70 74 75 protected static final String AUGMENTATIONS = "http://cyberneko.org/html/features/augmentations"; 76 77 78 protected static final String FILTERS = "http://cyberneko.org/html/properties/filters"; 79 80 81 protected static final String SCRIPT_TYPE = "text/x-nekoscript"; 82 83 87 88 protected HTMLConfiguration fConfiguration; 89 90 91 protected StringBuffer fBuffer; 92 93 94 protected String fSystemId; 95 96 97 protected int fScriptCount; 98 99 103 104 public Script(HTMLConfiguration config) { 105 fConfiguration = config; 106 } 108 112 113 public void startDocument(XMLLocator locator, String encoding, Augmentations augs) 114 throws XNIException { 115 fBuffer = null; 116 fSystemId = locator != null ? locator.getLiteralSystemId() : null; 117 fScriptCount = 0; 118 super.startDocument(locator, encoding, augs); 119 } 121 122 public void startElement(QName element, XMLAttributes attrs, Augmentations augs) 123 throws XNIException { 124 if (element.rawname.equalsIgnoreCase("script") && attrs != null) { 125 String value = attrs.getValue("type"); 126 if (value != null && value.equalsIgnoreCase(SCRIPT_TYPE)) { 127 fBuffer = new StringBuffer (); 128 return; 129 } 130 } 131 super.startElement(element, attrs, augs); 132 } 134 135 public void emptyElement(QName element, XMLAttributes attrs, Augmentations augs) 136 throws XNIException { 137 if (element.rawname.equalsIgnoreCase("script") && attrs != null) { 138 String value = attrs.getValue("type"); 139 if (value != null && value.equalsIgnoreCase(SCRIPT_TYPE)) { 140 return; 141 } 142 } 143 super.emptyElement(element, attrs, augs); 144 } 146 147 public void characters(XMLString text, Augmentations augs) 148 throws XNIException { 149 if (fBuffer != null) { 150 fBuffer.append(text.ch, text.offset, text.length); 151 } 152 else { 153 super.characters(text, augs); 154 } 155 } 157 158 public void endElement(QName element, Augmentations augs) throws XNIException { 159 if (fBuffer != null) { 160 try { 161 BufferedReader in = new BufferedReader (new StringReader (fBuffer.toString())); 163 StringWriter sout = new StringWriter (); 164 PrintWriter out = new PrintWriter (sout); 165 String line; 166 while ((line = in.readLine()) != null) { 167 line.trim(); 168 if (line.length() == 0) { 169 continue; 170 } 171 switch (line.charAt(0)) { 172 case '(': { 173 out.print('<'); 174 out.print(line.substring(1)); 175 out.print('>'); 176 break; 177 } 178 case '"': { 179 out.print(line.substring(1)); 180 break; 181 } 182 case ')': { 183 out.print("</"); 184 out.print(line.substring(1)); 185 out.print('>'); 186 break; 187 } 188 } 189 } 190 191 String systemId = fSystemId != null ? fSystemId+'_' : ""; 193 fScriptCount++; 194 systemId += "script"+fScriptCount; 195 XMLInputSource source = new XMLInputSource(null, systemId, null, 196 new StringReader (sout.toString()), 197 "UTF-8"); 198 fConfiguration.pushInputSource(source); 199 } 200 catch (IOException e) { 201 } 203 finally { 204 fBuffer = null; 205 } 206 } 207 else { 208 super.endElement(element, augs); 209 } 210 } 212 216 217 public static void main(String [] argv) throws Exception { 218 HTMLConfiguration parser = new HTMLConfiguration(); 219 parser.setFeature(AUGMENTATIONS, true); 220 XMLDocumentFilter[] filters = { new Script(parser), new Identity(), new Writer () }; 221 parser.setProperty(FILTERS, filters); 222 for (int i = 0; i < argv.length; i++) { 223 parser.parse(new XMLInputSource(null, argv[i], null)); 224 } 225 } 227 } | Popular Tags |