/** * 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()); }
/** * Returns a DOM source. * * @return A DOM source. */ @Override public DOMSource getDomSource() throws IOException { return new DOMSource(getDocument()); }
@Override public Object evaluate(String expression, QName returnType) throws Exception { final XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(this); return xpath.evaluate(expression, getDocument(), returnType); }
/** * 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) { } }
@Put public void store(DomRepresentation mailRep) throws IOException { // Parses and normalizes the DOM document Document doc = mailRep.getDocument(); Element mailElt = doc.getDocumentElement(); Element statusElt = (Element) mailElt.getElementsByTagName("status") .item(0); Element subjectElt = (Element) mailElt.getElementsByTagName("subject") .item(0); Element contentElt = (Element) mailElt.getElementsByTagName("content") .item(0); Element accountRefElt = (Element) mailElt.getElementsByTagName( "accountRef").item(0); // Output the XML element values System.out.println("Status: " + statusElt.getTextContent()); System.out.println("Subject: " + subjectElt.getTextContent()); System.out.println("Content: " + contentElt.getTextContent()); System.out.println("Account URI: " + accountRefElt.getTextContent()); } }
@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()); } } }
@Override public void write(Writer writer) throws IOException { if (Edition.CURRENT == Edition.ANDROID) { throw new UnsupportedOperationException( "Instances of DomRepresentation cannot be written at this time."); } try { if (getDocument() != null) { final javax.xml.transform.Transformer transformer = createTransformer(); transformer.transform(new javax.xml.transform.dom.DOMSource( getDocument()), new javax.xml.transform.stream.StreamResult(writer)); } } catch (javax.xml.transform.TransformerConfigurationException tce) { throw new IOException("Couldn't write the XML representation: " + tce.getMessage()); } catch (javax.xml.transform.TransformerException te) { throw new IOException("Couldn't write the XML representation: " + te.getMessage()); } catch (javax.xml.transform.TransformerFactoryConfigurationError tfce) { throw new IOException("Couldn't write the XML representation: " + tfce.getMessage()); } } }
@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; }
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());
@Get("xml") public Representation toXml() { try { DomRepresentation representation = new DomRepresentation( MediaType.TEXT_XML); // Generate a DOM document representing the item. Document d = representation.getDocument(); Element eltItem = d.createElement("item"); d.appendChild(eltItem); Element eltName = d.createElement("name"); eltName.appendChild(d.createTextNode(item.getName())); eltItem.appendChild(eltName); Element eltDescription = d.createElement("description"); eltDescription.appendChild(d.createTextNode(item.getDescription())); eltItem.appendChild(eltDescription); d.normalizeDocument(); // Returns the XML representation of this document. return representation; } catch (IOException e) { e.printStackTrace(); } return null; } }
@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; }
@Get("xml") public Representation toXml() { try { DomRepresentation representation = new DomRepresentation( MediaType.TEXT_XML); // Generate a DOM document representing the item. Document d = representation.getDocument(); Element eltItem = d.createElement("item"); d.appendChild(eltItem); Element eltName = d.createElement("name"); eltName.appendChild(d.createTextNode(item.getName())); eltItem.appendChild(eltName); Element eltDescription = d.createElement("description"); eltDescription.appendChild(d.createTextNode(item.getDescription())); eltItem.appendChild(eltDescription); d.normalizeDocument(); // Returns the XML representation of this document. return representation; } catch (IOException e) { e.printStackTrace(); } return null; } }
Document d = representation.getDocument(); Element r = d.createElement("items"); d.appendChild(r);
Document d = representation.getDocument(); Element r = d.createElement("items"); d.appendChild(r);
/** * TODO Provide a "json browser" rendering by default */ protected Representation representHtml() { try { DomRepresentation representation = new DomRepresentation( MediaType.TEXT_HTML ); Document d = representation.getDocument(); Element html = d.createElement( "html" ); Element body = d.createElement( "body" ); Element title = d.createElement( "h1" ); Element json = d.createElement( "pre" ); title.setTextContent( getReference().toString() ); json.setTextContent( new JSONObject( representJson().getText() ).toString( 2 ) ); body.appendChild( title ); body.appendChild( json ); html.appendChild( body ); d.appendChild( html ); d.normalizeDocument(); return representation; } catch ( JSONException ex ) { throw new ResourceException( ex ); } catch ( IOException ex ) { throw new ResourceException( ex ); } }
@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(); // 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; }
@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; }
Document doc = rmepMail.getDocument();