コード例 #1
0
  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();
  }
コード例 #2
0
  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);
      }
    }
  }
コード例 #3
0
  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();
  }
 /**
  * Set a property on the JAXBMarshaller. Attempting to set any unsupported property will result in
  * a javax.xml.bind.PropertyException
  *
  * @see org.eclipse.persistence.jaxb.MarshallerProperties
  */
 public void setProperty(String key, Object value) throws PropertyException {
   try {
     if (key == null) {
       throw new IllegalArgumentException();
     } else if (Constants.JAXB_FRAGMENT.equals(key)) {
       if (value == null) {
         throw new PropertyException(key, Constants.EMPTY_STRING);
       }
       Boolean fragment = (Boolean) value;
       xmlMarshaller.setFragment(fragment.booleanValue());
     } else if (JAXB_FORMATTED_OUTPUT.equals(key)) {
       if (value == null) {
         throw new PropertyException(key, Constants.EMPTY_STRING);
       }
       Boolean formattedOutput = (Boolean) value;
       xmlMarshaller.setFormattedOutput(formattedOutput.booleanValue());
     } else if (JAXB_ENCODING.equals(key)) {
       xmlMarshaller.setEncoding((String) value);
     } else if (JAXB_SCHEMA_LOCATION.equals(key)) {
       xmlMarshaller.setSchemaLocation((String) value);
     } else if (JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(key)) {
       xmlMarshaller.setNoNamespaceSchemaLocation((String) value);
     } else if (MarshallerProperties.NAMESPACE_PREFIX_MAPPER.equals(key)) {
       if (value == null) {
         xmlMarshaller.setNamespacePrefixMapper(null);
       } else if (value instanceof Map) {
         NamespacePrefixMapper namespacePrefixMapper = new MapNamespacePrefixMapper((Map) value);
         xmlMarshaller.setNamespacePrefixMapper(namespacePrefixMapper);
       } else {
         xmlMarshaller.setNamespacePrefixMapper((NamespacePrefixMapper) value);
       }
     } else if (SUN_NAMESPACE_PREFIX_MAPPER.equals(key)
         || SUN_JSE_NAMESPACE_PREFIX_MAPPER.equals(key)) {
       if (value == null) {
         xmlMarshaller.setNamespacePrefixMapper(null);
       } else {
         xmlMarshaller.setNamespacePrefixMapper(new NamespacePrefixMapperWrapper(value));
       }
     } else if (MarshallerProperties.INDENT_STRING.equals(key)
         || SUN_INDENT_STRING.equals(key)
         || SUN_JSE_INDENT_STRING.equals(key)) {
       xmlMarshaller.setIndentString((String) value);
     } else if (MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS.equals(key)) {
       xmlMarshaller.setMarshalEmptyCollections((Boolean) value);
     } else if (MarshallerProperties.JSON_REDUCE_ANY_ARRAYS.equals(key)) {
       xmlMarshaller.setReduceAnyArrays((Boolean) value);
     } else if (MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME.equals(key)) {
       xmlMarshaller.setWrapperAsCollectionName((Boolean) value);
     } else if (MarshallerProperties.CHARACTER_ESCAPE_HANDLER.equals(key)) {
       xmlMarshaller.setCharacterEscapeHandler((CharacterEscapeHandler) value);
     } else if (SUN_CHARACTER_ESCAPE_HANDLER.equals(key)
         || SUN_JSE_CHARACTER_ESCAPE_HANDLER.equals(key)) {
       if (value == null) {
         xmlMarshaller.setCharacterEscapeHandler(null);
       } else {
         xmlMarshaller.setCharacterEscapeHandler(new CharacterEscapeHandlerWrapper(value));
       }
     } else if (XML_DECLARATION.equals(key)) {
       if (value == null) {
         throw new PropertyException(key, Constants.EMPTY_STRING);
       }
       Boolean fragment = !(Boolean) value;
       xmlMarshaller.setFragment(fragment.booleanValue());
     } else if (XML_HEADERS.equals(key)) {
       xmlMarshaller.setXmlHeader((String) value);
     } else if (OBJECT_IDENTITY_CYCLE_DETECTION.equals(key)) {
       if (value == null) {
         throw new PropertyException(key, Constants.EMPTY_STRING);
       }
       xmlMarshaller.setEqualUsingIdenity(((Boolean) value).booleanValue());
     } else if (MarshallerProperties.MEDIA_TYPE.equals(key)) {
       MediaType mType = null;
       if (value instanceof MediaType) {
         mType = (MediaType) value;
       } else if (value instanceof String) {
         mType = MediaType.getMediaType((String) value);
       }
       if (mType == null) {
         throw new PropertyException(key, Constants.EMPTY_STRING);
       }
       xmlMarshaller.setMediaType(mType);
     } else if (MarshallerProperties.JSON_ATTRIBUTE_PREFIX.equals(key)) {
       xmlMarshaller.setAttributePrefix((String) value);
     } else if (MarshallerProperties.JSON_INCLUDE_ROOT.equals(key)) {
       if (value == null) {
         throw new PropertyException(key, Constants.EMPTY_STRING);
       }
       xmlMarshaller.setIncludeRoot((Boolean) value);
     } else if (MarshallerProperties.JSON_VALUE_WRAPPER.equals(key)) {
       if (value == null || (((String) value).length() == 0)) {
         throw new PropertyException(key, Constants.EMPTY_STRING);
       }
       xmlMarshaller.setValueWrapper((String) value);
     } else if (MarshallerProperties.JSON_NAMESPACE_SEPARATOR.equals(key)) {
       if (value == null) {
         throw new PropertyException(key, Constants.EMPTY_STRING);
       }
       xmlMarshaller.setNamespaceSeparator((Character) value);
     } else if (MarshallerProperties.OBJECT_GRAPH.equals(key)) {
       if (value == null) {
         xmlMarshaller.setMarshalAttributeGroup(null);
       } else if (value instanceof ObjectGraphImpl) {
         xmlMarshaller.setMarshalAttributeGroup(((ObjectGraphImpl) value).getAttributeGroup());
       } else if (value.getClass() == ClassConstants.STRING) {
         xmlMarshaller.setMarshalAttributeGroup(value);
       } else {
         throw org.eclipse.persistence.exceptions.JAXBException.invalidValueForObjectGraph(value);
       }
     } else {
       throw new PropertyException(key, value);
     }
   } catch (ClassCastException exception) {
     throw new PropertyException(key, exception);
   }
 }