Example #1
0
  private void addNewPostExtension(
      Message message, PostView postView, Set<EntityView> referencedEntities) {
    // Add the extra XML elements to the message that convey all our
    // structured information. Doing this by building an XML string then
    // parsing it is gross, but avoids larger code changes.

    XmlBuilder builder = new XmlBuilder();
    builder.openElement(NEW_POST_ELEMENT_NAME, "xmlns", NEW_POST_NAMESPACE);
    for (EntityView ev : referencedEntities) {
      ev.writeToXmlBuilderOld(builder);
    }
    postView.writeToXmlBuilderOld(builder);
    builder.closeElement();

    Document extensionDocument;
    try {
      extensionDocument = DocumentHelper.parseText(builder.toString());
    } catch (DocumentException e) {
      throw new RuntimeException("Couldn't parse payload as XML");
    }

    Element extensionElement = extensionDocument.getRootElement();
    extensionElement.detach();

    message.getElement().add(elementFromXml(builder.toString()));
  }
Example #2
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());
  }
Example #3
0
  private Element elementFromXml(String xmlString) {
    Document document;
    try {
      document = DocumentHelper.parseText(xmlString);
    } catch (DocumentException e) {
      throw new RuntimeException("Couldn't parse payload as XML");
    }

    Element element = document.getRootElement();
    element.detach();

    return element;
  }
Example #4
0
 public void add(QName name, Element value) {
   value.detach();
   Element prop = findProp(HttpServletResponse.SC_OK);
   Element e = null;
   for (Object obj : prop.elements()) {
     if (obj instanceof Element && ((Element) obj).getQName().equals(name)) {
       e = (Element) obj;
       break;
     }
   }
   if (e == null) e = prop.addElement(name);
   e.add(value);
 }
Example #5
0
  public void addElement(String _currentFile, HashSet<Integer> npcSpawnIds, Element spawnElement) {
    npcSpawnIds.removeAll(_npcIds);
    npcSpawnIds.removeAll(_bossCollection);

    _currentFile = _currentFile.replace(".xml", "");

    if (npcSpawnIds.size() == 0) // dont have mob inside
    return;

    //		Element rootElement = npcSpawnIds.size() > 0 ? getMobRoot(_currentFile) :
    // getNpcRoot(_currentFile);

    getMobRoot(_currentFile).add(spawnElement.detach());
  }
Example #6
0
 public void onEnd(ElementPath path) {
   Element tagElement = path.getCurrent();
   Tag tag;
   try {
     tag = TagXMLReader.readTag(tagElement, tagType);
     tags.add(tag);
   } catch (InvalidNameException e) {
     // FIXME: Handle exception properly
     e.printStackTrace();
   } catch (IOException ioe) {
     // FIXME: Handle exception properly
     ioe.printStackTrace();
   }
   tagElement.detach(); // prune element from tree
   tagElement = null;
 }
Example #7
0
 public void onEnd(ElementPath path) {
   Element headerElement = path.getCurrent();
   parseHeader(headerElement);
   headerElement.detach();
   headerElement = null;
 }
Example #8
0
 public void onEnd(ElementPath path) {
   Element tagElement = path.getCurrent();
   Tag tag = TagXMLReader.readUnknownTag(tagElement);
   tags.add(tag);
   tagElement.detach(); // prune element from tree
 }
Example #9
0
 public void addTer(String _currentFile, Element terElement) {
   getMobRoot(_currentFile.replace(".xml", "")).add(terElement.detach());
 }