List of usage examples for org.xml.sax SAXParseException SAXParseException
public SAXParseException(String message, String publicId, String systemId, int lineNumber, int columnNumber, Exception e)
From source file:nu.validator.xml.PrudentHttpEntityResolver.java
/** * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, * java.lang.String)//from w w w.j a v a2 s .c om */ public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { if (requestsLeft > -1) { if (requestsLeft == 0) { throw new IOException("Number of permitted HTTP requests exceeded."); } else { requestsLeft--; } } GetMethod m = null; try { IRI iri; try { iri = iriFactory.construct(systemId); } catch (IRIException e) { IOException ioe = (IOException) new IOException(e.getMessage()).initCause(e); SAXParseException spe = new SAXParseException(e.getMessage(), publicId, systemId, -1, -1, ioe); if (errorHandler != null) { errorHandler.fatalError(spe); } throw spe; } if (!iri.isAbsolute()) { SAXParseException spe = new SAXParseException("Not an absolute URI.", publicId, systemId, -1, -1, new IOException("Not an absolute URI.")); if (errorHandler != null) { errorHandler.fatalError(spe); } throw spe; } String scheme = iri.getScheme(); if (!("http".equals(scheme) || "https".equals(scheme))) { String msg = "Unsupported URI scheme: \u201C" + scheme + "\u201D."; SAXParseException spe = new SAXParseException(msg, publicId, systemId, -1, -1, new IOException(msg)); if (errorHandler != null) { errorHandler.fatalError(spe); } throw spe; } try { systemId = iri.toASCIIString(); } catch (MalformedURLException e) { IOException ioe = (IOException) new IOException(e.getMessage()).initCause(e); SAXParseException spe = new SAXParseException(e.getMessage(), publicId, systemId, -1, -1, ioe); if (errorHandler != null) { errorHandler.fatalError(spe); } throw spe; } try { m = new GetMethod(systemId); } catch (IllegalArgumentException e) { SAXParseException spe = new SAXParseException(e.getMessage(), publicId, systemId, -1, -1, (IOException) new IOException(e.getMessage()).initCause(e)); if (errorHandler != null) { errorHandler.fatalError(spe); } throw spe; } m.setFollowRedirects(true); m.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); m.addRequestHeader("Accept", buildAccept()); m.addRequestHeader("Accept-Encoding", "gzip"); log4j.info(systemId); client.executeMethod(m); int statusCode = m.getStatusCode(); if (statusCode != 200) { String msg = "HTTP resource not retrievable. The HTTP status from the remote server was: " + statusCode + "."; SAXParseException spe = new SAXParseException(msg, publicId, m.getURI().toString(), -1, -1, new IOException(msg)); if (errorHandler != null) { errorHandler.fatalError(spe); } throw spe; } long len = m.getResponseContentLength(); if (sizeLimit > -1 && len > sizeLimit) { SAXParseException spe = new SAXParseException("Resource size exceeds limit.", publicId, m.getURI().toString(), -1, -1, new StreamBoundException("Resource size exceeds limit.")); if (errorHandler != null) { errorHandler.fatalError(spe); } throw spe; } TypedInputSource is; Header ct = m.getResponseHeader("Content-Type"); String contentType = null; final String baseUri = m.getURI().toString(); if (ct != null) { contentType = ct.getValue(); } is = contentTypeParser.buildTypedInputSource(baseUri, publicId, contentType); Header cl = m.getResponseHeader("Content-Language"); if (cl != null) { is.setLanguage(cl.getValue().trim()); } Header xuac = m.getResponseHeader("X-UA-Compatible"); if (xuac != null) { SAXParseException spe = new SAXParseException("X-UA-Compatible is a browser-specific HTTP header.", publicId, systemId, -1, -1); errorHandler.warning(spe); } final GetMethod meth = m; InputStream stream = m.getResponseBodyAsStream(); if (sizeLimit > -1) { stream = new BoundedInputStream(stream, sizeLimit, baseUri); } Header ce = m.getResponseHeader("Content-Encoding"); if (ce != null) { String val = ce.getValue().trim(); if ("gzip".equalsIgnoreCase(val) || "x-gzip".equalsIgnoreCase(val)) { stream = new GZIPInputStream(stream); if (sizeLimit > -1) { stream = new BoundedInputStream(stream, sizeLimit, baseUri); } } } is.setByteStream(new ObservableInputStream(stream, new StreamObserver() { private final Logger log4j = Logger .getLogger("nu.validator.xml.PrudentEntityResolver.StreamObserver"); private boolean released = false; public void closeCalled() { log4j.debug("closeCalled"); if (!released) { log4j.debug("closeCalled, not yet released"); released = true; try { meth.releaseConnection(); } catch (Exception e) { log4j.debug("closeCalled, releaseConnection", e); } } } public void exceptionOccurred(Exception ex) throws IOException { if (!released) { released = true; try { meth.abort(); } catch (Exception e) { log4j.debug("exceptionOccurred, abort", e); } finally { try { meth.releaseConnection(); } catch (Exception e) { log4j.debug("exceptionOccurred, releaseConnection", e); } } } if (ex instanceof SystemIdIOException) { SystemIdIOException siie = (SystemIdIOException) ex; throw siie; } else if (ex instanceof IOException) { IOException ioe = (IOException) ex; throw new SystemIdIOException(baseUri, ioe.getMessage(), ioe); } else if (ex instanceof RuntimeException) { RuntimeException re = (RuntimeException) ex; throw re; } else { throw new RuntimeException("API contract violation. Wrong exception type.", ex); } } public void finalizerCalled() { if (!released) { released = true; try { meth.abort(); } catch (Exception e) { log4j.debug("finalizerCalled, abort", e); } finally { try { meth.releaseConnection(); } catch (Exception e) { log4j.debug("finalizerCalled, releaseConnection", e); } } } } })); return is; } catch (IOException e) { if (m != null) { try { m.abort(); } catch (Exception ex) { log4j.debug("abort", ex); } finally { try { m.releaseConnection(); } catch (Exception ex) { log4j.debug("releaseConnection", ex); } } } throw e; } catch (SAXException e) { if (m != null) { try { m.abort(); } catch (Exception ex) { log4j.debug("abort", ex); } finally { try { m.releaseConnection(); } catch (Exception ex) { log4j.debug("releaseConnection", ex); } } } throw e; } catch (RuntimeException e) { if (m != null) { try { m.abort(); } catch (Exception ex) { log4j.debug("abort", ex); } finally { try { m.releaseConnection(); } catch (Exception ex) { log4j.debug("releaseConnection", ex); } } } throw e; } }
From source file:org.apache.cocoon.generation.VelocityGenerator.java
/** * Generate XML data using Velocity template. * * @see org.apache.cocoon.generation.Generator#generate() *//*from ww w . java 2s . com*/ public void generate() throws IOException, SAXException, ProcessingException { // Guard against calling generate before setup. if (!activeFlag) { throw new IllegalStateException("generate called on sitemap component before setup."); } SAXParser parser = null; StringWriter w = new StringWriter(); try { parser = (SAXParser) this.manager.lookup(SAXParser.ROLE); if (getLogger().isDebugEnabled()) { getLogger().debug("Processing File: " + super.source); } if (!tmplEngineInitialized) { tmplEngine.init(); tmplEngineInitialized = true; } /* lets render a template */ this.tmplEngine.mergeTemplate(super.source, velocityContext, w); InputSource xmlInput = new InputSource(new StringReader(w.toString())); xmlInput.setSystemId(super.source); parser.parse(xmlInput, this.xmlConsumer); } catch (IOException e) { getLogger().warn("VelocityGenerator.generate()", e); throw new ResourceNotFoundException("Could not get Resource for VelocityGenerator", e); } catch (SAXParseException e) { int line = e.getLineNumber(); int column = e.getColumnNumber(); if (line <= 0) { line = Integer.MAX_VALUE; } BufferedReader reader = new BufferedReader(new StringReader(w.toString())); StringBuffer message = new StringBuffer(e.getMessage()); message.append(" In generated document:\n"); for (int i = 0; i < line; i++) { String lineStr = reader.readLine(); if (lineStr == null) { break; } message.append(lineStr); message.append("\n"); } if (column > 0) { message.append(StringUtils.leftPad("^\n", column + 1)); } SAXException pe = new SAXParseException(message.toString(), e.getPublicId(), "(Document generated from template " + e.getSystemId() + ")", e.getLineNumber(), e.getColumnNumber(), null); getLogger().error("VelocityGenerator.generate()", pe); throw pe; } catch (SAXException e) { getLogger().error("VelocityGenerator.generate()", e); throw e; } catch (ServiceException e) { getLogger().error("Could not get parser", e); throw new ProcessingException("Exception in VelocityGenerator.generate()", e); } catch (ProcessingException e) { throw e; } catch (Exception e) { getLogger().error("Could not get parser", e); throw new ProcessingException("Exception in VelocityGenerator.generate()", e); } finally { this.manager.release(parser); } }
From source file:org.testdwr.plain.Test.java
public void throwSPE() throws SAXParseException { // This is exported by dwr.xml as a result of it being a SAXException throw new SAXParseException("No message for SPE", "publicId", "systemId", 42, 24, new NullPointerException("No message for NPE")); }
From source file:org.testdwr.plain.Test.java
public void throwSPE(String messageSpe, String messageNpe) throws SAXParseException { // This is exported by dwr.xml as a result of it being a SAXException throw new SAXParseException(messageSpe, "publicId", "systemId", 42, 24, new NullPointerException(messageNpe)); }