Java by API/javax.xml.namespace/NamespaceContext

Материал из Java эксперт
Перейти к: навигация, поиск

implements NamespaceContext

  
import java.io.FileReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.xml.sax.InputSource;
public class MainClass {
  public static void main(String[] args) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    SimpleNamespaceContext nsContext = new SimpleNamespaceContext();
    nsContext.addNamespace("s", "uri:test:schedule");
    xPath.setNamespaceContext(nsContext);
    String result = xPath.evaluate("/s:schedule/@name", new InputSource(
        new FileReader("t.xml")));
    System.out.println(result);
  }
}
class SimpleNamespaceContext implements NamespaceContext {
  private Map<String, String> urisByPrefix = new HashMap<String, String>();
  private Map<String, Set> prefixesByURI = new HashMap<String, Set>();
  public SimpleNamespaceContext() {
    addNamespace(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
    addNamespace(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
  }
  public synchronized void addNamespace(String prefix, String namespaceURI) {
    urisByPrefix.put(prefix, namespaceURI);
    if (prefixesByURI.containsKey(namespaceURI)) {
      (prefixesByURI.get(namespaceURI)).add(prefix);
    } else {
      Set<String> set = new HashSet<String>();
      set.add(prefix);
      prefixesByURI.put(namespaceURI, set);
    }
  }
  public String getNamespaceURI(String prefix) {
    if (prefix == null)
      throw new IllegalArgumentException("prefix cannot be null");
    if (urisByPrefix.containsKey(prefix))
      return (String) urisByPrefix.get(prefix);
    else
      return XMLConstants.NULL_NS_URI;
  }
  public String getPrefix(String namespaceURI) {
    return (String) getPrefixes(namespaceURI).next();
  }
  public Iterator getPrefixes(String namespaceURI) {
    if (namespaceURI == null)
      throw new IllegalArgumentException("namespaceURI cannot be null");
    if (prefixesByURI.containsKey(namespaceURI)) {
      return ((Set) prefixesByURI.get(namespaceURI)).iterator();
    } else {
      return Collections.EMPTY_SET.iterator();
    }
  }
}