private void save(XMLDocument xmlDocument, Writer outputWriter, XMLMarshaller anXMLMarshaller) throws IOException { if (xmlDocument == null) { throw new IllegalArgumentException( SDOException.cannotPerformOperationWithNullInputParameter("save", "xmlDocument")); } // Ask the SDOXMLDocument if we should include the XML declaration in the resulting XML anXMLMarshaller.setFragment(!xmlDocument.isXMLDeclaration()); anXMLMarshaller.setEncoding(xmlDocument.getEncoding()); anXMLMarshaller.setSchemaLocation(xmlDocument.getSchemaLocation()); anXMLMarshaller.setNoNamespaceSchemaLocation(xmlDocument.getNoNamespaceSchemaLocation()); WriterRecord writerRecord; if (anXMLMarshaller.isFormattedOutput()) { writerRecord = new FormattedWriterRecord(); } else { writerRecord = new WriterRecord(); } writerRecord.setWriter(outputWriter); writerRecord.setMarshaller(anXMLMarshaller); ((SDOMarshalListener) anXMLMarshaller.getMarshalListener()) .setMarshalledObject(xmlDocument.getRootObject()); ((SDOMarshalListener) anXMLMarshaller.getMarshalListener()) .setMarshalledObjectRootQName( new QName(xmlDocument.getRootElementURI(), xmlDocument.getRootElementName())); ((SDOMarshalListener) anXMLMarshaller.getMarshalListener()).setRootMarshalRecord(writerRecord); anXMLMarshaller.marshal(xmlDocument, writerRecord); outputWriter.flush(); }
public void save(XMLDocument xmlDocument, Result result, Object options) throws IOException { if (xmlDocument == null) { throw new IllegalArgumentException( SDOException.cannotPerformOperationWithNullInputParameter("save", "xmlDocument")); } if (result instanceof StreamResult) { StreamResult streamResult = (StreamResult) result; Writer writer = streamResult.getWriter(); if (null == writer) { save(xmlDocument, streamResult.getOutputStream(), options); } else { save(xmlDocument, writer, options); } } else { // get XMLMarshaller once - as we may create a new instance if this helper isDirty=true XMLMarshaller anXMLMarshaller = getXmlMarshaller(options); // Ask the SDOXMLDocument if we should include the XML declaration in the resulting XML anXMLMarshaller.setFragment(!xmlDocument.isXMLDeclaration()); anXMLMarshaller.setEncoding(xmlDocument.getEncoding()); anXMLMarshaller.setSchemaLocation(xmlDocument.getSchemaLocation()); anXMLMarshaller.setNoNamespaceSchemaLocation(xmlDocument.getNoNamespaceSchemaLocation()); ((SDOMarshalListener) anXMLMarshaller.getMarshalListener()) .setMarshalledObject(xmlDocument.getRootObject()); ((SDOMarshalListener) anXMLMarshaller.getMarshalListener()) .setMarshalledObjectRootQName( new QName(xmlDocument.getRootElementURI(), xmlDocument.getRootElementName())); if (result instanceof SAXResult) { ContentHandlerRecord marshalRecord = new ContentHandlerRecord(); marshalRecord.setContentHandler(((SAXResult) result).getHandler()); marshalRecord.setMarshaller(anXMLMarshaller); ((SDOMarshalListener) anXMLMarshaller.getMarshalListener()) .setRootMarshalRecord(marshalRecord); anXMLMarshaller.marshal(xmlDocument, marshalRecord); } else if (result instanceof DOMResult) { NodeRecord marshalRecord = new NodeRecord(); marshalRecord.setDOM(((DOMResult) result).getNode()); marshalRecord.setMarshaller(anXMLMarshaller); ((SDOMarshalListener) anXMLMarshaller.getMarshalListener()) .setRootMarshalRecord(marshalRecord); anXMLMarshaller.marshal(xmlDocument, marshalRecord); } else { StringWriter writer = new StringWriter(); this.save(xmlDocument, writer, options); String xml = writer.toString(); StreamSource source = new StreamSource(new java.io.StringReader(xml)); anXMLMarshaller.getTransformer().transform(source, result); } } }
/** * Serializes an XMLDocument as an XML document into the outputStream. If the DataObject's Type * was defined by an XSD, the serialization will follow the XSD. Otherwise the serialization will * follow the format as if an XSD were generated as defined by the SDO specification. The * OutputStream will be flushed after writing. Does not perform validation to ensure compliance * with an XSD. * * @param xmlDocument specifies XMLDocument to be saved * @param outputStream specifies the OutputStream to write to. * @param options implementation-specific options. * @throws IOException for stream exceptions. * @throws IllegalArgumentException if the dataObject tree is not closed or has no container. */ public void save(XMLDocument xmlDocument, OutputStream outputStream, Object options) throws IOException { if (xmlDocument == null) { throw new IllegalArgumentException( SDOException.cannotPerformOperationWithNullInputParameter("save", "xmlDocument")); } XMLMarshaller xmlMarshaller = getXmlMarshaller(options); String encoding = xmlMarshaller.getEncoding(); if (xmlDocument.getEncoding() != null) { encoding = xmlDocument.getEncoding(); } OutputStreamWriter writer = new OutputStreamWriter(outputStream, encoding); save(xmlDocument, writer, xmlMarshaller); }
public DataObject create(Type type) { if (type == null) { throw new IllegalArgumentException( SDOException.cannotPerformOperationWithNullInputParameter("create", "type")); } SDOType sdoType = (SDOType) type; if (sdoType.isAbstract()) { // throw illegal arg exception // spec page 40 throw new IllegalArgumentException( SDOException.errorCreatingDataObjectForType(sdoType.getURI(), sdoType.getName())); } if (sdoType.isDataType()) { SDOTypeHelper sth = (SDOTypeHelper) getHelperContext().getTypeHelper(); sdoType = (SDOType) sth.getWrappersHashMap().get(sdoType.getQName()); } Class implClass; try { implClass = sdoType.getImplClass(); if (implClass != null) { // initialization of the properties Map Implementation will be done in the default // constructor call below // testcase is in org.apache.tuscany.sdo.test SDODataObject theDataObject = (SDODataObject) implClass.newInstance(); theDataObject._setType(sdoType); theDataObject._setHelperContext(getHelperContext()); return theDataObject; } } catch (InstantiationException e) { throw new IllegalArgumentException( SDOException.errorCreatingDataObjectForClass( e, (sdoType).getInstanceClassName(), sdoType.getURI(), sdoType.getName())); } catch (IllegalAccessException e) { throw new IllegalArgumentException( SDOException.errorCreatingDataObjectForClass( e, (sdoType).getInstanceClassName(), sdoType.getURI(), sdoType.getName())); } SDODataObject dataObject = new SDODataObject(); dataObject._setType(sdoType); dataObject._setHelperContext(getHelperContext()); return dataObject; }
private void save(XMLDocument xmlDocument, Writer outputWriter, XMLMarshaller anXMLMarshaller) throws IOException { if (xmlDocument == null) { throw new IllegalArgumentException( SDOException.cannotPerformOperationWithNullInputParameter("save", "xmlDocument")); } // Ask the SDOXMLDocument if we should include the XML declaration in the resulting XML anXMLMarshaller.setFragment(!xmlDocument.isXMLDeclaration()); anXMLMarshaller.setEncoding(xmlDocument.getEncoding()); anXMLMarshaller.setSchemaLocation(xmlDocument.getSchemaLocation()); anXMLMarshaller.setNoNamespaceSchemaLocation(xmlDocument.getNoNamespaceSchemaLocation()); WriterRecord writerRecord; if (anXMLMarshaller.isFormattedOutput()) { writerRecord = new FormattedWriterRecord(); } else { writerRecord = new WriterRecord(); } writerRecord.setWriter(outputWriter); writerRecord.setMarshaller(anXMLMarshaller); ((SDOMarshalListener) anXMLMarshaller.getMarshalListener()) .setMarshalledObject(xmlDocument.getRootObject()); ((SDOMarshalListener) anXMLMarshaller.getMarshalListener()) .setMarshalledObjectRootQName( new QName(xmlDocument.getRootElementURI(), xmlDocument.getRootElementName())); ((SDOMarshalListener) anXMLMarshaller.getMarshalListener()).setRootMarshalRecord(writerRecord); try { anXMLMarshaller.marshal(xmlDocument, writerRecord); } catch (XMLMarshalException xme) { if (xme.getErrorCode() == XMLMarshalException.DESCRIPTOR_NOT_FOUND_IN_PROJECT) { if (aHelperContext != ((SDOType) xmlDocument.getRootObject().getType()).getHelperContext()) { throw SDOException.dataObjectNotFromHelperContext(); } } } outputWriter.flush(); }
public DataObject create(Class interfaceClass) { if (interfaceClass == null) { throw new IllegalArgumentException( SDOException.cannotPerformOperationWithNullInputParameter("create", "interfaceClass")); } Type type = getHelperContext().getTypeHelper().getType(interfaceClass); if (type != null) { return create(type); } // at this point the type may not have been defined or there could be a classloader issue SDOXMLHelper xmlHelper = (SDOXMLHelper) getHelperContext().getXMLHelper(); ClassLoader contextLoader = xmlHelper.getLoader(); ClassLoader interfaceLoader = interfaceClass.getClassLoader(); boolean loadersAreRelated = false; // check the hierarchy to see if the interface loader is a parent of the context loader List<ClassLoader> visitedLoaders = new ArrayList<ClassLoader>(); while (contextLoader != null && !loadersAreRelated) { if (visitedLoaders.contains(contextLoader)) { // if we encounter a loader we've already checked, break out break; } visitedLoaders.add(contextLoader); if (contextLoader == interfaceLoader) { loadersAreRelated = true; } ClassLoader parentLoader = contextLoader.getParent(); if (contextLoader == parentLoader) { break; } contextLoader = parentLoader; } throw new IllegalArgumentException( SDOException.typeNotFoundForInterface(interfaceClass.getName(), loadersAreRelated)); }