/** * Sets the content of that element as text. * * @param textContent * The content of that element as text. */ public void setTextContent(String textContent) { try { Document doc = new DomRepresentation(MediaType.TEXT_XML) .getDocument(); this.mixedContent = doc.createTextNode(textContent); } catch (IOException e) { } }
/** * Returns the wrapped DOM document. If no document is defined yet, it * attempts to parse the XML representation eventually given at construction * time. Otherwise, it just creates a new document. * * @return The wrapped DOM document. */ @Override public Document getDocument() throws IOException { if (this.document == null) { if (this.xmlRepresentation != null) { try { this.document = getDocumentBuilder().parse(getInputSource()); } catch (SAXException se) { throw new IOException("Couldn't read the XML representation. " + se.getMessage()); } } else { this.document = getDocumentBuilder().newDocument(); } } return this.document; }
@Override public void write(OutputStream outputStream) throws IOException { try { if (getDocument() != null) { final Transformer transformer = createTransformer(); transformer.transform(new DOMSource(getDocument()), new StreamResult(outputStream)); } } catch (TransformerConfigurationException tce) { throw new IOException("Couldn't write the XML representation: " + tce.getMessage()); } catch (TransformerException te) { throw new IOException("Couldn't write the XML representation: " + te.getMessage()); } catch (TransformerFactoryConfigurationError tfce) { throw new IOException("Couldn't write the XML representation: " + tfce.getMessage()); } } }
@Get public Representation toXml() throws IOException { // Create a new DOM representation DomRepresentation result = new DomRepresentation(); // Ensure pretty printing result.setIndenting(true); // Retrieve the DOM document to populate Document doc = result.getDocument(); // Append the root node Node mailElt = doc.createElement("mail"); doc.appendChild(mailElt); // Append the child nodes and set their text content Node statusElt = doc.createElement("status"); statusElt.setTextContent("received"); mailElt.appendChild(statusElt); Node subjectElt = doc.createElement("subject"); subjectElt.setTextContent("Message to self"); mailElt.appendChild(subjectElt); Node contentElt = doc.createElement("content"); contentElt.setTextContent("Doh!"); mailElt.appendChild(contentElt); Node accountRefElt = doc.createElement("accountRef"); accountRefElt.setTextContent(new Reference(getReference(), "..") .getTargetRef().toString()); mailElt.appendChild(accountRefElt); return result; }
@Get public Representation toXml() throws IOException { // Create a new DOM representation DomRepresentation result = new DomRepresentation(); result.setIndenting(true); // XML namespace configuration result.setNamespaceAware(true); // Populate the DOM document Document doc = result.getDocument(); Node mailElt = doc.createElementNS( "http://www.rmep.org/namespaces/1.0", "mail"); doc.appendChild(mailElt); Node statusElt = doc.createElement("status"); statusElt.setTextContent("received"); mailElt.appendChild(statusElt); Node subjectElt = doc.createElement("subject"); subjectElt.setTextContent("Message to self"); mailElt.appendChild(subjectElt); Node contentElt = doc.createElement("content"); contentElt.setTextContent("Doh!"); mailElt.appendChild(contentElt); Node accountRefElt = doc.createElement("accountRef"); accountRefElt.setTextContent(new Reference(getReference(), "..") .getTargetRef().toString()); mailElt.appendChild(accountRefElt); return result; }
/** * Returns a DOM source. * * @return A DOM source. */ @Override public javax.xml.transform.dom.DOMSource getDomSource() throws IOException { return new javax.xml.transform.dom.DOMSource(getDocument()); }
transformer.setOutputProperty( javax.xml.transform.OutputKeys.INDENT, isIndenting() ? "yes" : "no"); if (getCharacterSet() != null) { transformer.setOutputProperty( javax.xml.transform.OutputKeys.ENCODING, getCharacterSet().getName()); } else { transformer.setOutputProperty( DocumentType docType = getDocument().getDoctype(); transformer.setOutputProperty( javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM, getDocument().getDoctype().getSystemId()); transformer.setOutputProperty( javax.xml.transform.OutputKeys.DOCTYPE_PUBLIC, getDocument().getDoctype().getPublicId());
@Override public Representation toRepresentation(Object source, Variant target, Resource resource) throws IOException { Representation result = null; if (source instanceof Document) { result = new DomRepresentation(target.getMediaType(), (Document) source); } else if (source instanceof Representation) { result = (Representation) source; } return result; }
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); DocumentType docType = getDocument().getDoctype(); if (docType != null) { if (docType.getSystemId() != null) { transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, getDocument().getDoctype().getSystemId()); getDocument().getDoctype().getPublicId()); .toString(isIndent()));
/** * Constructor for an empty document. * * @param mediaType * The representation's media type. */ public DomRepresentation(MediaType mediaType) throws IOException { super(mediaType); this.document = getDocumentBuilder().newDocument(); }
/** * Releases the wrapped DOM document and the source XML representation if * they have been defined. */ @Override public void release() { setDocument(null); if (this.xmlRepresentation != null) { this.xmlRepresentation.release(); } super.release(); }
/** * Constructor. * * @param xmlRepresentation * A source XML representation to parse. */ public DomRepresentation(Representation xmlRepresentation) { super((xmlRepresentation == null) ? null : xmlRepresentation .getMediaType()); this.setAvailable(xmlRepresentation.isAvailable()); this.xmlRepresentation = xmlRepresentation; }
@Get public Representation toXml() throws IOException { // Create an empty DOM representation DomRepresentation result = new DomRepresentation(); // Ensure pretty printing result.setIndenting(true); // Retrieve the DOM document to populate Document doc = result.getDocument(); // Append the root node Node mailElt = doc.createElement("mail"); doc.appendChild(mailElt); // Append the child nodes and set their text content Node statusElt = doc.createElement("status"); statusElt.setTextContent("received"); mailElt.appendChild(statusElt); Node subjectElt = doc.createElement("subject"); subjectElt.setTextContent("Message to self"); mailElt.appendChild(subjectElt); Node contentElt = doc.createElement("content"); contentElt.setTextContent("Doh!"); mailElt.appendChild(contentElt); Node accountRefElt = doc.createElement("accountRef"); // Computes the parent URI including slash accountRefElt.setTextContent(new Reference(getReference(), "..") .getTargetRef().toString()); mailElt.appendChild(accountRefElt); return result; }
@Get public Representation toXml() throws IOException { // Create a new DOM representation DomRepresentation result = new DomRepresentation(); result.setIndenting(true); // XML namespace configuration result.setNamespaceAware(true); // Populate the DOM document Document doc = result.getDocument(); Node mailElt = doc.createElementNS( "http://www.rmep.org/namespaces/1.0", "mail"); doc.appendChild(mailElt); Node statusElt = doc.createElement("status"); statusElt.setTextContent("received"); mailElt.appendChild(statusElt); Node subjectElt = doc.createElement("subject"); subjectElt.setTextContent("Message to self"); mailElt.appendChild(subjectElt); Node contentElt = doc.createElement("content"); contentElt.setTextContent("Doh!"); mailElt.appendChild(contentElt); Node accountRefElt = doc.createElement("accountRef"); accountRefElt.setTextContent(new Reference(getReference(), "..") .getTargetRef().toString()); mailElt.appendChild(accountRefElt); return result; }
/** * Returns a DOM source. * * @return A DOM source. */ @Override public DOMSource getDomSource() throws IOException { return new DOMSource(getDocument()); }
@Override public Representation toRepresentation(Object source, Variant target, Resource resource) throws IOException { Representation result = null; if (source instanceof Document) { result = new DomRepresentation(target.getMediaType(), (Document) source); } else if (source instanceof Representation) { result = (Representation) source; } return result; }
/** * Constructor for an empty document. * * @param mediaType * The representation's media type. */ public DomRepresentation(MediaType mediaType) throws IOException { super(mediaType); this.dom = getDocumentBuilder().newDocument(); }
/** * Releases the wrapped DOM document and the source XML representation if * they have been defined. */ @Override public void release() { setDocument(null); if (this.xmlRepresentation != null) { this.xmlRepresentation.release(); } super.release(); }
/** * Constructor. * * @param xmlRepresentation * A source XML representation to parse. */ public DomRepresentation(Representation xmlRepresentation) { super((xmlRepresentation == null) ? null : xmlRepresentation.getMediaType()); this.setAvailable((xmlRepresentation == null) ? false : xmlRepresentation.isAvailable()); this.xmlRepresentation = xmlRepresentation; }
@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; }