示例#1
0
  /**
   * Determine the original namespace of the given qualified name with a namespace previously mapped
   * with {@link #map(QName)} and return the original name.
   *
   * @param mapped the adapted qualified name
   * @return the original qualified name
   */
  public static QName unmap(QName mapped) {
    if (XMLConstants.NULL_NS_URI.equals(mapped.getNamespaceURI())) {
      return mapped;
    }

    return new QName(IDS.getObject(mapped.getNamespaceURI()), mapped.getLocalPart());
  }
示例#2
0
  /**
   * Map the namespace of the given qualified name to a short identifier and return the adapted
   * name.
   *
   * @param org the original qualified name
   * @return the adapted qualified name
   */
  public static QName map(QName org) {
    if (XMLConstants.NULL_NS_URI.equals(org.getNamespaceURI())) {
      return org;
    }

    return new QName(IDS.getId(org.getNamespaceURI()), org.getLocalPart());
  }
示例#3
0
  /**
   * Encode a {@link QName} for runtime use with OrientDB.
   *
   * @param org the qualified name
   * @return the encoded name
   */
  public static String encode(QName org) {
    String ns = org.getNamespaceURI();
    if (!XMLConstants.NULL_NS_URI.equals(ns)) {
      ns = IDS.getId(org.getNamespaceURI());
    }

    return ns + "_" + ONameUtil.encodeName(org.getLocalPart());
  }
 public String getURI(String prefix) {
   if (fNamespaceContext != null) {
     String uri = fNamespaceContext.getNamespaceURI(prefix);
     if (uri != null && !XMLConstants.NULL_NS_URI.equals(uri)) {
       return (fSymbolTable != null) ? fSymbolTable.addSymbol(uri) : uri.intern();
     }
   }
   return null;
 }
  /** @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) */
  @Override
  public Object getParent(Object element) {
    if (element instanceof TypeDefinition) {
      String ns = ((TypeDefinition) element).getName().getNamespaceURI();
      if (XMLConstants.NULL_NS_URI.equals(ns)) ns = "(no namespace)";
      return ns;
    }

    return null;
  }
示例#6
0
 public String getAttribute(QName name)
     throws NullPointerException, IllegalStateException, StaxNavException {
   if (name == null) {
     throw new NullPointerException("No null attribute name expected");
   }
   if (XMLConstants.NULL_NS_URI.equals(name.getNamespaceURI())) {
     return getAttribute(name.getLocalPart());
   } else {
     Map<QName, String> qualifiedAttributes = current.getElement().getQualifiedAttributes();
     if (qualifiedAttributes.isEmpty()) {
       return null;
     } else {
       return qualifiedAttributes.get(name);
     }
   }
 }
  /**
   * @see org.gvsig.bxml.stream.BxmlStreamWriter#setSchemaLocation(java.lang.String,
   *     java.lang.String)
   */
  @Override
  public void setSchemaLocation(String namespaceUri, String schemaLocationUri) {
    // * @pre {getLastEvent() == NONE}
    lastEvent = impl.getLastEvent();
    assertPre(
        lastEvent == START_DOCUMENT,
        "setSchemaLocation: last event shall be START_DOCUMENT: ",
        lastEvent);

    assertPre(
        namespaceUri != null && schemaLocationUri != null,
        "setSchemaLocation: namespace and schema location can't be null");

    assertPre(
        !XMLConstants.NULL_NS_URI.equals(namespaceUri),
        "setSchemaLocation: It is not allowed specify the location of the null namespace");

    impl.setSchemaLocation(namespaceUri, schemaLocationUri);
  }
示例#8
0
  /**
   * <code>QName</code> derived from parsing the formatted <code>String</code>.
   *
   * <p>If the <code>String</code> is <code>null</code> or does not conform to {@link #toString()
   * QName.toString()} formatting, an <code>IllegalArgumentException</code> is thrown.
   *
   * <p><em>The <code>String</code> <strong>MUST</strong> be in the form returned by {@link
   * #toString() QName.toString()}.</em>
   *
   * <p>The commonly accepted way of representing a <code>QName</code> as a <code>String</code> was
   * <a href="http://jclark.com/xml/xmlns.htm">defined</a> by James Clark. Although this is not a
   * <em>standard</em> specification, it is in common use, e.g. {@link
   * javax.xml.transform.Transformer#setParameter(String name, Object value)}. This implementation
   * parses a <code>String</code> formatted as: "{" + Namespace URI + "}" + local part. If the
   * Namespace URI <code>.equals(XMLConstants.NULL_NS_URI)</code>, only the local part should be
   * provided.
   *
   * <p>The prefix value <strong><em>CANNOT</em></strong> be represented in the <code>String</code>
   * and will be set to {@link javax.xml.XMLConstants#DEFAULT_NS_PREFIX
   * XMLConstants.DEFAULT_NS_PREFIX}.
   *
   * <p>This method does not do full validation of the resulting <code>QName</code>.
   *
   * <p>The Namespace URI is not validated as a <a href="http://www.ietf.org/rfc/rfc2396.txt">URI
   * reference</a>. The local part is not validated as a <a
   * href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a> as specified in <a
   * href="http://www.w3.org/TR/REC-xml-names/">Namespaces in XML</a>.
   *
   * @param qNameAsString <code>String</code> representation of the <code>QName</code>
   * @return <code>QName</code> corresponding to the given <code>String</code>
   * @see #toString() QName.toString()
   */
  public static QName valueOf(String qNameAsString) {

    // null is not valid
    if (qNameAsString == null) {
      throw new IllegalArgumentException("cannot create QName from \"null\" or \"\" String");
    }

    // "" local part is valid to preserve compatible behavior with QName 1.0
    if (qNameAsString.length() == 0) {
      return new QName(XMLConstants.NULL_NS_URI, qNameAsString, XMLConstants.DEFAULT_NS_PREFIX);
    }

    // local part only?
    if (qNameAsString.charAt(0) != '{') {
      return new QName(XMLConstants.NULL_NS_URI, qNameAsString, XMLConstants.DEFAULT_NS_PREFIX);
    }

    // Namespace URI improperly specified?
    if (qNameAsString.startsWith("{" + XMLConstants.NULL_NS_URI + "}")) {
      throw new IllegalArgumentException(
          "Namespace URI .equals(XMLConstants.NULL_NS_URI), "
              + ".equals(\""
              + XMLConstants.NULL_NS_URI
              + "\"), "
              + "only the local part, "
              + "\""
              + qNameAsString.substring(2 + XMLConstants.NULL_NS_URI.length())
              + "\", "
              + "should be provided.");
    }

    // Namespace URI and local part specified
    int endOfNamespaceURI = qNameAsString.indexOf('}');
    if (endOfNamespaceURI == -1) {
      throw new IllegalArgumentException(
          "cannot create QName from \"" + qNameAsString + "\", missing closing \"}\"");
    }
    return new QName(
        qNameAsString.substring(1, endOfNamespaceURI),
        qNameAsString.substring(endOfNamespaceURI + 1),
        XMLConstants.DEFAULT_NS_PREFIX);
  }
  /** @see org.eclipse.jface.viewers.ITreeContentProvider#getElements(java.lang.Object) */
  @Override
  public Object[] getElements(Object inputElement) {
    if (inputElement == null) {
      nameSpaces = null;
      return new Object[0];
    }

    @SuppressWarnings("unchecked")
    Collection<? extends TypeDefinition> types =
        (Collection<? extends TypeDefinition>) inputElement;
    nameSpaces = ArrayListMultimap.create();
    for (TypeDefinition type : types) {
      // only show types with mappable flag
      if (!type.getConstraint(MappableFlag.class).isEnabled()) continue;
      String ns = type.getName().getNamespaceURI();
      if (XMLConstants.NULL_NS_URI.equals(ns)) ns = "(no namespace)";
      nameSpaces.put(ns, type);
    }

    return nameSpaces.keySet().toArray();
  }
示例#10
0
    @Override
    public void startElement(String uri, String localName, String name, Attributes atts)
        throws SAXException {
      // Always lower-case element names, for easier XPath matching
      String lower = localName.toLowerCase();

      if (_removeNamespaces) {
        AttributesImpl attributes = new AttributesImpl();
        for (int i = 0; i < atts.getLength(); i++) {
          String local = atts.getLocalName(i);
          String qname = atts.getQName(i);
          if (!XMLConstants.NULL_NS_URI.equals(atts.getURI(i).length())
              && !local.equals(XMLConstants.XMLNS_ATTRIBUTE)
              && !qname.startsWith(XMLConstants.XMLNS_ATTRIBUTE + ":")) {
            attributes.addAttribute(
                atts.getURI(i), local, qname, atts.getType(i), atts.getValue(i));
          }
        }

        super.startElement(XMLConstants.NULL_NS_URI, lower, lower, attributes);
      } else {
        super.startElement(uri, lower, lower, atts);
      }
    }
示例#11
0
    private Element(XMLStreamReader stream, Element parent) throws XMLStreamException {
      // We assume that the stream points to the start of the modelled element
      if (stream.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new AssertionError();
      }

      //
      QName name = stream.getName();
      Location location = stream.getLocation();

      //
      Map<String, String> attributes = Collections.emptyMap();
      Map<QName, String> qualifiedAttributes = Collections.emptyMap();
      int attributeCount = stream.getAttributeCount();
      for (int i = 0; i < attributeCount; i++) {
        String attributeValue = stream.getAttributeValue(i);
        QName attributeName = stream.getAttributeName(i);
        if (XMLConstants.NULL_NS_URI.equals(attributeName.getNamespaceURI())) {
          if (attributes.isEmpty()) {
            attributes = new HashMap<String, String>();
          }
          attributes.put(attributeName.getLocalPart(), attributeValue);
        } else {
          if (qualifiedAttributes.isEmpty()) {
            qualifiedAttributes = new HashMap<QName, String>();
          }
          qualifiedAttributes.put(attributeName, attributeValue);
        }
      }

      //
      Map<String, String> namespaces;
      int namespaceCount = stream.getNamespaceCount();
      if (namespaceCount > 0) {
        namespaces = new HashMap<String, String>();
        for (int i = 0; i < namespaceCount; i++) {
          String namespacePrefix = stream.getNamespacePrefix(i);
          if (namespacePrefix == null) {
            namespacePrefix = "";
          }
          String namespaceURI = stream.getNamespaceURI(i);
          namespaces.put(namespacePrefix, namespaceURI);
        }
      } else {
        namespaces = Collections.emptyMap();
      }

      // When we leave we assume that we are positionned on the next element start or the document
      // end
      StringBuilder sb = null;
      String chunk = null;
      Object content = null;
      while (true) {
        stream.next();
        int type = stream.getEventType();
        if (type == XMLStreamConstants.END_DOCUMENT || type == XMLStreamConstants.START_ELEMENT) {
          break;
        } else if (type == XMLStreamConstants.CHARACTERS) {
          if (chunk == null) {
            chunk = stream.getText();
          } else {
            if (sb == null) {
              sb = new StringBuilder(chunk);
            }
            sb.append(stream.getText());
          }
        } else if (type == XMLStreamConstants.END_ELEMENT) {
          if (sb != null) {
            content = sb;
          } else {
            content = chunk;
          }
          break;
        }
      }

      //
      int depth = 1 + (parent != null ? parent.getDepth() : 0);

      //
      this.parent = parent;
      this.name = name;
      this.depth = depth;
      this.content = content;
      this.attributes = attributes;
      this.qualifiedAttributes = qualifiedAttributes;
      this.namespaces = namespaces;
      this.location = location;
    }
 public static Namespace forUri(String uri) {
   // FIXME when STXM-8 is done, remove the null check
   if (uri == null || XMLConstants.NULL_NS_URI.equals(uri)) return NONE;
   final Namespace element = MAP.get(uri);
   return element == null ? UNKNOWN : element;
 }
示例#13
0
 public static boolean isNoNamespaceAttribute(
     final XMLExtendedStreamReader reader, final int index) {
   String namespace = reader.getAttributeNamespace(index);
   // FIXME when STXM-8 is done, remove the null check
   return namespace == null || XMLConstants.NULL_NS_URI.equals(namespace);
 }