@Override public void write(OutputStream outputStream) throws IOException { XmlWriter xmlWriter = new XmlWriter(outputStream, (getCharacterSet() == null) ? "UTF-8" : getCharacterSet() .toString()); write(xmlWriter); xmlWriter.flush(); }
/** * Writes the representation to a XML writer. The default implementation * calls {@link #parse(ContentHandler)} using the {@link XmlWriter} * parameter as the content handler. This behavior is intended to be * overridden. * * @param writer * The XML writer to write to. * @throws IOException */ public void write(XmlWriter writer) throws IOException { parse(writer); } }
@Override public InputSource getInputSource() throws IOException { return (getSaxSource() == null) ? null : getSaxSource() .getInputSource(); }
try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(isNamespaceAware()); spf.setValidating(isValidatingDtd()); javax.xml.validation.Schema xsd = getSchema(); spf.setXIncludeAware(isXIncludeAware()); spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, isSecureProcessing()); spf.setFeature( "http://xml.org/sax/features/external-general-entities", isExpandingEntityRefs()); spf.setFeature( "http://xml.org/sax/features/external-parameter-entities", isExpandingEntityRefs()); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); this.source = new SAXSource(xmlReader, new InputSource(
@SuppressWarnings("unchecked") @Override public <T> T toObject(Representation source, Class<T> target, Resource resource) throws IOException { Object result = null; if (target != null) { if (Document.class.isAssignableFrom(target)) { if (source instanceof DomRepresentation) { result = ((DomRepresentation) source).getDocument(); } else { result = new DomRepresentation(source).getDocument(); } } else if (DomRepresentation.class.isAssignableFrom(target)) { if (source instanceof DomRepresentation) { result = (DomRepresentation) source; } else { result = new DomRepresentation(source); } } else if (SaxRepresentation.class.isAssignableFrom(target)) { if (source instanceof SaxRepresentation) { result = (SaxRepresentation) source; } else { result = new SaxRepresentation(source); } } } return (T) result; }
@Override public void write(Writer writer) throws IOException { XmlWriter xmlWriter = new XmlWriter(writer); write(xmlWriter); }
/** * Returns the SAX source that can be parsed by the * {@link #parse(ContentHandler)} method or used for an XSLT transformation. */ @Override public SAXSource getSaxSource() throws IOException { if (this.source == null) { return new SAXSource(new InputSource(getStream())); } else { return this.source; } }
@Override public Object evaluate(String expression, QName returnType) throws Exception { Object result = null; final XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(this); if (this.source != null) { final Document document = getDocumentBuilder().parse( SAXSource.sourceToInputSource(this.source)); result = xpath.evaluate(expression, document, returnType); } else { throw new Exception( "Unable to obtain a DOM document for the SAX representation. " + "XPath evaluation cancelled."); } return result; }
@SuppressWarnings("unchecked") @Override public <T> T toObject(Representation source, Class<T> target, Resource resource) throws IOException { Object result = null; if (target != null) { if (Document.class.isAssignableFrom(target)) { if (source instanceof DomRepresentation) { result = ((DomRepresentation) source).getDocument(); } else { result = new DomRepresentation(source).getDocument(); } } else if (DomRepresentation.class.isAssignableFrom(target)) { if (source instanceof DomRepresentation) { result = (DomRepresentation) source; } else { result = new DomRepresentation(source); } } else if (SaxRepresentation.class.isAssignableFrom(target)) { if (source instanceof SaxRepresentation) { result = (SaxRepresentation) source; } else { result = new SaxRepresentation(source); } } } return (T) result; }
@Override public void write(Writer writer) throws IOException { XmlWriter xmlWriter = new XmlWriter(writer); write(xmlWriter); }
@Override public void write(OutputStream outputStream) throws IOException { XmlWriter xmlWriter = new XmlWriter(outputStream, (getCharacterSet() == null) ? "UTF-8" : getCharacterSet() .toString()); write(xmlWriter); xmlWriter.flush(); }
} else if (SaxRepresentation.class .isAssignableFrom(targetClass)) { result = new SaxRepresentation(representation);
@Put public void store(SaxRepresentation mailRep) throws IOException { mailRep.parse(new DefaultHandler() {
/** * Parses the source and sends SAX events to a content handler. * * @param contentHandler * The SAX content handler to use for parsing. */ public void parse(ContentHandler contentHandler) throws IOException { if (contentHandler != null) { try { Result result = new SAXResult(contentHandler); TransformerFactory.newInstance().newTransformer() .transform(getSaxSource(), result); } catch (TransformerConfigurationException tce) { throw new IOException( "Couldn't parse the source representation: " + tce.getMessage(), tce); } catch (TransformerException te) { throw new IOException( "Couldn't parse the source representation: " + te.getMessage(), te); } catch (TransformerFactoryConfigurationError tfce) { throw new IOException( "Couldn't parse the source representation: " + tfce.getMessage(), tfce); } } else { throw new IOException( "Couldn't parse the source representation: no content restlet defined."); } }