/**
   * Get the default namespace associated with the supplied element.
   *
   * @param element The element to be checked.
   * @return The default namespace, or null if none was found.
   */
  public static String getDefaultNamespace(Element element) {
    NamedNodeMap attributes = element.getAttributes();
    int attributeCount = attributes.getLength();

    for (int i = 0; i < attributeCount; i++) {
      Attr attribute = (Attr) attributes.item(i);

      if (XMLConstants.XMLNS_ATTRIBUTE.equals(attribute.getName())
          && XMLConstants.XMLNS_ATTRIBUTE.equals(attribute.getLocalName())) {
        return attribute.getValue();
      }
    }

    return null;
  }
 /** Declare a Namespace prefix. */
 public boolean declarePrefix(String prefix, String uri) {
   if (XMLConstants.XML_NS_PREFIX.equals(prefix) || XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
     return false;
   } else {
     currentContext.declarePrefix(prefix, uri);
     return true;
   }
 }
Exemple #3
0
 public QName(String namespaceURI, String localPart, String prefix) {
   if (namespaceURI == null) namespaceURI = XMLConstants.NULL_NS_URI;
   if (localPart == null) throw new IllegalArgumentException();
   if (prefix == null) prefix = XMLConstants.DEFAULT_NS_PREFIX;
   else {
     if (XMLConstants.XML_NS_PREFIX.equals(prefix)) namespaceURI = XMLConstants.XML_NS_URI;
     else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
       namespaceURI = XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
   }
   this.namespaceURI = namespaceURI;
   this.localPart = localPart;
   this.prefix = prefix;
 }
 protected static void addNamespace(Element element, Map<String, String> nsMap) {
   for (String ns : nsMap.keySet()) {
     // We should not override the namespace setting of the element
     if (XMLConstants.XMLNS_ATTRIBUTE.equals(ns)) {
       if (ObjectHelper.isEmpty(element.getAttribute(XMLConstants.XMLNS_ATTRIBUTE))) {
         element.setAttribute(ns, nsMap.get(ns));
       }
     } else {
       if (ObjectHelper.isEmpty(element.getAttribute(XMLConstants.XMLNS_ATTRIBUTE + ":" + ns))) {
         element.setAttribute(XMLConstants.XMLNS_ATTRIBUTE + ":" + ns, nsMap.get(ns));
       }
     }
   }
 }
 @Override
 public String getNamespaceURI(String prefix) {
   if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
     if (defaultNamespaceURI == null) return XMLConstants.NULL_NS_URI;
     else return defaultNamespaceURI;
   } else if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
     return XMLConstants.XML_NS_URI;
   } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
     return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
   } else if (prefix == null) {
     throw new IllegalArgumentException("Null prefix not allowed");
   } else {
     String uri = idNamespacesMap.get(prefix);
     if (uri == null) return XMLConstants.NULL_NS_URI;
     else return uri;
   }
 }
  /**
   * @throws IOException
   * @see {@link org.gvsig.bxml.stream.BxmlStreamWriter#writeNamespace(String, String)}
   */
  @Override
  public void writeNamespace(String prefix, String namespaceUri) throws IOException {
    // @pre {getLastEvent() IN (START_ELEMENT, NAMESPACE_DECL)}
    lastEvent = impl.getLastEvent();
    assertPre(
        lastEvent == EventType.NAMESPACE_DECL
            || lastEvent == START_ELEMENT
            || lastEvent == START_DOCUMENT,
        "setPrefix: last event shall be either NAMESPACE_DECL, START_DOCUMENT or START_ELEMENT: ",
        lastEvent);
    assertPre(prefix != null, "setPrefix: prefix can't be null");
    assertPre(namespaceUri != null, "setPrefix: namespaceUri can't be null");
    impl.writeNamespace(prefix, namespaceUri);

    String boundPrefix = impl.getPrefix(namespaceUri);
    if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
      assertPost(
          null == boundPrefix,
          "The 'xmlns' attribute should not be declared as prefix, but got it bound to ",
          namespaceUri);
    } else {
      assertPost(
          prefix.equals(boundPrefix),
          "Expected prefix ",
          prefix,
          " to be bound to namespace ",
          namespaceUri,
          " but got ",
          boundPrefix);
    }

    lastEvent = impl.getLastEvent();
    assertPost(
        lastEvent == NAMESPACE_DECL,
        "expected last event to be NAMESPACE_DECL, but is ",
        lastEvent);
  }