/** * PUBLIC: * Convert the given object to XML and update the given contentHandler with that XML Document * @param object the object to marshal * @param contentHandler the contentHandler which the specified object should be marshalled to * @throws XMLMarshalException if an error occurred during marshalling */ public void marshal(Object object, ContentHandler contentHandler) throws XMLMarshalException { marshal(object, contentHandler, null); }
public void serialize(XMLDocument xmlDocument, OutputStream outputStream, Object options) throws IOException { XMLMarshaller xmlMarshaller = getXmlMarshaller(); XMLAttachmentMarshaller attachmentMarshaller = xmlMarshaller.getAttachmentMarshaller(); //temporarily null out the attachment marshaller as it should not be used during serialization xmlMarshaller.setAttachmentMarshaller(null); OutputStreamWriter writer = new OutputStreamWriter(outputStream, xmlMarshaller.getEncoding()); save(xmlDocument, writer, xmlMarshaller); xmlMarshaller.setAttachmentMarshaller(attachmentMarshaller); }
public void setListener(Marshaller.Listener listener) { if(xmlMarshaller.getMarshalListener() == null) { xmlMarshaller.setMarshalListener(new JAXBMarshalListener(jaxbContext, this)); } ((JAXBMarshalListener) xmlMarshaller.getMarshalListener()).setListener(listener); }
/** * Convert the given object to XML and update the given marshal record with * that XML Document. * @param object the object to marshal * @param marshalRecord the marshalRecord to marshal the object to */ protected void marshal(Object object, AbstractSession session, MarshalRecord marshalRecord) { boolean isXMLRoot = (object instanceof Root); marshal(object, marshalRecord, session, getDescriptor(object, isXMLRoot), isXMLRoot); }
throw new IllegalArgumentException(); } else if (JAXB_FORMATTED_OUTPUT.equals(key)) { return xmlMarshaller.isFormattedOutput(); } else if (JAXB_ENCODING.equals(key)) { return xmlMarshaller.getEncoding(); } else if (JAXB_SCHEMA_LOCATION.equals(key)) { return xmlMarshaller.getSchemaLocation(); } else if (JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(key)) { return xmlMarshaller.getNoNamespaceSchemaLocation(); } else if (Constants.JAXB_FRAGMENT.equals(key)) { return xmlMarshaller.isFragment(); } else if (MarshallerProperties.MEDIA_TYPE.equals(key)) { return xmlMarshaller.getMediaType(); } else if (MarshallerProperties.NAMESPACE_PREFIX_MAPPER.equals(key)) { return xmlMarshaller.getNamespacePrefixMapper(); } else if (MarshallerProperties.INDENT_STRING.equals(key) || SUN_INDENT_STRING.equals(key) || SUN_JSE_INDENT_STRING.equals(key)) { return xmlMarshaller.getIndentString(); } else if (MarshallerProperties.CHARACTER_ESCAPE_HANDLER.equals(key)) { return xmlMarshaller.getCharacterEscapeHandler(); } else if (XML_DECLARATION.equals(key)) { return !xmlMarshaller.isFragment(); } else if (XML_HEADERS.equals(key)) { return xmlMarshaller.getXmlHeader(); } else if (OBJECT_IDENTITY_CYCLE_DETECTION.equals(key)) { return xmlMarshaller.isEqualUsingIdenity(); } else if (MarshallerProperties.JSON_ATTRIBUTE_PREFIX.equals(key)) { return xmlMarshaller.getAttributePrefix(); } else if (MarshallerProperties.JSON_INCLUDE_ROOT.equals(key)) { return xmlMarshaller.isIncludeRoot();
session = xmlContext.getSession(((XMLRoot)object).getObject()); if(session != null){ xmlDescriptor = getDescriptor(((XMLRoot)object).getObject(), session); if (!isSimpleXMLRoot((XMLRoot) object)) { throw marshalException; xmlDescriptor = getDescriptor(object.getClass(), session); contentHandlerRecord.setContentHandler(contentHandler); contentHandlerRecord.setLexicalHandler(lexicalHandler); marshal(object, contentHandlerRecord, session, xmlDescriptor,isXMLRoot); return; xmlDocument = objectToXML(object, xmlDescriptor, isXMLRoot); if (isFragment()) { FragmentContentHandler fragmentHandler = new FragmentContentHandler(contentHandler); reader.setContentHandler(fragmentHandler);
anXMLMarshaller.setFragment(!xmlDocument.isXMLDeclaration()); anXMLMarshaller.setEncoding(xmlDocument.getEncoding()); anXMLMarshaller.setSchemaLocation(xmlDocument.getSchemaLocation()); anXMLMarshaller.setNoNamespaceSchemaLocation(xmlDocument.getNoNamespaceSchemaLocation()); if(anXMLMarshaller.isFormattedOutput()) { writerRecord = new FormattedWriterRecord(); } else { writerRecord.setMarshaller(anXMLMarshaller); SDOMarshalListener listener = ((SDOMarshalListener)anXMLMarshaller.getMarshalListener()); anXMLMarshaller.marshal(xmlDocument, writerRecord); writerRecord.flush(); }catch(XMLMarshalException xme){
anXMLMarshaller.setFragment(!xmlDocument.isXMLDeclaration()); anXMLMarshaller.setEncoding(xmlDocument.getEncoding()); anXMLMarshaller.setSchemaLocation(xmlDocument.getSchemaLocation()); anXMLMarshaller.setNoNamespaceSchemaLocation(xmlDocument.getNoNamespaceSchemaLocation()); SDOMarshalListener listener = ((SDOMarshalListener)anXMLMarshaller.getMarshalListener()); marshalRecord.setMarshaller(anXMLMarshaller); listener.setRootMarshalRecord(marshalRecord); anXMLMarshaller.marshal(xmlDocument, marshalRecord); marshalRecord.flush(); } else if(result instanceof DOMResult) { marshalRecord.setMarshaller(anXMLMarshaller); listener.setRootMarshalRecord(marshalRecord); anXMLMarshaller.marshal(xmlDocument, marshalRecord); marshalRecord.flush(); } else { String xml = writer.toString(); StreamSource source = new StreamSource(new java.io.StringReader(xml)); anXMLMarshaller.getTransformer().transform(source, result);
String encoding = getEncoding(); if (object instanceof XMLRoot) { isXMLRoot = true; session = xmlContext.getSession(((XMLRoot)object).getObject()); if(session != null){ xmlDescriptor = getDescriptor(((XMLRoot)object).getObject(), session); if (!isSimpleXMLRoot((XMLRoot) object)) { throw marshalException; xmlDescriptor = getDescriptor(object.getClass(), session); if (isFormattedOutput()) { writerRecord = new FormattedWriterRecord(); } else { marshal(object, writerRecord, session, xmlDescriptor, isXMLRoot); } else { try { xmlDocument = (Node)((XMLRoot)object).getObject(); } else { xmlDocument = objectToXMLNode(object, session, xmlDescriptor, isXMLRoot); if (isFragment()) { writerRecord.node(xmlDocument, xmlDescriptor.getNamespaceResolver()); } else {
if (getAttachmentMarshaller() != null) { marshalRecord.setXOPPackage(getAttachmentMarshaller().isXOPPackage()); addDescriptorNamespacesToXMLRecord(descriptor, marshalRecord); NamespaceResolver nr = marshalRecord.getNamespaceResolver(); XMLRoot root = null; if (getMarshalListener() != null) { getMarshalListener().beforeMarshal(object); if (!isFragment()) { String encoding = getEncoding(); String version = DEFAULT_XML_VERSION; if (!isXMLRoot) { XPathFragment rootFragment = buildRootFragment(object, descriptor, isXMLRoot, marshalRecord); boolean shouldWriteTypeAttribute = shouldWriteTypeAttribute(object, descriptor, isXMLRoot); String schemaLocation = getSchemaLocation(); String noNsSchemaLocation = getNoNamespaceSchemaLocation(); if (isXMLRoot) { object = root.getObject(); if ((null != getSchemaLocation()) || (null != getNoNamespaceSchemaLocation()) || shouldWriteTypeAttribute) { xsiPrefix = nr.resolveNamespaceURI(XMLConstants.SCHEMA_INSTANCE_URL); if (null == xsiPrefix) { writeTypeAttribute(marshalRecord, descriptor, xsiPrefix);
session = context.getSession(((Root)object).getObject()); if(session != null){ xmlDescriptor = getDescriptor(((Root)object).getObject(), session); if (!isSimpleXMLRoot((Root) object)) { throw marshalException; for(Object o : (Collection) object) { if (o == null) { valueWrapper = this.getValueWrapper(); if (isApplicationJSON()) { this.setValueWrapper(""); writerRecord.setMarshaller(this); this.setValueWrapper(valueWrapper); } else { if (isApplicationJSON() && o != null && (o.getClass() == CoreClassConstants.STRING || o.getClass() == CoreClassConstants.BOOLEAN || CoreClassConstants.NUMBER.isAssignableFrom(o.getClass()))) { if (writerRecord.getSession() == null) { writerRecord.setSession((CoreAbstractSession) this.getContext().getSession()); valueWrapper = this.getValueWrapper(); this.setValueWrapper(""); writerRecord.setMarshaller(this); writerRecord.characters(null, o, null, false); this.setValueWrapper(valueWrapper); } else { marshal(o, writerRecord); return;
@Override public Object getProperty(String propName) throws PropertyException { if (null == propName) { throw new IllegalArgumentException(); } if (propName.equals(Marshaller.JAXB_ENCODING)) { return this.xmlBinder.getMarshaller().getEncoding(); } if (propName.equals(Marshaller.JAXB_FORMATTED_OUTPUT)) { return this.xmlBinder.getMarshaller().isFormattedOutput(); } if (propName.equals(Marshaller.JAXB_FRAGMENT)) { return this.xmlBinder.getMarshaller().isFragment(); } if (propName.equals(Marshaller.JAXB_SCHEMA_LOCATION)) { return this.xmlBinder.getMarshaller().getSchemaLocation(); } if (propName.equals(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION)) { return this.xmlBinder.getMarshaller().getNoNamespaceSchemaLocation(); } throw new PropertyException(propName); }
anXMLMarshaller.setFragment(!xmlDocument.isXMLDeclaration()); WriterRecord writerRecord; if(anXMLMarshaller.isFormattedOutput()) { writerRecord = new FormattedWriterRecord(); } else { writerRecord.setMarshaller(anXMLMarshaller); SDOMarshalListener listener = ((SDOMarshalListener)anXMLMarshaller.getMarshalListener()); anXMLMarshaller.marshal(xmlDocument, writerRecord); writerRecord.flush(); }catch(XMLMarshalException xme){
/** * INTERNAL: * Convert the given object to an XML Document * @param object the object to marshal * @return the document which the specified object has been marshalled to * @param descriptor the XMLDescriptor for the object being marshalled * @throws XMLMarshalException if an error occurred during marshalling */ protected Document objectToXML(Object object, XMLDescriptor descriptor, boolean isXMLRoot) throws XMLMarshalException { AbstractSession session = xmlContext.getSession(descriptor); DocumentPreservationPolicy docPresPolicy = xmlContext.getDocumentPreservationPolicy(session); if (docPresPolicy != null && docPresPolicy.shouldPreserveDocument()) { XMLRecord xmlRow = null; if (!isXMLRoot) { xmlRow = (XMLRecord) ((XMLObjectBuilder) descriptor.getObjectBuilder()).createRecordFor(object, xmlContext.getDocumentPreservationPolicy(session)); xmlRow.setMarshaller(this); if (getAttachmentMarshaller() != null) { xmlRow.setXOPPackage(getAttachmentMarshaller().isXOPPackage()); } addDescriptorNamespacesToXMLRecord(descriptor, xmlRow); } return objectToXML(object, descriptor, xmlRow, isXMLRoot, docPresPolicy); } MarshalRecord marshalRecord = new NodeRecord(); marshalRecord.setMarshaller(this); marshal(object, marshalRecord, session, descriptor, isXMLRoot); return marshalRecord.getDocument(); }
session = xmlContext.getSession(((XMLRoot)object).getObject()); if(session != null){ descriptor = getDescriptor(((XMLRoot)object).getObject(), session); if (!isSimpleXMLRoot((XMLRoot) object)) { throw marshalException; descriptor = getDescriptor(object, session); XMLRecord xmlRow = (XMLRecord) ((XMLObjectBuilder) descriptor.getObjectBuilder()).createRecord(localRootName, parent, session); xmlRow.setMarshaller(this); if (getAttachmentMarshaller() != null) { xmlRow.setXOPPackage(getAttachmentMarshaller().isXOPPackage()); return objectToXML(object, descriptor, xmlRow, isXMLRoot, docPresPolicy); marshal(object, marshalRecord, session, descriptor, isXMLRoot); return marshalRecord.getDocument();
@Override public AttachmentMarshaller getAttachmentMarshaller() { if (xmlMarshaller.getAttachmentMarshaller() == null) { return null; } return ((AttachmentMarshallerAdapter) xmlMarshaller.getAttachmentMarshaller()).getAttachmentMarshaller(); }
public Marshaller.Listener getListener() { XMLMarshalListener xmlMarshalListener = xmlMarshaller.getMarshalListener(); if(null != xmlMarshalListener) { return ((JAXBMarshalListener) xmlMarshalListener).getListener(); } return null; }
public XMLMarshaller getXmlMarshaller() { XMLMarshaller marshaller = xmlMarshallerMap.get(Thread.currentThread()); if (marshaller == null) { marshaller = getXmlContext().createMarshaller(); marshaller.setMarshalListener(new SDOMarshalListener(marshaller, (SDOTypeHelper) aHelperContext.getTypeHelper())); xmlMarshallerMap.put(Thread.currentThread(), marshaller); } XMLContext context = getXmlContext(); if (marshaller.getXMLContext() != context) { marshaller.setXMLContext(context); } return marshaller; }
private void addSchemaLocations(Document document, AbstractSession session) { Element docElement = document.getDocumentElement(); NamespaceResolver resolver = new NamespaceResolver(); resolver.put(javax.xml.XMLConstants.XMLNS_ATTRIBUTE, javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI); resolver.put(XMLConstants.SCHEMA_INSTANCE_PREFIX, javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI); if ((getSchemaLocation() != null) || (getNoNamespaceSchemaLocation() != null)) { XMLField field = new XMLField("@xmlns:xsi"); field.setNamespaceResolver(resolver); XPathEngine.getInstance().create(field, docElement, javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, session); } if (getSchemaLocation() != null) { XMLField field = new XMLField("@xsi:" + XMLConstants.SCHEMA_LOCATION); field.setNamespaceResolver(resolver); XPathEngine.getInstance().create(field, docElement, getSchemaLocation(), session); } if (getNoNamespaceSchemaLocation() != null) { XMLField field = new XMLField("@xsi:" + XMLConstants.NO_NS_SCHEMA_LOCATION); field.setNamespaceResolver(resolver); XPathEngine.getInstance().create(field, docElement, getNoNamespaceSchemaLocation(), session); } }
private Object wrapEnumeration(Object object, Class enumerationClass) { Class generatedClass = this.getClassToGeneratedClasses().get(enumerationClass.getName()); if (generatedClass != null && WrappedValue.class.isAssignableFrom(generatedClass)) { ClassDescriptor desc = xmlMarshaller.getXMLContext().getSession(generatedClass).getDescriptor(generatedClass); Object newObject = desc.getInstantiationPolicy().buildNewInstance(); ((WrappedValue) newObject).setValue(object); object = newObject; } return object; }