Ejemplo n.º 1
0
 void lookupPrefix(org.w3c.dom.Node node) {
   if (node == null) throw new IllegalArgumentException("node must not be null");
   String prefix = node.lookupPrefix(namespace.getUri());
   if (prefix == null) {
     //    check to see if we match the default namespace
     String defaultNamespace = node.lookupNamespaceURI(null);
     if (defaultNamespace == null) defaultNamespace = "";
     String nodeNamespace = namespace.getUri();
     if (nodeNamespace.equals(defaultNamespace)) {
       prefix = "";
     }
   }
   int i = 0;
   while (prefix == null) {
     String generatedPrefix = "e4x_" + i++;
     String generatedUri = node.lookupNamespaceURI(generatedPrefix);
     if (generatedUri == null) {
       prefix = generatedPrefix;
       org.w3c.dom.Node top = node;
       while (top.getParentNode() != null
           && top.getParentNode() instanceof org.w3c.dom.Element) {
         top = top.getParentNode();
       }
       ((org.w3c.dom.Element) top)
           .setAttributeNS(
               "http://www.w3.org/2000/xmlns/", "xmlns:" + prefix, namespace.getUri());
     }
   }
   namespace.setPrefix(prefix);
 }
Ejemplo n.º 2
0
  void removeNamespace(Namespace namespace) {
    Namespace current = getNodeNamespace();

    //    Do not remove in-use namespace
    if (namespace.is(current)) return;
    NamedNodeMap attrs = this.dom.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
      XmlNode attr = XmlNode.createImpl(attrs.item(i));
      if (namespace.is(attr.getNodeNamespace())) return;
    }

    //    TODO    I must confess I am not sure I understand the spec fully.  See ECMA357 13.4.4.31
    String existingPrefix = getExistingPrefixFor(namespace);
    if (existingPrefix != null) {
      if (namespace.isUnspecifiedPrefix()) {
        //    we should remove any namespace with this URI from scope; we do this by declaring a
        // namespace with the same
        //    prefix as the existing prefix and setting its URI to the default namespace
        declareNamespace(existingPrefix, getDefaultNamespace().getUri());
      } else {
        if (existingPrefix.equals(namespace.getPrefix())) {
          declareNamespace(existingPrefix, getDefaultNamespace().getUri());
        }
      }
    } else {
      //    the argument namespace is not declared in this scope, so do nothing.
    }
  }
Ejemplo n.º 3
0
  /**
   * Appends a namespace declaration in the form of a xmlns attribute to an attribute list,
   * crerating this latter if needed.
   *
   * @param atts <code>AttributeImpl</code> where to add the attribute.
   * @param ns <code>Namespace</code> the namespace to declare.
   * @return <code>AttributeImpl</code> the updated attribute list.
   */
  private AttributesImpl addNsAttribute(AttributesImpl atts, Namespace ns) {
    if (this.declareNamespaces) {
      if (atts == null) {
        atts = new AttributesImpl();
      }

      String prefix = ns.getPrefix();
      if (prefix.equals("")) {
        atts.addAttribute(
            "", // namespace
            "", // local name
            "xmlns", // qualified name
            "CDATA", // type
            ns.getURI()); // value
      } else {
        atts.addAttribute(
            "", // namespace
            "", // local name
            "xmlns:" + ns.getPrefix(), // qualified name
            "CDATA", // type
            ns.getURI()); // value
      }
    }
    return atts;
  }
Ejemplo n.º 4
0
 /**
  * This will invoke the <code>endPrefixMapping</code> callback in the <code>ContentHandler</code>
  * when a namespace is goes out of scope in the <code>Document</code>.
  *
  * @param namespace Namespace leaving scope.
  */
 private void startPrefixMapping(Namespace namespace) throws JDOMException {
   try {
     contentHandler.startPrefixMapping(namespace.getPrefix(), namespace.getURI());
   } catch (SAXException se) {
     throw new JDOMException("Exception in startPrefixMapping", se);
   }
 }
Ejemplo n.º 5
0
 static Namespace create(String prefix, String uri) {
   if (prefix == null)
     throw new IllegalArgumentException("Empty string represents default namespace prefix");
   if (uri == null) throw new IllegalArgumentException("Namespace may not lack a URI");
   Namespace rv = new Namespace();
   rv.prefix = prefix;
   rv.uri = uri;
   return rv;
 }
  private QueryResult gatherResultInfoForSelectQuery(
      String queryString, int queryNr, boolean sorted, Document doc, String[] rows) {
    Element root = doc.getRootElement();

    // Get head information
    Element child =
        root.getChild("head", Namespace.getNamespace("http://www.w3.org/2005/sparql-results#"));

    // Get result rows (<head>)
    List headChildren =
        child.getChildren(
            "variable", Namespace.getNamespace("http://www.w3.org/2005/sparql-results#"));

    Iterator it = headChildren.iterator();
    ArrayList<String> headList = new ArrayList<String>();
    while (it.hasNext()) {
      headList.add(((Element) it.next()).getAttributeValue("name"));
    }

    List resultChildren =
        root.getChild("results", Namespace.getNamespace("http://www.w3.org/2005/sparql-results#"))
            .getChildren(
                "result", Namespace.getNamespace("http://www.w3.org/2005/sparql-results#"));
    int nrResults = resultChildren.size();

    QueryResult queryResult = new QueryResult(queryNr, queryString, nrResults, sorted, headList);

    it = resultChildren.iterator();
    while (it.hasNext()) {
      Element resultElement = (Element) it.next();
      String result = "";

      // get the row values and paste it together to one String
      for (int i = 0; i < rows.length; i++) {
        List bindings =
            resultElement.getChildren(
                "binding", Namespace.getNamespace("http://www.w3.org/2005/sparql-results#"));
        String rowName = rows[i];
        for (int j = 0; j < bindings.size(); j++) {
          Element binding = (Element) bindings.get(j);
          if (binding.getAttributeValue("name").equals(rowName))
            if (result.equals(""))
              result +=
                  rowName + ": " + ((Element) binding.getChildren().get(0)).getTextNormalize();
            else
              result +=
                  "\n"
                      + rowName
                      + ": "
                      + ((Element) binding.getChildren().get(0)).getTextNormalize();
        }
      }

      queryResult.addResult(result);
    }
    return queryResult;
  }
Ejemplo n.º 7
0
 /**
  * creates a new container instance.
  *
  * @param description the container type description.
  * @param parameters the parameter passed to the container constructor.
  * @return the factored container instance.
  * @see
  *     org.eclipse.ecf.core.provider.IContainerInstantiator#createInstance(org.eclipse.ecf.core.ContainerTypeDescription,
  *     java.lang.Object[])
  */
 public IContainer createInstance(
     final ContainerTypeDescription description, final Object[] parameters)
     throws ContainerCreateException {
   try {
     final RemoteOSGiService remoteOSGiService = Activator.getDefault().getRemoteOSGiService();
     String descriptionName = description.getName();
     boolean wss = descriptionName.equals(ROSGI_WEBSOCKETSS_CONFIG);
     boolean ws = (descriptionName.equals(ROSGI_WEBSOCKETS_CONFIG) || wss);
     Namespace ns =
         (wss
             ? R_OSGiWSSNamespace.getDefault()
             : ((ws) ? R_OSGiWSNamespace.getDefault() : R_OSGiNamespace.getDefault()));
     ID containerID = null;
     if (parameters == null) {
       String localHost = "localhost"; // $NON-NLS-1$
       if (useHostname) {
         try {
           localHost = InetAddress.getLocalHost().getCanonicalHostName();
         } catch (UnknownHostException e) {
           // Ignore
         }
       }
       final String nsScheme = ns.getScheme();
       final String wsProtocol = (wss ? WSS_PROTOCOL : (ws ? WS_PROTOCOL : null));
       int listeningPort =
           remoteOSGiService.getListeningPort((wsProtocol != null) ? wsProtocol : nsScheme);
       int idPort = -1;
       if (WSS_PROTOCOL.equals(wsProtocol) && listeningPort != WSS_DEFAULT_PORT)
         idPort = listeningPort;
       else if (WS_PROTOCOL.equals(wsProtocol) && listeningPort != WS_DEFAULT_PORT)
         idPort = listeningPort;
       String portStr = (idPort > 0 ? (":" + idPort) : ""); // $NON-NLS-1$ //$NON-NLS-2$
       containerID =
           createR_OSGiID(ns, new String(nsScheme + "://" + localHost + portStr)); // $NON-NLS-1$
     } else if (parameters.length > 0) {
       if (parameters[0] instanceof ID) containerID = (ID) parameters[0];
       else if (parameters[0] instanceof String)
         containerID = createR_OSGiID(ns, (String) parameters[0]);
       else if (parameters[0] instanceof Map) {
         Map params = (Map) parameters[0];
         String idStr = (String) params.get(ID_PROP);
         if (idStr == null)
           throw new NullPointerException("No ID prop found in parameters map"); // $NON-NLS-1$
         containerID = createR_OSGiID(ns, idStr);
       }
     }
     if (containerID == null)
       throw new ContainerCreateException(
           "Unsupported arguments " //$NON-NLS-1$
               + Arrays.asList(parameters));
     if (wss) return new R_OSGiWSSRemoteServiceContainer(remoteOSGiService, containerID);
     else if (ws) return new R_OSGiWSRemoteServiceContainer(remoteOSGiService, containerID);
     else return new R_OSGiRemoteServiceContainer(remoteOSGiService, containerID);
   } catch (Exception e) {
     throw new ContainerCreateException("Could not create R_OSGI ID", e); // $NON-NLS-1$
   }
 }
Ejemplo n.º 8
0
 Namespace[] getNamespaces() {
   Iterator i = map.keySet().iterator();
   ArrayList rv = new ArrayList();
   while (i.hasNext()) {
     String prefix = (String) i.next();
     String uri = (String) map.get(prefix);
     Namespace n = Namespace.create(prefix, uri);
     if (!n.isEmpty()) {
       rv.add(n);
     }
   }
   return (Namespace[]) rv.toArray(new Namespace[0]);
 }
Ejemplo n.º 9
0
  /**
   * This will add the prefix mapping to the JDOM <code>Document</code> object.
   *
   * @param prefix <code>String</code> namespace prefix.
   * @param uri <code>String</code> namespace URI.
   */
  public void startPrefixMapping(String prefix, String uri) throws SAXException {

    if (suppress) return;

    Namespace ns = Namespace.getNamespace(prefix, uri);
    declaredNamespaces.add(ns);
  }
Ejemplo n.º 10
0
 private Namespace getNodeNamespace() {
   String uri = dom.getNamespaceURI();
   String prefix = dom.getPrefix();
   if (uri == null) uri = "";
   if (prefix == null) prefix = "";
   return Namespace.create(prefix, uri);
 }
Ejemplo n.º 11
0
 Namespace getNamespaceDeclaration(String prefix) {
   if (prefix.equals("") && dom instanceof Attr) {
     //    Default namespaces do not apply to attributes; see XML Namespaces section 5.2
     return Namespace.create("", "");
   }
   Namespaces rv = getAllNamespaces();
   return rv.getNamespace(prefix);
 }
Ejemplo n.º 12
0
 String qualify(org.w3c.dom.Node node) {
   if (namespace.getPrefix() == null) {
     if (node != null) {
       lookupPrefix(node);
     } else {
       if (namespace.getUri().equals("")) {
         namespace.setPrefix("");
       } else {
         //    TODO    I am not sure this is right, but if we are creating a standalone node, I
         // think we can set the
         //            default namespace on the node itself and not worry about setting a prefix
         // for that namespace.
         namespace.setPrefix("");
       }
     }
   }
   return qualify(namespace.getPrefix(), localName);
 }
Ejemplo n.º 13
0
 private void addNamespaces(Namespaces rv, Element element) {
   if (element == null) throw new RuntimeException("element must not be null");
   String myDefaultNamespace = toUri(element.lookupNamespaceURI(null));
   String parentDefaultNamespace = "";
   if (element.getParentNode() != null) {
     parentDefaultNamespace = toUri(element.getParentNode().lookupNamespaceURI(null));
   }
   if (!myDefaultNamespace.equals(parentDefaultNamespace)
       || !(element.getParentNode() instanceof Element)) {
     rv.declare(Namespace.create("", myDefaultNamespace));
   }
   NamedNodeMap attributes = element.getAttributes();
   for (int i = 0; i < attributes.getLength(); i++) {
     Attr attr = (Attr) attributes.item(i);
     if (attr.getPrefix() != null && attr.getPrefix().equals("xmlns")) {
       rv.declare(Namespace.create(attr.getLocalName(), attr.getValue()));
     }
   }
 }
Ejemplo n.º 14
0
 /** Return index of the <code>Attribute</code> with the given name and uri. */
 int indexOf(String name, Namespace namespace) {
   String uri = namespace.getURI();
   if (elementData != null) {
     for (int i = 0; i < size; i++) {
       Attribute old = elementData[i];
       String oldURI = old.getNamespaceURI();
       String oldName = old.getName();
       if (oldURI.equals(uri) && oldName.equals(name)) {
         return i;
       }
     }
   }
   return -1;
 }
Ejemplo n.º 15
0
  private Namespaces getAllNamespaces() {
    Namespaces rv = new Namespaces();

    Node target = this.dom;
    if (target instanceof Attr) {
      target = ((Attr) target).getOwnerElement();
    }
    while (target != null) {
      if (target instanceof Element) {
        addNamespaces(rv, (Element) target);
      }
      target = target.getParentNode();
    }
    //    Fallback in case no namespace was declared
    rv.declare(Namespace.create("", ""));
    return rv;
  }
Ejemplo n.º 16
0
  /**
   * This will invoke the <code>ContentHandler.startPrefixMapping</code> callback when a new
   * namespace is encountered in the <code>Document</code>.
   *
   * @param element <code>Element</code> used in callbacks.
   * @param namespaces <code>List</code> stack of Namespaces in scope.
   * @return <code>Attributes</code> declaring the namespaces local to <code>element</code> or
   *     <code>null</code>.
   */
  private Attributes startPrefixMapping(Element element, NamespaceStack namespaces)
      throws JDOMException {
    AttributesImpl nsAtts = null; // The namespaces as xmlns attributes

    Namespace ns = element.getNamespace();
    if (ns != Namespace.XML_NAMESPACE) {
      String prefix = ns.getPrefix();
      String uri = namespaces.getURI(prefix);
      if (!ns.getURI().equals(uri)) {
        namespaces.push(ns);
        nsAtts = this.addNsAttribute(nsAtts, ns);
        try {
          contentHandler.startPrefixMapping(prefix, ns.getURI());
        } catch (SAXException se) {
          throw new JDOMException("Exception in startPrefixMapping", se);
        }
      }
    }

    // Fire additional namespace declarations
    List additionalNamespaces = element.getAdditionalNamespaces();
    if (additionalNamespaces != null) {
      Iterator itr = additionalNamespaces.iterator();
      while (itr.hasNext()) {
        ns = (Namespace) itr.next();
        String prefix = ns.getPrefix();
        String uri = namespaces.getURI(prefix);
        if (!ns.getURI().equals(uri)) {
          namespaces.push(ns);
          nsAtts = this.addNsAttribute(nsAtts, ns);
          try {
            contentHandler.startPrefixMapping(prefix, ns.getURI());
          } catch (SAXException se) {
            throw new JDOMException("Exception in startPrefixMapping", se);
          }
        }
      }
    }
    return nsAtts;
  }
Ejemplo n.º 17
0
  /**
   * This reports the occurrence of an actual element. It will include the element's attributes,
   * with the exception of XML vocabulary specific attributes, such as <code>
   * xmlns:[namespace prefix]</code> and <code>xsi:schemaLocation</code>.
   *
   * @param namespaceURI <code>String</code> namespace URI this element is associated with, or an
   *     empty <code>String</code>
   * @param localName <code>String</code> name of element (with no namespace prefix, if one is
   *     present)
   * @param qName <code>String</code> XML 1.0 version of element name: [namespace
   *     prefix]:[localName]
   * @param atts <code>Attributes</code> list for this element
   * @throws SAXException when things go wrong
   */
  public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
      throws SAXException {
    if (suppress) return;

    Element element = null;

    if ((namespaceURI != null) && (!namespaceURI.equals(""))) {
      String prefix = "";

      // Determine any prefix on the Element
      if (!qName.equals(localName)) {
        int split = qName.indexOf(":");
        prefix = qName.substring(0, split);
      }
      Namespace elementNamespace = Namespace.getNamespace(prefix, namespaceURI);
      element = factory.element(localName, elementNamespace);
    } else {
      element = factory.element(localName);
    }

    // Take leftover declared namespaces and add them to this element's
    // map of namespaces
    if (declaredNamespaces.size() > 0) {
      transferNamespaces(element);
    }

    // Handle attributes
    for (int i = 0, len = atts.getLength(); i < len; i++) {
      Attribute attribute = null;

      String attLocalName = atts.getLocalName(i);
      String attQName = atts.getQName(i);
      int attType = getAttributeType(atts.getType(i));

      // Bypass any xmlns attributes which might appear, as we got
      // them already in startPrefixMapping().
      // This is sometimes necessary when SAXHandler is used with
      // another source than SAXBuilder, as with JDOMResult.
      if (attQName.startsWith("xmlns:") || attQName.equals("xmlns")) {
        continue;
      }

      // First clause per http://markmail.org/message/2p245ggcjst27xe6
      // patch from Mattias Jiderhamn
      if ("".equals(attLocalName) && attQName.indexOf(":") == -1) {
        attribute = factory.attribute(attQName, atts.getValue(i), attType);
      } else if (!attQName.equals(attLocalName)) {
        String attPrefix = attQName.substring(0, attQName.indexOf(":"));
        Namespace attNs = Namespace.getNamespace(attPrefix, atts.getURI(i));

        attribute = factory.attribute(attLocalName, atts.getValue(i), attType, attNs);
      } else {
        attribute = factory.attribute(attLocalName, atts.getValue(i), attType);
      }
      factory.setAttribute(element, attribute);
    }

    flushCharacters();

    if (atRoot) {
      document.setRootElement(element); // XXX should we use a factory call?
      atRoot = false;
    } else {
      factory.addContent(getCurrentElement(), element);
    }
    currentElement = element;
  }
Ejemplo n.º 18
0
 /** @deprecated Use getNamespace() */
 String getPrefix() {
   return namespace.getPrefix();
 }
Ejemplo n.º 19
0
 void setAttribute(org.w3c.dom.Element element, String value) {
   if (namespace.getPrefix() == null) lookupPrefix(element);
   element.setAttributeNS(namespace.getUri(), qualify(namespace.getPrefix(), localName), value);
 }
Ejemplo n.º 20
0
 /** @deprecated Use getNamespace() */
 String getUri() {
   return namespace.getUri();
 }
Ejemplo n.º 21
0
 Namespace getNamespace(String prefix) {
   if (map.get(prefix) == null) return null;
   return Namespace.create(prefix, (String) map.get(prefix));
 }
Ejemplo n.º 22
0
 Namespace getNamespaceByUri(String uri) {
   if (uriToPrefix.get(uri) == null) return null;
   return Namespace.create(uri, (String) uriToPrefix.get(uri));
 }
Ejemplo n.º 23
0
 private boolean namespacesEqual(Namespace one, Namespace two) {
   if (one == null && two == null) return true;
   if (one == null || two == null) return false;
   return equals(one.getUri(), two.getUri());
 }
Ejemplo n.º 24
0
 /** @deprecated */
 static QName create(String uri, String localName, String prefix) {
   return create(Namespace.create(prefix, uri), localName);
 }
Ejemplo n.º 25
0
 static Namespace create(String uri) {
   Namespace rv = new Namespace();
   rv.uri = uri;
   return rv;
 }
Ejemplo n.º 26
0
 private Namespace getDefaultNamespace() {
   String prefix = "";
   String uri = (dom.lookupNamespaceURI(null) == null) ? "" : dom.lookupNamespaceURI(null);
   return Namespace.create(prefix, uri);
 }
Ejemplo n.º 27
0
 private String getExistingPrefixFor(Namespace namespace) {
   if (getDefaultNamespace().getUri().equals(namespace.getUri())) {
     return "";
   }
   return dom.lookupPrefix(namespace.getUri());
 }