1 24 25 package org.gjt.sp.util; 26 27 import java.io.BufferedReader ; 28 import java.io.InputStream ; 29 import java.io.InputStreamReader ; 30 import java.io.IOException ; 31 import java.io.Reader ; 32 33 import org.xml.sax.InputSource ; 34 import org.xml.sax.SAXException ; 35 import org.xml.sax.SAXParseException ; 36 import org.xml.sax.XMLReader ; 37 import org.xml.sax.helpers.DefaultHandler ; 38 import org.xml.sax.helpers.XMLReaderFactory ; 39 40 47 public class XMLUtilities 48 { 49 50 62 public static String charsToEntities(String str, boolean xml11) 63 { 64 StringBuffer buf = new StringBuffer (str.length()); 65 for(int i = 0; i < str.length(); i++) 66 { 67 char ch = str.charAt(i); 68 69 if (xml11 && ch < 32 && ch != '\r' && ch != '\n' && ch != '\t') 71 { 72 buf.append("&#").append((int)ch).append(";"); 73 continue; 74 } 75 76 switch(ch) 77 { 78 case '<': 79 buf.append("<"); 80 break; 81 case '>': 82 buf.append(">"); 83 break; 84 case '&': 85 buf.append("&"); 86 break; 87 default: 88 buf.append(ch); 89 break; 90 } 91 } 92 return buf.toString(); 93 } 95 112 public static boolean parseXML(InputStream in, DefaultHandler handler) 113 throws IOException 114 { 115 Reader r = null; 116 try 117 { 118 XMLReader parser = XMLReaderFactory.createXMLReader(); 119 r = new BufferedReader (new InputStreamReader (in)); 120 InputSource isrc = new InputSource (r); 121 isrc.setSystemId("jedit.jar"); 122 parser.setContentHandler(handler); 123 parser.setDTDHandler(handler); 124 parser.setEntityResolver(handler); 125 parser.setErrorHandler(handler); 126 parser.parse(isrc); 127 } 128 catch(SAXParseException se) 129 { 130 int line = se.getLineNumber(); 131 String message = se.getMessage(); 132 Log.log(Log.ERROR,XMLUtilities.class, 133 "while parsing from " + in + ": SAXParseException: line " + line + ": " , se); 134 return true; 135 } 136 catch(SAXException e) 137 { 138 Log.log(Log.ERROR,XMLUtilities.class,e); 139 return true; 140 } 141 finally 142 { 143 try 144 { 145 if(in != null) 146 in.close(); 147 } 148 catch(IOException io) 149 { 150 Log.log(Log.ERROR,XMLUtilities.class,io); 151 } 152 } 153 return false; 154 } 156 168 public static InputSource findEntity(String systemId, String test, Class where) 169 { 170 if (systemId != null && systemId.endsWith(test)) 171 { 172 try 173 { 174 return new InputSource (new BufferedReader ( 175 new InputStreamReader ( 176 where.getResourceAsStream(test)))); 177 } 178 catch (Exception e) 179 { 180 Log.log(Log.ERROR,XMLUtilities.class, 181 "Error while opening " + test + ":"); 182 Log.log(Log.ERROR,XMLUtilities.class,e); 183 } 184 } 185 186 return null; 187 } 189 private XMLUtilities() { } 190 } 191 192 | Popular Tags |