Esempio n. 1
0
  public static XMLDocument document(Object... objects) throws ThinklabException {

    XmlNode root = null;
    ArrayList<String> namespaces = null;

    for (Object o : objects) {
      if (o instanceof String) {

        /*
         * namespace
         */
        if (namespaces == null) namespaces = new ArrayList<String>();

        // namespaces.add((String)o);

      } else if (o instanceof XmlNode) {

        /*
         * must be only root node
         */
        if (root != null)
          throw new ThinklabValidationException("XML document: non-unique root node");
        root = (XmlNode) o;
      }
    }

    if (root == null) throw new ThinklabValidationException("XML.document: no root node specified");

    XMLDocument doc = new XMLDocument(root.tag);

    if (namespaces != null) {
      for (String ns : namespaces) {
        String[] nss = ns.split("=");
        if (nss.length != 2)
          throw new ThinklabValidationException(
              "XML.document: bad namespace specification: must be name=uri: " + ns);
        doc.addNamespace(nss[0], nss[1]);
      }
    }

    root.define(doc.root(), doc.dom);

    return doc;
  }