示例#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
  private Object applyOrCall(
      boolean isApply, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
    String methodName = isApply ? "apply" : "call";
    if (!(thisObj instanceof XMLList) || ((XMLList) thisObj).targetProperty == null)
      throw ScriptRuntime.typeError1("msg.isnt.function", methodName);

    return ScriptRuntime.applyOrCall(isApply, cx, scope, thisObj, args);
  }
示例#3
0
  @Override
  void putXMLProperty(XMLName xmlName, Object value) {
    // Log("put property: " + name);

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

    if (length() > 1) {
      throw ScriptRuntime.typeError("Assignment to lists with more than one item is not supported");
    } else if (length() == 0) {
      // Secret sauce for super-expandos.
      // We set an element here, and then add ourselves to our target.
      if (targetObject != null
          && targetProperty != null
          && targetProperty.getLocalName() != null
          && targetProperty.getLocalName().length() > 0) {
        // Add an empty element with our targetProperty name and
        // then set it.
        XML xmlValue = newTextElementXML(null, targetProperty, null);
        addToList(xmlValue);

        if (xmlName.isAttributeName()) {
          setAttribute(xmlName, value);
        } else {
          XML xml = item(0);
          xml.putXMLProperty(xmlName, value);

          // Update the list with the new item at location 0.
          replace(0, item(0));
        }

        // Now add us to our parent
        XMLName name2 =
            XMLName.formProperty(
                targetProperty.getNamespace().getUri(), targetProperty.getLocalName());
        targetObject.putXMLProperty(name2, this);
        replace(0, targetObject.getXML().getLastXmlChild());
      } else {
        throw ScriptRuntime.typeError("Assignment to empty XMLList without targets not supported");
      }
    } else if (xmlName.isAttributeName()) {
      setAttribute(xmlName, value);
    } else {
      XML xml = item(0);
      xml.putXMLProperty(xmlName, value);

      // Update the list with the new item at location 0.
      replace(0, item(0));
    }
  }
示例#4
0
 private static RuntimeException badXMLName(Object value) {
   String msg;
   if (value instanceof Number) {
     msg = "Can not construct XML name from number: ";
   } else if (value instanceof Boolean) {
     msg = "Can not construct XML name from boolean: ";
   } else if (value == Undefined.instance || value == null) {
     msg = "Can not construct XML name from ";
   } else {
     throw new IllegalArgumentException(value.toString());
   }
   return ScriptRuntime.typeError(msg + ScriptRuntime.toString(value));
 }
示例#5
0
  XmlNode.QName toNodeQName(Context cx, Object namespaceValue, Object nameValue) {
    // This is duplication of constructQName(cx, namespaceValue, nameValue)
    // but for XMLName

    String localName;

    if (nameValue instanceof QName) {
      QName qname = (QName) nameValue;
      localName = qname.localName();
    } else {
      localName = ScriptRuntime.toString(nameValue);
    }

    XmlNode.Namespace ns;
    if (namespaceValue == Undefined.instance) {
      if ("*".equals(localName)) {
        ns = null;
      } else {
        ns = getDefaultNamespace(cx).getDelegate();
      }
    } else if (namespaceValue == null) {
      ns = null;
    } else if (namespaceValue instanceof Namespace) {
      ns = ((Namespace) namespaceValue).getDelegate();
    } else {
      ns = this.namespacePrototype.constructNamespace(namespaceValue).getDelegate();
    }

    if (localName != null && localName.equals("*")) localName = null;
    return XmlNode.QName.create(ns, localName);
  }
示例#6
0
 private XML parse(String frag) {
   try {
     return newXML(
         XmlNode.createElement(
             options, getDefaultNamespaceURI(Context.getCurrentContext()), frag));
   } catch (SAXException e) {
     throw ScriptRuntime.typeError("Cannot parse XML: " + e.getMessage());
   }
 }
示例#7
0
  /**
   * If value represents Uint32 index, make it available through ScriptRuntime.lastUint32Result(cx)
   * and return null. Otherwise return the same value as toXMLName(cx, value).
   */
  XMLName toXMLNameOrIndex(Context cx, Object value) {
    XMLName result;

    if (value instanceof XMLName) {
      result = (XMLName) value;
    } else if (value instanceof String) {
      String str = (String) value;
      long test = ScriptRuntime.testUint32String(str);
      if (test >= 0) {
        ScriptRuntime.storeUint32Result(cx, test);
        result = null;
      } else {
        result = toXMLNameFromString(cx, str);
      }
    } else if (value instanceof Number) {
      double d = ((Number) value).doubleValue();
      long l = (long) d;
      if (l == d && 0 <= l && l <= 0xFFFFFFFFL) {
        ScriptRuntime.storeUint32Result(cx, l);
        result = null;
      } else {
        throw badXMLName(value);
      }
    } else if (value instanceof QName) {
      QName qname = (QName) value;
      String uri = qname.uri();
      boolean number = false;
      result = null;
      if (uri != null && uri.length() == 0) {
        // Only in this case qname.toString() can resemble uint32
        long test = ScriptRuntime.testUint32String(uri);
        if (test >= 0) {
          ScriptRuntime.storeUint32Result(cx, test);
          number = true;
        }
      }
      if (!number) {
        result = XMLName.formProperty(uri, qname.localName());
      }
    } else if (value instanceof Boolean || value == Undefined.instance || value == null) {
      throw badXMLName(value);
    } else {
      String str = ScriptRuntime.toString(value);
      long test = ScriptRuntime.testUint32String(str);
      if (test >= 0) {
        ScriptRuntime.storeUint32Result(cx, test);
        result = null;
      } else {
        result = toXMLNameFromString(cx, str);
      }
    }

    return result;
  }
示例#8
0
 @Override
 boolean propertyIsEnumerable(Object name) {
   long index;
   if (name instanceof Integer) {
     index = ((Integer) name).intValue();
   } else if (name instanceof Number) {
     double x = ((Number) name).doubleValue();
     index = (long) x;
     if (index != x) {
       return false;
     }
     if (index == 0 && 1.0 / x < 0) {
       // Negative 0
       return false;
     }
   } else {
     String s = ScriptRuntime.toString(name);
     index = ScriptRuntime.testUint32String(s);
   }
   return (0 <= index && index < length());
 }
示例#9
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;
    }
  }
示例#10
0
  /* TODO: Can this can be replaced by ecmaToXml below?
   */
  final XML newXMLFromJs(Object inputObject) {
    String frag;

    if (inputObject == null || inputObject == Undefined.instance) {
      frag = "";
    } else if (inputObject instanceof XMLObjectImpl) {
      // todo: faster way for XMLObjects?
      frag = ((XMLObjectImpl) inputObject).toXMLString();
    } else {
      frag = ScriptRuntime.toString(inputObject);
    }

    if (frag.trim().startsWith("<>")) {
      throw ScriptRuntime.typeError("Invalid use of XML object anonymous tags <></>.");
    }

    if (frag.indexOf("<") == -1) {
      //    Solo text node
      return newXML(XmlNode.createText(options, frag));
    }
    return parse(frag);
  }
示例#11
0
  public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
    // This XMLList is being called as a Function.
    // Let's find the real Function object.
    if (targetProperty == null) throw ScriptRuntime.notFunctionError(this);

    String methodName = targetProperty.getLocalName();

    boolean isApply = methodName.equals("apply");
    if (isApply || methodName.equals("call")) return applyOrCall(isApply, cx, scope, thisObj, args);

    if (!(thisObj instanceof XMLObject)) {
      throw ScriptRuntime.typeError1("msg.incompat.call", methodName);
    }
    Object func = null;
    Scriptable sobj = thisObj;

    while (sobj instanceof XMLObject) {
      XMLObject xmlObject = (XMLObject) sobj;
      func = xmlObject.getFunctionProperty(cx, methodName);
      if (func != Scriptable.NOT_FOUND) {
        break;
      }
      sobj = xmlObject.getExtraMethodSource(cx);
      if (sobj != null) {
        thisObj = sobj;
        if (!(sobj instanceof XMLObject)) {
          func = ScriptableObject.getProperty(sobj, methodName);
        }
      }
    }

    if (!(func instanceof Callable)) {
      throw ScriptRuntime.notFunctionError(thisObj, func, methodName);
    }
    return ((Callable) func).call(cx, scope, thisObj, args);
  }
示例#12
0
 /*
    TODO: Too general; this should be split into overloaded methods.
    Is that possible?
 */
 XmlNode.QName toNodeQName(Context cx, Object nameValue, boolean attribute) {
   if (nameValue instanceof XMLName) {
     return ((XMLName) nameValue).toQname();
   } else if (nameValue instanceof QName) {
     QName qname = (QName) nameValue;
     return qname.getDelegate();
   } else if (nameValue instanceof Boolean
       || nameValue instanceof Number
       || nameValue == Undefined.instance
       || nameValue == null) {
     throw badXMLName(nameValue);
   } else {
     String local = null;
     if (nameValue instanceof String) {
       local = (String) nameValue;
     } else {
       local = ScriptRuntime.toString(nameValue);
     }
     return toNodeQName(cx, local, attribute);
   }
 }
示例#13
0
  Namespace getDefaultNamespace(Context cx) {
    if (cx == null) {
      cx = Context.getCurrentContext();
      if (cx == null) {
        return namespacePrototype;
      }
    }

    Object ns = ScriptRuntime.searchDefaultNamespace(cx);
    if (ns == null) {
      return namespacePrototype;
    } else {
      if (ns instanceof Namespace) {
        return (Namespace) ns;
      } else {
        //    TODO    Clarify or remove the following comment
        // Should not happen but for now it could
        // due to bad searchDefaultNamespace implementation.
        return namespacePrototype;
      }
    }
  }
示例#14
0
  /* TODO: Marked deprecated by original author */
  XMLName toXMLName(Context cx, Object nameValue) {
    XMLName result;

    if (nameValue instanceof XMLName) {
      result = (XMLName) nameValue;
    } else if (nameValue instanceof QName) {
      QName qname = (QName) nameValue;
      result = XMLName.formProperty(qname.uri(), qname.localName());
    } else if (nameValue instanceof String) {
      result = toXMLNameFromString(cx, (String) nameValue);
    } else if (nameValue instanceof Boolean
        || nameValue instanceof Number
        || nameValue == Undefined.instance
        || nameValue == null) {
      throw badXMLName(nameValue);
    } else {
      String name = ScriptRuntime.toString(nameValue);
      result = toXMLNameFromString(cx, name);
    }

    return result;
  }
示例#15
0
 /** @deprecated */
 XMLName toAttributeName(Context cx, Object nameValue) {
   if (nameValue instanceof XMLName) {
     //    TODO    Will this always be an XMLName of type attribute name?
     return (XMLName) nameValue;
   } else if (nameValue instanceof QName) {
     return XMLName.create(((QName) nameValue).getDelegate(), true, false);
   } else if (nameValue instanceof Boolean
       || nameValue instanceof Number
       || nameValue == Undefined.instance
       || nameValue == null) {
     throw badXMLName(nameValue);
   } else {
     //    TODO    Not 100% sure that putting these in global namespace is the right thing to do
     String localName = null;
     if (nameValue instanceof String) {
       localName = (String) nameValue;
     } else {
       localName = ScriptRuntime.toString(nameValue);
     }
     if (localName != null && localName.equals("*")) localName = null;
     return XMLName.create(
         XmlNode.QName.create(XmlNode.Namespace.create(""), localName), true, false);
   }
 }
示例#16
0
 public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
   throw ScriptRuntime.typeError1("msg.not.ctor", "XMLList");
 }