Exemplo n.º 1
0
  /**
   * buildDocument builds org.w3c.dom.Document from given properties using given metadata for a part
   *
   * @param partMeta
   * @param rootElementName
   * @param objectProps
   * @return Document
   * @throws Exception
   */
  public static org.dom4j.Element buildDocument(
      ObjectPartType partMeta, String rootElementName, Map<String, Object> objectProps)
      throws Exception {
    ObjectPartContentType partContentMeta = partMeta.getContent();
    XmlContentType xc = partContentMeta.getXmlContent();
    if (xc == null) {
      return null;
    }

    // FIXME: We support XML validation on the way in, so we should add it here (on the way out) as
    // well.
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.newDocument();
    document.setXmlStandalone(
        true); // FIXME: REM - Can we set this to false since it is not really standalone?

    /*
     * JAXB unmarshaller recognizes the following kind of namespace
     * qualification only. More investigation is needed to use other prefix
     *
     * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
     * <ns2:collectionobjects-common xmlns:ns2="http://collectionspace.org/services/collectionobject">
     * 		<objectNumber>objectNumber-1252960222412</objectNumber>
     * 		<objectName>objectName-1252960222412</objectName>
     * </ns2:collectionobjects-common>
     */

    String ns = "ns2";
    Element root = document.createElementNS(xc.getNamespaceURI(), ns + ":" + rootElementName);
    root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

    //		String getSchemaLocation = xc.getSchemaLocation();					//FIXME: REM - w3c Document to DOM4j
    // Document mangles this attribute
    //		root.setAttribute("xsi:schemaLocation", xc.getSchemaLocation());

    String getNamespaceURI = xc.getNamespaceURI();
    root.setAttribute("xmlns:" + ns, xc.getNamespaceURI());
    document.appendChild(root);

    Schema schema = getSchemaFromName(partMeta.getLabel());

    buildDocument(document, root, objectProps, schema);
    String w3cDocumentStr = xmlToString(document);

    DOMReader reader = new DOMReader();
    org.dom4j.Document dom4jDoc = reader.read(document);
    org.dom4j.Element result = dom4jDoc.getRootElement();
    result.detach(); // return just the root element detached from the DOM document

    return result; // FIXME: REM - Add if (logger.isTraceEnabled() == true)
    // logger.trace(document.asXML());
  }