Ejemplo n.º 1
0
  public void marshal(Object obj, Result result) throws JAXBException {
    // XMLSerializable so = Util.toXMLSerializable(obj);
    XMLSerializable so = context.getGrammarInfo().castToXMLSerializable(obj);

    if (so == null) throw new MarshalException(Messages.format(Messages.NOT_MARSHALLABLE));

    if (result instanceof SAXResult) {
      write(so, ((SAXResult) result).getHandler());
      return;
    }
    if (result instanceof DOMResult) {
      Node node = ((DOMResult) result).getNode();

      if (node == null) {
        try {
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          dbf.setNamespaceAware(true);
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document doc = db.newDocument();
          ((DOMResult) result).setNode(doc);
          write(so, new SAX2DOMEx(doc));
        } catch (ParserConfigurationException pce) {
          throw new JAXBAssertionError(pce);
        }
      } else {
        write(so, new SAX2DOMEx(node));
      }

      return;
    }
    if (result instanceof StreamResult) {
      StreamResult sr = (StreamResult) result;
      XMLWriter w = null;

      if (sr.getWriter() != null) w = createWriter(sr.getWriter());
      else if (sr.getOutputStream() != null) w = createWriter(sr.getOutputStream());
      else if (sr.getSystemId() != null) {
        String fileURL = sr.getSystemId();

        if (fileURL.startsWith("file:///")) {
          if (fileURL.substring(8).indexOf(":") > 0) fileURL = fileURL.substring(8);
          else fileURL = fileURL.substring(7);
        } // otherwise assume that it's a file name

        try {
          w = createWriter(new FileOutputStream(fileURL));
        } catch (IOException e) {
          throw new MarshalException(e);
        }
      }

      if (w == null) throw new IllegalArgumentException();

      write(so, w);
      return;
    }

    // unsupported parameter type
    throw new MarshalException(Messages.format(Messages.UNSUPPORTED_RESULT));
  }
 /**
  * Test whether this object model recognizes a particular kind of JAXP Result object, and if it
  * does, return a Receiver that builds an instance of this data model from a sequence of events.
  * If the Result is not recognised, return null.
  */
 public Receiver getDocumentBuilder(Result result) throws XPathException {
   if (result instanceof DOMResult) {
     DOMWriter emitter = new DOMWriter();
     Node root = ((DOMResult) result).getNode();
     if (root == null) {
       try {
         DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
         Document out = docBuilder.newDocument();
         ((DOMResult) result).setNode(out);
         emitter.setNode(out);
       } catch (ParserConfigurationException e) {
         throw new DynamicError(e);
       }
     } else {
       emitter.setNode(root);
     }
     return emitter;
   }
   return null;
 }
  @Override
  public void marshal(Object graph, Result result) throws XmlMappingException, IOException {
    RewardConfirmation rewardConfirmation = (RewardConfirmation) graph;
    Assert.hasText(
        rewardConfirmation.getDiningTransactionId(),
        "'diningTransactionId' is missing from reward confirmation");

    Document doc;
    try {
      doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (Throwable e) {
      throw new MarshallingFailureException("Can't create new Document", e);
    }
    Element elConfirmation = (Element) doc.appendChild(doc.createElement("reward-confirmation"));
    elConfirmation.setAttribute(
        "dining-transaction-id", rewardConfirmation.getDiningTransactionId());

    if (result instanceof DOMResult) {
      ((DOMResult) result).setNode(doc);
    } else {
      throw new IllegalArgumentException(
          "Got instance of " + result.getClass().getSimpleName() + " which is not supported");
    }
  }
  /**
   * Implements org.xml.sax.ContentHandler.endDocument() Receive notification of the end of a
   * document.
   */
  public void endDocument() throws SAXException {
    // Signal to the DOMBuilder that the document is complete
    _handler.endDocument();

    if (!_isIdentity) {
      // Run the transformation now if we have a reference to a Result object
      if (_result != null) {
        try {
          _transformer.setDOM(_dom);
          _transformer.transform(null, _result);
        } catch (TransformerException e) {
          throw new SAXException(e);
        }
      }
      // Signal that the internal DOM is built (see 'setResult()').
      _done = true;

      // Set this DOM as the transformer's DOM
      _transformer.setDOM(_dom);
    }
    if (_isIdentity && _result instanceof DOMResult) {
      ((DOMResult) _result).setNode(_transformer.getTransletOutputHandlerFactory().getNode());
    }
  }