1 64 package com.jcorporate.expresso.core.misc; 65 66 import org.apache.log4j.helpers.LogLog; 67 import org.xml.sax.ErrorHandler ; 68 import org.xml.sax.InputSource ; 69 import org.xml.sax.SAXException ; 70 import org.xml.sax.SAXParseException ; 71 import org.xml.sax.ext.LexicalHandler ; 72 73 import javax.xml.parsers.SAXParser ; 74 import javax.xml.parsers.SAXParserFactory ; 75 76 77 85 public class XmlDocTypeFilter extends org.xml.sax.helpers.DefaultHandler 86 implements ErrorHandler , LexicalHandler { 87 90 protected boolean result; 91 SAXParser parser = null; 92 String theDocType = null; 93 94 97 public XmlDocTypeFilter() { 98 } 99 100 106 public SAXParser getParser() { 107 if (parser != null) { 109 return (parser); 110 } 111 112 try { 114 SAXParserFactory factory = SAXParserFactory.newInstance(); 115 factory.setNamespaceAware(false); 116 factory.setValidating(false); 117 parser = factory.newSAXParser(); 118 parser.setProperty("http://xml.org/sax/properties/lexical-handler", 119 this); 120 121 return (parser); 122 } catch (Exception e) { 123 LogLog.error("XmlDocTypeFilter.getParser(): ", e); 124 125 return (null); 126 } 127 } 128 129 137 public boolean isProperDocType(String docType, InputSource source) { 138 try { 139 result = false; 140 theDocType = docType; 141 142 SAXParser parser = this.getParser(); 143 parser.parse(source, this); 144 } catch (SAXParseException ex) { 145 return result; 146 } catch (SAXException sax_ex) { 147 return result; 148 } catch (java.io.IOException ioe) { 149 LogLog.error("Io Exception in XmlDocTypeFilter.isProperDoctype", ioe); 150 151 return result; 152 } 153 154 return result; 155 } 156 157 162 public void comment(char[] ch, int start, int length) 163 throws SAXException { 164 } 165 166 169 public void endCDATA() throws SAXException { 170 } 171 172 public void endDTD() throws SAXException { 176 } 177 178 181 public void endEntity(String name) throws SAXException { 182 } 183 184 189 public void error(SAXParseException ex) throws SAXException { 190 LogLog.error(getLocationString(ex) + ": " + ex.getMessage()); 191 } 192 193 198 public void fatalError(SAXParseException ex) throws SAXException { 199 LogLog.error(getLocationString(ex) + ": " + ex.getMessage()); 200 throw ex; 201 } 202 203 206 public void startCDATA() throws SAXException { 207 } 208 209 public void startDTD(String name, String publicId, String systemId) 213 throws SAXException { 214 if (name.equals(theDocType)) { 215 result = true; 216 } 217 218 throw new SAXException ("Done"); 219 } 220 221 224 public void startEntity(String name) throws SAXException { 225 } 226 227 231 236 public void warning(SAXParseException ex) { 237 LogLog.debug(getLocationString(ex) + ": " + ex.getMessage()); 238 } 239 240 246 private String getLocationString(SAXParseException ex) { 247 StringBuffer str = new StringBuffer (); 248 String systemId = ex.getSystemId(); 249 250 if (systemId != null) { 251 int index = systemId.lastIndexOf('/'); 252 253 if (index != -1) { 254 systemId = systemId.substring(index + 1); 255 } 256 257 str.append(systemId); 258 } 259 260 str.append(':'); 261 str.append(ex.getLineNumber()); 262 str.append(':'); 263 str.append(ex.getColumnNumber()); 264 265 return str.toString(); 266 } 267 } 269 | Popular Tags |