private void marshalDocument(IMarshallingContext marshallingContext, Object graph) throws IOException, JiBXException { if (StringUtils.hasLength(this.docTypeRootElementName)) { IXMLWriter xmlWriter = marshallingContext.getXmlWriter(); xmlWriter.writeDocType(this.docTypeRootElementName, this.docTypeSystemId, this.docTypePublicId, this.docTypeInternalSubset); } marshallingContext.marshalDocument(graph); }
/** * Create a new {@code IMarshallingContext}, configured with the correct indentation. * @return the created marshalling context * @throws JiBXException in case of errors */ protected IMarshallingContext createMarshallingContext() throws JiBXException { Assert.state(this.bindingFactory != null, "JibxMarshaller not initialized"); IMarshallingContext marshallingContext = this.bindingFactory.createMarshallingContext(); marshallingContext.setIndent(this.indent); return marshallingContext; }
private String encode2Xml(Order order) throws JiBXException, IOException { factory = BindingDirectory.getFactory(Order.class); writer = new StringWriter(); IMarshallingContext mctx = factory.createMarshallingContext(); mctx.setIndent(2); mctx.marshalDocument(order, CHARSET_NAME, null, writer); String xmlStr = writer.toString(); writer.close(); System.out.println(xmlStr.toString()); return xmlStr; }
public void marshal(Object obj, IMarshallingContext ictx) throws JiBXException { // make sure the parameters are as expected if (!(obj instanceof List)) { throw new JiBXException("Mapped object not a java.util.List"); } else { try { // marshal all content with no indentation m_xmlWriter = ictx.getXmlWriter(); int indent = ictx.getIndent(); ictx.setIndent(-1); m_defaultNamespaceURI = null; marshalContent((List)obj); ictx.setIndent(indent); } catch (IOException e) { throw new JiBXException("Error writing to document", e); } } }
public class MyClass{ private String name; private IBindingFactory bindingFactory; public MyClass(String name){ this(name, BindingDirectory.getFactory(PROTECTEDCLASSNAME.class)); } MyClass(String name, IBindingFactory bindingFactory){ this.name = name; this.bindingFactory = bindingFactory; } public String marshalRequest() { StringWriter requestAsXml = new StringWriter(); try { IMarshallingContext marshalingContext = bindingFactory.createMarshallingContext(); marshalingContext.setIndent(2); marshalingContext.setOutput(requestAsXml); marshalingContext.marshalDocument(request); } catch (JiBXException e) { logger.error("Problem marshaling PROTECTEDCLASSNAME.", e); } return requestAsXml.toString(); } }
@Override protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException { try { IMarshallingContext marshallingContext = createMarshallingContext(); marshallingContext.startDocument(this.encoding, this.standalone, writer); marshalDocument(marshallingContext, graph); } catch (JiBXException ex) { throw convertJibxException(ex, true); } }
/** * Marshal this message to DOM. * @param message * @return DOM tree */ public static Element marshalObjectToDOM(Object message) { try { IBindingFactory jc = BindingDirectory.getFactory(message.getClass()); IMarshallingContext marshaller = jc.createMarshallingContext(); String[] namespaces = jc.getNamespaces(); JDOMWriter jdomWriter = new JDOMWriter(namespaces); marshaller.setXmlWriter(jdomWriter); marshaller.marshalDocument(message); marshaller.endDocument(); org.jdom.Document jdoc = jdomWriter.getDocument(); DOMOutputter dout = new DOMOutputter(); Document doc = dout.output(jdoc); Element node = doc.getDocumentElement(); return node; } catch (JiBXException e) { e.printStackTrace(); } catch (JDOMException e) { e.printStackTrace(); } return null; } /**
private RawSld parseSldI(StyledLayerDescriptorInfo sld) throws JiBXException { RawSld res = new RawSld(); IBindingFactory bfact; bfact = BindingDirectory.getFactory(StyledLayerDescriptorInfo.class); IMarshallingContext mctx = bfact.createMarshallingContext(); StringWriter writer = new StringWriter(); mctx.setOutput(writer); mctx.getXmlWriter().setIndentSpaces(2, "\n", ' '); mctx.marshalDocument(sld); res.setXml(writer.toString()); res.setName(sld.getName()); res.setVersion(sld.getVersion()); res.setTitle(sld.getTitle() == null ? getTitle(sld, "?") : sld.getTitle()); return res; }
/** * Write a collection of schemas to a target directory. * * @param dir target directory * @param schemas list of {@link SchemaHolder} * @throws JiBXException * @throws IOException */ public static void writeSchemas(File dir, Collection schemas) throws JiBXException, IOException { IBindingFactory fact = BindingDirectory.getFactory(SchemaUtils.XS_PREFIX_BINDING, SchemaElement.class); for (Iterator iter = schemas.iterator(); iter.hasNext();) { SchemaHolder holder = (SchemaHolder)iter.next(); if (!holder.isExistingFile()) { IMarshallingContext ictx = fact.createMarshallingContext(); File file = new File(dir, holder.getFileName()); ictx.setOutput(new FileOutputStream(file), null); ictx.setIndent(2); ((IMarshallable)holder.getSchema()).marshal(ictx); ictx.getXmlWriter().flush(); } } }
private void marshalDocument(IMarshallingContext marshallingContext, Object graph) throws IOException, JiBXException { if (StringUtils.hasLength(this.docTypeRootElementName)) { IXMLWriter xmlWriter = marshallingContext.getXmlWriter(); xmlWriter.writeDocType(this.docTypeRootElementName, this.docTypeSystemId, this.docTypePublicId, this.docTypeInternalSubset); } marshallingContext.marshalDocument(graph); }
public void write(Object obj, XMLStreamWriter output) { try { IBindingFactory factory = BindingDirectory.getFactory(obj.getClass()); IMarshallingContext ctx = getMarshallingContext(obj); StAXWriter writer = new StAXWriter(factory.getNamespaces(), output); ctx.setXmlWriter(writer); ((IMarshallable)obj).marshal(ctx); } catch (Exception e) { throw new RuntimeException(e); } }
IXMLWriter wrtr = ctx.getXmlWriter(); String name = elementName; int nsidx = 0; IMarshaller mrsh = ctx.getMarshaller(marshallerName); mrsh.marshal(dataObject, ctx); wrtr.endTag(nsidx, name); ctx.getXmlWriter().flush();
protected ByteBuf encode0(ChannelHandlerContext ctx, Object body) throws Exception { factory = BindingDirectory.getFactory(body.getClass()); writer = new StringWriter(); IMarshallingContext mctx = factory.createMarshallingContext(); mctx.setIndent(2); mctx.marshalDocument(body, CHARSET_NAME, null, writer); String xmlStr = writer.toString(); writer.close(); writer = null; ByteBuf encodeBuf = Unpooled.copiedBuffer(xmlStr, UTF_8); return encodeBuf; }
public void marshal(Object obj, IMarshallingContext ictx) throws JiBXException { // make sure the parameters are as expected if (!(obj instanceof Element)) { throw new JiBXException("Mapped object not an org.w3c.dom.Element"); } else { try { // marshal element and all content with only leading indentation m_xmlWriter = ictx.getXmlWriter(); m_xmlWriter.indent(); int indent = ictx.getIndent(); ictx.setIndent(-1); m_defaultNamespaceURI = null; marshalElement((Element)obj); ictx.setIndent(indent); } catch (IOException e) { throw new JiBXException("Error writing to document", e); } } }
/** * Write schema definitions to file system. * * @param destdir destination directory * @throws JiBXException on error in marshalling * @throws IOException on error writing */ private void writeSchemas(File destdir) throws JiBXException, IOException { for (Iterator iter = m_validationContext.iterateSchemas(); iter.hasNext();) { SchemaElement schema = (SchemaElement)iter.next(); File file = new File(destdir, schema.getResolver().getName()); OutputStream stream = new FileOutputStream(file); Writer writer = new OutputStreamWriter(stream, "utf-8"); IBindingFactory factory = BindingDirectory.getFactory(SchemaUtils.XS_PREFIX_BINDING, SchemaElement.class); IMarshallingContext ictx = factory.createMarshallingContext(); ictx.setOutput(writer); ictx.setIndent(2); ictx.marshalDocument(schema); writer.close(); } }
public void marshal(Exchange exchange, Object body, OutputStream stream) throws Exception { IBindingFactory bindingFactory = createBindingFactory(body.getClass(), bindingName); IMarshallingContext marshallingContext = bindingFactory.createMarshallingContext(); marshallingContext.marshalDocument(body, null, null, stream); if (contentTypeHeader) { if (exchange.hasOut()) { exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/xml"); } else { exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/xml"); } } }
/** * Marshal this message to DOM. * @param message * @return DOM tree */ public static Element marshalObjectToDOM(Object message) { try { IBindingFactory jc = BindingDirectory.getFactory(message.getClass()); IMarshallingContext marshaller = jc.createMarshallingContext(); String[] namespaces = jc.getNamespaces(); JDOMWriter jdomWriter = new JDOMWriter(namespaces); marshaller.setXmlWriter(jdomWriter); marshaller.marshalDocument(message); marshaller.endDocument(); org.jdom.Document jdoc = jdomWriter.getDocument(); DOMOutputter dout = new DOMOutputter(); Document doc = dout.output(jdoc); Element node = doc.getDocumentElement(); return node; } catch (JiBXException e) { e.printStackTrace(); } catch (JDOMException e) { e.printStackTrace(); } return null; } /**
IXMLWriter writer = ictx.getXmlWriter(); for (int i = 0; i < m_attributes.size();) { String name = (String)m_attributes.get(i++);
public void write(Object obj, XMLStreamWriter output) { try { IBindingFactory factory = BindingDirectory.getFactory(obj.getClass()); IMarshallingContext ctx = getMarshallingContext(obj); StAXWriter writer = new StAXWriter(factory.getNamespaces(), output); ctx.setXmlWriter(writer); ((IMarshallable)obj).marshal(ctx); } catch (Exception e) { throw new RuntimeException(e); } }
public static byte[] toXML(Object obj) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); IBindingFactory bfact = BindingDirectory.getFactory(obj.getClass()); IMarshallingContext mctx = bfact.createMarshallingContext(); mctx.setIndent(2); mctx.marshalDocument(obj, "UTF-8", null, out); return out.toByteArray(); }