示例#1
0
 final XML ecmaToXml(Object object) {
   //    See ECMA357 10.3
   if (object == null || object == Undefined.instance) {
     throw ScriptRuntime.typeError("Cannot convert " + object + " to XML");
   }
   if (object instanceof XML) return (XML) object;
   if (object instanceof XMLList) {
     XMLList list = (XMLList) object;
     if (list.getXML() != null) {
       return list.getXML();
     } else {
       throw ScriptRuntime.typeError("Cannot convert list of >1 element to XML");
     }
   }
   //    TODO    Technically we should fail on anything except a String, Number or Boolean
   //            See ECMA357 10.3
   // Extension: if object is a DOM node, use that to construct the XML
   // object.
   if (object instanceof Wrapper) {
     object = ((Wrapper) object).unwrap();
   }
   if (object instanceof org.w3c.dom.Node) {
     org.w3c.dom.Node node = (org.w3c.dom.Node) object;
     return newXML(XmlNode.createElementFromNode(node));
   }
   //    Instead we just blindly cast to a String and let them convert anything.
   String s = ScriptRuntime.toString(object);
   //    TODO    Could this get any uglier?
   if (s.length() > 0 && s.charAt(0) == '<') {
     return parse(s);
   } else {
     return newXML(XmlNode.createText(options, s));
   }
 }
示例#2
0
  @Override
  boolean equivalentXml(Object target) {
    boolean result = false;

    // Zero length list should equate to undefined
    if (target instanceof Undefined && length() == 0) {
      result = true;
    } else if (length() == 1) {
      result = getXmlFromAnnotation(0).equivalentXml(target);
    } else if (target instanceof XMLList) {
      XMLList otherList = (XMLList) target;

      if (otherList.length() == length()) {
        result = true;

        for (int i = 0; i < length(); i++) {
          if (!getXmlFromAnnotation(i).equivalentXml(otherList.getXmlFromAnnotation(i))) {
            result = false;
            break;
          }
        }
      }
    }

    return result;
  }
示例#3
0
  @Override
  XMLList children() {
    ArrayList<XML> list = new ArrayList<XML>();

    for (int i = 0; i < length(); i++) {
      XML xml = getXmlFromAnnotation(i);

      if (xml != null) {
        XMLList childList = xml.children();

        int cChildren = childList.length();
        for (int j = 0; j < cChildren; j++) {
          list.add(childList.item(j));
        }
      }
    }

    XMLList allChildren = newXMLList();
    int sz = list.size();

    for (int i = 0; i < sz; i++) {
      allChildren.addToList(list.get(i));
    }

    return allChildren;
  }
示例#4
0
 @Override
 XMLList elements(XMLName name) {
   XMLList rv = newXMLList();
   for (int i = 0; i < length(); i++) {
     XML xml = getXmlFromAnnotation(i);
     rv.addToList(xml.elements(name));
   }
   return rv;
 }
示例#5
0
  @Override
  XMLList text() {
    XMLList result = newXMLList();

    for (int i = 0; i < length(); i++) {
      result.addToList(getXmlFromAnnotation(i).text());
    }

    return result;
  }
示例#6
0
  @Override
  XMLList child(XMLName xmlName) {
    XMLList result = newXMLList();

    for (int i = 0; i < length(); i++) {
      result.addToList(getXmlFromAnnotation(i).child(xmlName));
    }

    return result;
  }
示例#7
0
  @Override
  XMLObjectImpl copy() {
    XMLList result = newXMLList();

    for (int i = 0; i < length(); i++) {
      XML xml = getXmlFromAnnotation(i);
      result.addToList(xml.copy());
    }

    return result;
  }
示例#8
0
  @Override
  XMLList processingInstructions(XMLName xmlName) {
    XMLList result = newXMLList();

    for (int i = 0; i < length(); i++) {
      XML xml = getXmlFromAnnotation(i);

      result.addToList(xml.processingInstructions(xmlName));
    }

    return result;
  }
示例#9
0
  private XMLList getPropertyList(XMLName name) {
    XMLList propertyList = newXMLList();
    XmlNode.QName qname = null;

    if (!name.isDescendants() && !name.isAttributeName()) {
      // Only set the targetProperty if this is a regular child get
      // and not a descendant or attribute get
      qname = name.toQname();
    }

    propertyList.setTargets(this, qname);

    for (int i = 0; i < length(); i++) {
      propertyList.addToList(getXmlFromAnnotation(i).getPropertyList(name));
    }

    return propertyList;
  }
示例#10
0
  private void exportToScope(boolean sealed) {
    xmlPrototype = newXML(XmlNode.createText(options, ""));
    xmlListPrototype = newXMLList();
    namespacePrototype = Namespace.create(this.globalScope, null, XmlNode.Namespace.GLOBAL);
    qnamePrototype =
        QName.create(
            this, this.globalScope, null, XmlNode.QName.create(XmlNode.Namespace.create(""), ""));

    xmlPrototype.exportAsJSClass(sealed);
    xmlListPrototype.exportAsJSClass(sealed);
    namespacePrototype.exportAsJSClass(sealed);
    qnamePrototype.exportAsJSClass(sealed);
  }
示例#11
0
  final XMLList newXMLListFrom(Object inputObject) {
    XMLList rv = newXMLList();

    if (inputObject == null || inputObject instanceof Undefined) {
      return rv;
    } else if (inputObject instanceof XML) {
      XML xml = (XML) inputObject;
      rv.getNodeList().add(xml);
      return rv;
    } else if (inputObject instanceof XMLList) {
      XMLList xmll = (XMLList) inputObject;
      rv.getNodeList().add(xmll.getNodeList());
      return rv;
    } else {
      String frag = ScriptRuntime.toString(inputObject).trim();

      if (!frag.startsWith("<>")) {
        frag = "<>" + frag + "</>";
      }

      frag = "<fragment>" + frag.substring(2);
      if (!frag.endsWith("</>")) {
        throw ScriptRuntime.typeError("XML with anonymous tag missing end anonymous tag");
      }

      frag = frag.substring(0, frag.length() - 3) + "</fragment>";

      XML orgXML = newXMLFromJs(frag);

      // Now orphan the children and add them to our XMLList.
      XMLList children = orgXML.children();

      for (int i = 0; i < children.getNodeList().length(); i++) {
        // Copy here is so that they'll be orphaned (parent() will be undefined)
        rv.getNodeList().add(((XML) children.item(i).copy()));
      }
      return rv;
    }
  }
示例#12
0
  Object addXMLObjects(Context cx, XMLObject obj1, XMLObject obj2) {
    XMLList listToAdd = newXMLList();

    if (obj1 instanceof XMLList) {
      XMLList list1 = (XMLList) obj1;
      if (list1.length() == 1) {
        listToAdd.addToList(list1.item(0));
      } else {
        // Might be xmlFragment + xmlFragment + xmlFragment + ...;
        // then the result will be an XMLList which we want to be an
        // rValue and allow it to be assigned to an lvalue.
        listToAdd = newXMLListFrom(obj1);
      }
    } else {
      listToAdd.addToList(obj1);
    }

    if (obj2 instanceof XMLList) {
      XMLList list2 = (XMLList) obj2;
      for (int i = 0; i < list2.length(); i++) {
        listToAdd.addToList(list2.item(i));
      }
    } else if (obj2 instanceof XML) {
      listToAdd.addToList(obj2);
    }

    return listToAdd;
  }
示例#13
0
  @Override
  public void put(int index, Scriptable start, Object value) {
    Object parent = Undefined.instance;
    // Convert text into XML if needed.
    XMLObject xmlValue;

    // Special-case checks for undefined and null
    if (value == null) {
      value = "null";
    } else if (value instanceof Undefined) {
      value = "undefined";
    }

    if (value instanceof XMLObject) {
      xmlValue = (XMLObject) value;
    } else {
      if (targetProperty == null) {
        xmlValue = newXMLFromJs(value.toString());
      } else {
        //    Note that later in the code, we will use this as an argument to replace(int,value)
        //    So we will be "replacing" this element with itself
        //    There may well be a better way to do this
        //    TODO    Find a way to refactor this whole method and simplify it
        xmlValue = item(index);
        if (xmlValue == null) {
          XML x = item(0);
          xmlValue = x == null ? newTextElementXML(null, targetProperty, null) : x.copy();
        }
        ((XML) xmlValue).setChildren(value);
      }
    }

    // Find the parent
    if (index < length()) {
      parent = item(index).parent();
    } else if (length() == 0) {
      parent = targetObject != null ? targetObject.getXML() : parent();
    } else {
      // Appending
      parent = parent();
    }

    if (parent instanceof XML) {
      // found parent, alter doc
      XML xmlParent = (XML) parent;

      if (index < length()) {
        // We're replacing the the node.
        XML xmlNode = getXmlFromAnnotation(index);

        if (xmlValue instanceof XML) {
          replaceNode(xmlNode, (XML) xmlValue);
          replace(index, xmlNode);
        } else if (xmlValue instanceof XMLList) {
          // Replace the first one, and add the rest on the list.
          XMLList list = (XMLList) xmlValue;

          if (list.length() > 0) {
            int lastIndexAdded = xmlNode.childIndex();
            replaceNode(xmlNode, list.item(0));
            replace(index, list.item(0));

            for (int i = 1; i < list.length(); i++) {
              xmlParent.insertChildAfter(xmlParent.getXmlChild(lastIndexAdded), list.item(i));
              lastIndexAdded++;
              insert(index + i, list.item(i));
            }
          }
        }
      } else {
        // Appending
        xmlParent.appendChild(xmlValue);
        addToList(xmlParent.getLastXmlChild());
      }
    } else {
      // Don't all have same parent, no underlying doc to alter
      if (index < length()) {
        XML xmlNode = getXML(_annos, index);

        if (xmlValue instanceof XML) {
          replaceNode(xmlNode, (XML) xmlValue);
          replace(index, xmlNode);
        } else if (xmlValue instanceof XMLList) {
          // Replace the first one, and add the rest on the list.
          XMLList list = (XMLList) xmlValue;

          if (list.length() > 0) {
            replaceNode(xmlNode, list.item(0));
            replace(index, list.item(0));

            for (int i = 1; i < list.length(); i++) {
              insert(index + i, list.item(i));
            }
          }
        }
      } else {
        addToList(xmlValue);
      }
    }
  }