/** * Evaluates a URI and adds it to the namespace map as a namespace. * * @param uri URI */ protected void addNamespaceURI(URI uri) { if (uri == null) { throw new IllegalArgumentException("URI argument is null."); } // extract URI without fragment String uriString = uri.toString(); String newURI = null; if (uriString != null) { // determine what comes last a '#' or '/' int hashindex = uriString.lastIndexOf('#'); int slashindex = uriString.lastIndexOf('/'); // validate (URI must contain a forward slash) if (slashindex == -1) { // namespace may have been evaluated already return; } // is there a '/' after the '#'? if (slashindex > hashindex) { // remove everything after the last '/' int index = uriString.lastIndexOf('/'); newURI = uriString.substring(0, index) + "/"; } else { // '#' comes after last '/' (remove entire fragment) newURI = uriString.replaceAll(uri.getFragment(), ""); } // only add namespace if it is new if ((newURI != null) && (!namespaces.containsValue(newURI))) { // add to namespaces namespaces.put("ns" + namespaces.size(), newURI); } } }