/** Tests that Session.getNamespaceURI() returns according the session scoped mapping */
 public void testGetNamespaceURI() throws RepositoryException {
   String testPrefix = getUnusedPrefix();
   // remap the jcr uri
   session.setNamespacePrefix(testPrefix, NS_JCR_URI);
   String uri = session.getNamespaceURI(testPrefix);
   assertEquals("Session.getNamespaceURI does not return the correct value.", NS_JCR_URI, uri);
 }
Beispiel #2
0
  public void defineNamespacePrefixes(Session session) throws RepositoryException {
    final Iterator<NamespaceEntry[]> iter = this.namespaceTable.values().iterator();
    while (iter.hasNext()) {
      final NamespaceEntry[] entries = iter.next();
      for (int i = 0; i < entries.length; i++) {

        // the namespace prefixing is a little bit tricky:
        String mappedPrefix = null;
        // first, we check if the namespace is registered with a prefix
        try {
          mappedPrefix = session.getNamespacePrefix(entries[i].namespace);
        } catch (NamespaceException ne) {
          // the namespace is not registered yet, so we should do this
          // can we directly use the desired prefix?
          mappedPrefix = entries[i].prefix + "_new";
          try {
            session.getNamespaceURI(entries[i].prefix);
          } catch (NamespaceException ne2) {
            // as an exception occured we can directly use the new prefix
            mappedPrefix = entries[i].prefix;
          }
          session
              .getWorkspace()
              .getNamespaceRegistry()
              .registerNamespace(mappedPrefix, entries[i].namespace);
        }
        // do we have to remap?
        if (mappedPrefix != null && !mappedPrefix.equals(entries[i].prefix)) {
          // check if the prefix is already used?
          String oldUri = null;
          try {
            oldUri = session.getNamespaceURI(entries[i].prefix);
            session.setNamespacePrefix(entries[i].prefix + "_old", oldUri);
          } catch (NamespaceException ne) {
            // ignore: prefix is not used
          }
          // finally set prefix
          session.setNamespacePrefix(entries[i].prefix, entries[i].namespace);
        }
      }
    }
  }
 /**
  * @param name
  * @param session
  * @return
  */
 private boolean isFilteredNamespace(String name, Session session) {
   // shortcut
   if (prefixFilter.isEmpty() && uriFilter.isEmpty()) {
     return false;
   }
   int pos = name.indexOf(":");
   if (pos < 0) {
     // no namespace info present
     return false;
   }
   try {
     String prefix = name.substring(0, pos);
     String uri = session.getNamespaceURI(prefix);
     return prefixFilter.contains(prefix) || uriFilter.contains(uri);
   } catch (RepositoryException e) {
     log.warn(e.getMessage());
   }
   return false;
 }