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);
  }
 static QName create(Namespace namespace, String localName) {
   //    A null namespace indicates a wild-card match for any namespace
   //    A null localName indicates "*" from the point of view of ECMA357
   if (localName != null && localName.equals("*"))
     throw new RuntimeException("* is not valid localName");
   QName rv = new QName();
   rv.namespace = namespace;
   rv.localName = localName;
   return rv;
 }
  /**
   * 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;
  }
  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);
  }
 void invalidateNamespacePrefix() {
   if (!(dom instanceof Element)) throw new IllegalStateException();
   String prefix = this.dom.getPrefix();
   QName after = QName.create(this.dom.getNamespaceURI(), this.dom.getLocalName(), null);
   renameNode(after);
   NamedNodeMap attrs = this.dom.getAttributes();
   for (int i = 0; i < attrs.getLength(); i++) {
     if (attrs.item(i).getPrefix().equals(prefix)) {
       createImpl(attrs.item(i))
           .renameNode(
               QName.create(attrs.item(i).getNamespaceURI(), attrs.item(i).getLocalName(), null));
     }
   }
 }
 final void setLocalName(String localName) {
   if (dom instanceof ProcessingInstruction) {
     setProcessingInstructionName(localName);
   } else {
     String prefix = dom.getPrefix();
     if (prefix == null) prefix = "";
     this.dom =
         dom.getOwnerDocument()
             .renameNode(dom, dom.getNamespaceURI(), QName.qualify(prefix, localName));
   }
 }
 /*
    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);
   }
 }
  /* 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;
  }
 QName newQName(XmlNode.QName qname) {
   return QName.create(this, this.globalScope, this.qnamePrototype, qname);
 }
 void setAttribute(QName name, String value) {
   if (!(dom instanceof Element))
     throw new IllegalStateException("Can only set attribute on elements.");
   name.setAttribute((Element) dom, value);
 }
 final QName getQname() {
   String uri = (dom.getNamespaceURI()) == null ? "" : dom.getNamespaceURI();
   String prefix = (dom.getPrefix() == null) ? "" : dom.getPrefix();
   return QName.create(uri, dom.getLocalName(), prefix);
 }
 final void renameNode(QName qname) {
   this.dom = dom.getOwnerDocument().renameNode(dom, qname.getUri(), qname.qualify(dom));
 }