@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; }
@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; }
/** * 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) { } }
/** * Generate an XML representation of an error response. * * @param errorMessage * the error message. * @param errorCode * the error code. */ private Representation generateErrorRepresentation(String errorMessage, String errorCode) { DomRepresentation result = null; // This is an error // Generate the output representation try { result = new DomRepresentation(MediaType.TEXT_XML); // Generate a DOM document representing the list of // items. Document d = result.getDocument(); Element eltError = d.createElement("error"); Element eltCode = d.createElement("code"); eltCode.appendChild(d.createTextNode(errorCode)); eltError.appendChild(eltCode); Element eltMessage = d.createElement("message"); eltMessage.appendChild(d.createTextNode(errorMessage)); eltError.appendChild(eltMessage); } catch (IOException e) { e.printStackTrace(); } return result; }
/** * Generate an XML representation of an error response. * * @param errorMessage * the error message. * @param errorCode * the error code. */ private Representation generateErrorRepresentation(String errorMessage, String errorCode) { DomRepresentation result = null; // This is an error // Generate the output representation try { result = new DomRepresentation(MediaType.TEXT_XML); // Generate a DOM document representing the list of // items. Document d = result.getDocument(); Element eltError = d.createElement("error"); Element eltCode = d.createElement("code"); eltCode.appendChild(d.createTextNode(errorCode)); eltError.appendChild(eltCode); Element eltMessage = d.createElement("message"); eltMessage.appendChild(d.createTextNode(errorMessage)); eltError.appendChild(eltMessage); } catch (IOException e) { e.printStackTrace(); } return result; }
this.mixedContentDocument = new DomRepresentation( MediaType.TEXT_XML).getDocument(); } catch (IOException e) {
@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; } }
@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; } }
DomRepresentation representation = new DomRepresentation( MediaType.TEXT_XML);
DomRepresentation representation = new DomRepresentation( MediaType.TEXT_XML);
/** * 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 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 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; }
@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 { DomRepresentation rmepMail = new DomRepresentation(); rmepMail.setIndenting(true);
} else { if (Document.class.isAssignableFrom(targetClass)) { result = new DomRepresentation(representation) .getDocument(); } else if (DomRepresentation.class .isAssignableFrom(targetClass)) { result = new DomRepresentation(representation); } else if (SaxRepresentation.class .isAssignableFrom(targetClass)) {
@Override public Representation toRepresentation(Object object, Variant targetVariant, UniformResource resource) throws IOException { Representation result = null; if (object instanceof Document) { result = new DomRepresentation(targetVariant == null ? null : targetVariant.getMediaType(), (Document) object); } if ((result != null) && (targetVariant != null)) { // Copy the variant metadata result.setCharacterSet(targetVariant.getCharacterSet()); result.setMediaType(targetVariant.getMediaType()); result.setEncodings(targetVariant.getEncodings()); result.setLanguages(targetVariant.getLanguages()); } return result; } }