List of usage examples for org.dom4j Namespace getURI
public String getURI()
From source file:com.cladonia.schema.XMLSchema.java
License:Open Source License
private void updatePrefixes(Vector declarations, ElementInformation element) { Vector attributes = element.getAttributes(); Vector children = element.getChildElements(); for (int i = 0; i < children.size(); i++) { ElementInformation child = (ElementInformation) children.elementAt(i); for (int k = 0; k < declarations.size(); k++) { Namespace ns = (Namespace) declarations.elementAt(k); if (ns.getURI().equals(child.getNamespace())) {// && ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) { child.setPrefix(ns.getPrefix()); break; }//from ww w .ja v a 2 s. co m } } if (attributes != null) { for (int j = 0; j < attributes.size(); j++) { AttributeInformation attribute = (AttributeInformation) attributes.elementAt(j); for (int k = 0; k < declarations.size(); k++) { Namespace ns = (Namespace) declarations.elementAt(k); if (ns.getURI().equals(attribute.getNamespace())) { // && ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) { attribute.setPrefix(ns.getPrefix()); break; } } } } }
From source file:com.cladonia.schema.XMLSchema.java
License:Open Source License
public void updatePrefixes(Vector declarations) { Vector allElements = getAllElements(); for (int i = 0; i < allElements.size(); i++) { ElementInformation model = (ElementInformation) allElements.elementAt(i); Vector attributes = model.getAttributes(); Vector children = model.getChildElements(); if (children != null) { for (int j = 0; j < children.size(); j++) { ElementInformation child = (ElementInformation) children.elementAt(j); updatePrefixes(declarations, child); for (int k = 0; k < declarations.size(); k++) { Namespace ns = (Namespace) declarations.elementAt(k); if (ns.getURI().equals(child.getNamespace())) {// && ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) { child.setPrefix(ns.getPrefix()); break; }// w w w . j av a2 s . c o m } } } if (attributes != null) { for (int j = 0; j < attributes.size(); j++) { AttributeInformation attribute = (AttributeInformation) attributes.elementAt(j); for (int k = 0; k < declarations.size(); k++) { Namespace ns = (Namespace) declarations.elementAt(k); if (ns.getURI().equals(attribute.getNamespace())) { // && ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) { attribute.setPrefix(ns.getPrefix()); break; } } } } for (int j = 0; j < declarations.size(); j++) { Namespace ns = (Namespace) declarations.elementAt(j); if (ns.getURI().equals(model.getNamespace())) { // && ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) { model.setPrefix(ns.getPrefix()); break; } } } }
From source file:com.cladonia.xml.designer.ElementNode.java
License:Open Source License
/** * Wether the current uri has already been declared * as a namespace?// w w w . j a va 2 s .c o m * * @param uri the namespace uri. * * @return true when the namespace has been declared by a parent element. */ public boolean isNamespaceDeclared() { XElement element = parent.getElement(); Namespace ns = element.getNamespaceForURI(type.getNamespace()); if (DEBUG) { if (ns != null) { System.out.println("ns: xmlns:" + ns.getPrefix() + "=" + ns.getURI()); } } return ns != null; }
From source file:com.cladonia.xml.ExchangerDocument.java
License:Open Source License
private void setNames(Hashtable eNames, Hashtable aNames, Vector prefixes, XElement element) { XElement[] elements = element.getElements(); for (int i = 0; i < elements.length; i++) { setNames(eNames, aNames, prefixes, elements[i]); }/* w w w . java 2s . c o m*/ XAttribute[] attributes = element.getAttributes(); for (int i = 0; i < attributes.length; i++) { QName name = attributes[i].getQName(); Vector values = (Vector) aNames.get(name); if (values == null) { values = new Vector(); aNames.put(name, values); } if (!values.contains(attributes[i].getValue())) { values.addElement(attributes[i].getValue()); } } QName name = element.getQName(); Counter count = (Counter) eNames.get(name); if (count == null) { eNames.put(name, new Counter()); } else { count.counter++; } List nss = element.declaredNamespaces(); for (int i = 0; i < nss.size(); i++) { Namespace ns = (Namespace) nss.get(i); if (!prefixes.contains(ns) && ns.getURI() != null && ns.getURI().trim().length() > 0) { //&& ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) { prefixes.addElement(ns); } } }
From source file:com.cladonia.xml.ExchangerXMLWriter.java
License:Open Source License
protected void writeAttributes(Element element) throws IOException { // I do not yet handle the case where the same prefix maps to // two different URIs. For attributes on the same element // this is illegal; but as yet we don't throw an exception // if someone tries to do this for (int i = 0, size = element.attributeCount(); i < size; i++) { Attribute attribute = element.attribute(i); Namespace ns = attribute.getNamespace(); if (ns != null && ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) { String prefix = ns.getPrefix(); String uri = namespaceStack.getURI(prefix); if (!ns.getURI().equals(uri)) { // output a new namespace declaration writeNamespace(ns);/*from ww w . ja v a2s .c o m*/ namespaceStack.push(ns); } } writeAttribute(attribute); } }
From source file:com.cladonia.xml.ExchangerXMLWriter.java
License:Open Source License
protected boolean isNamespaceDeclaration(Namespace ns) { if (ns != null && ns != Namespace.XML_NAMESPACE) { String uri = ns.getURI(); if (uri != null) { if (!namespaceStack.contains(ns)) { return true; }//w w w.j a va 2 s . c om } } return false; }
From source file:com.cladonia.xml.navigator.Navigator.java
License:Open Source License
private void updateOutlineInternal() { if (DEBUG)/*from ww w. j ava 2s .co m*/ System.out.println("Navigator.updateOutline()"); boolean showAll = false; if (document != null) { XElement root = (XElement) document.getLastRoot(); String xpathValue = null; if (xpathPanel.isVisible()) { xpathValue = xpathField.getText(); } else { int type = selectedNamespaceAction.getType(); Namespace namespace = selectedNamespaceAction.getNamespace(); if (showAttributes() && showElementContent()) { if (type == SELECTED_NAMESPACE_TYPE_ALL) { showAll = true; xpathValue = "//*|//text()|//@*"; } else if (type == SELECTED_NAMESPACE_TYPE_NONE) { xpathValue = "//*[not(namespace-uri())]|//*[not(namespace-uri())]/text()|//*[not(namespace-uri())]/@*"; } else if (type == SELECTED_NAMESPACE_TYPE_OTHER) { String prefix = namespace.getPrefix(); String uri = namespace.getURI(); if (prefix != null && prefix.trim().length() > 0) { xpathValue = "//" + prefix + ":*|//" + prefix + ":*/text()|//" + prefix + ":*/@*"; } else { xpathValue = "//*[namespace-uri()='" + uri + "']|//*[namespace-uri()='" + uri + "']/text()|//*[namespace-uri()='" + uri + "']/@*"; } } } else if (showAttributes() && !showElementContent()) { if (type == SELECTED_NAMESPACE_TYPE_ALL) { showAll = true; xpathValue = "//*|//@*"; } else if (type == SELECTED_NAMESPACE_TYPE_NONE) { xpathValue = "//*[not(namespace-uri())]|//*[not(namespace-uri())]/@*"; } else if (type == SELECTED_NAMESPACE_TYPE_OTHER) { String prefix = namespace.getPrefix(); String uri = namespace.getURI(); if (prefix != null && prefix.trim().length() > 0) { xpathValue = "//" + prefix + ":*|//" + prefix + ":*/@*"; } else { xpathValue = "//*[namespace-uri()='" + uri + "']|//*[namespace-uri()='" + uri + "']/@*"; } } } else if (!showAttributes() && showElementContent()) { if (type == SELECTED_NAMESPACE_TYPE_ALL) { showAll = true; xpathValue = "//*|//text()"; } else if (type == SELECTED_NAMESPACE_TYPE_NONE) { xpathValue = "//*[not(namespace-uri())]|//*[not(namespace-uri())]/text()"; } else if (type == SELECTED_NAMESPACE_TYPE_OTHER) { String prefix = namespace.getPrefix(); String uri = namespace.getURI(); if (prefix != null && prefix.trim().length() > 0) { xpathValue = "//" + prefix + ":*|//" + prefix + ":*/text()"; } else { xpathValue = "//*[namespace-uri()='" + uri + "']|//*[namespace-uri()='" + uri + "']/text()"; } } } else { if (type == SELECTED_NAMESPACE_TYPE_ALL) { showAll = true; xpathValue = "//*"; } else if (type == SELECTED_NAMESPACE_TYPE_NONE) { xpathValue = "//*[not(namespace-uri())]"; } else if (type == SELECTED_NAMESPACE_TYPE_OTHER) { String prefix = namespace.getPrefix(); String uri = namespace.getURI(); if (prefix != null && prefix.trim().length() > 0) { xpathValue = "//" + prefix + ":*"; } else { xpathValue = "//*[namespace-uri()='" + uri + "']"; } } } } Vector results = null; try { results = document.searchLastWellFormedDocument(xpathValue); xpathField.setForeground(Color.black); } catch (Exception e) { if (xpathField.getText() != null && xpathField.getText().trim().length() > 0) { xpathField.setForeground(Color.red); } } elementContentInResult = false; attributeInResult = false; if (root != null) { long time = System.currentTimeMillis(); // System.out.println( "Building Tree..."); Vector shrinkingNodes = new Vector(); if (results != null) { for (int i = 0; i < results.size(); i++) { Object object = results.elementAt(i); shrinkingNodes.addElement(object); if (object instanceof Text) { elementContentInResult = true; } if (object instanceof Attribute) { attributeInResult = true; } } } XmlElementNode node = new XmlElementNode(this, (XElement) document.getLastRoot(), results, shrinkingNodes, getDeclaredNamespaces(), true, showAll); time = System.currentTimeMillis() - time; // System.out.println( "Tree Build ["+time+"]"); // System.out.println( "Showing Tree!"); tree.setRoot(node); tree.expand(3); forceSelectedElement(xngreditor.getView().getSelectedElement()); // System.out.println( "Tree Shown!"); } else { tree.setRoot(null); } } else { tree.setRoot(null); } hasLatest = true; }
From source file:com.cladonia.xml.navigator.Navigator.java
License:Open Source License
private Vector getDeclaredNamespaces() { Vector namespaces = new Vector(); Vector uris = new Vector(); Vector namespaceList = document.getDeclaredNamespaces(); for (int i = 0; i < namespaceList.size(); i++) { Namespace ns = (Namespace) namespaceList.elementAt(i); if (!uris.contains(ns.getURI())) { uris.addElement(ns.getURI()); namespaces.addElement(ns);// www . j a v a2s .co m } } return namespaces; }
From source file:com.cladonia.xml.XMLFormatter.java
License:Mozilla Public License
protected boolean isNamespaceDeclaration(Namespace ns) { if (DEBUG)//from ww w. j a va 2 s . c om System.out.println("XMLFormatter.isNamespaceDeclaration( " + ns + ")"); // if (indentMixed) { // return super.isNamespaceDeclaration( ns); // } else if (ns != null && ns != Namespace.XML_NAMESPACE) { String uri = ns.getURI(); if (uri != null) { if (!namespaceStack.contains(ns)) { return true; } } // } return false; }
From source file:com.cladonia.xngreditor.actions.ToolsRemoveUnusedNSAction.java
License:Open Source License
private String removeUnusedNS(ExchangerDocument document) throws Exception { Vector allNamespaces = document.getDeclaredNamespaces(); if (allNamespaces.size() < 1) { MessageHandler.showError(parent, "Cannot Find Any Namespace Declarations", "Tools Remove Unused Namespace Error"); return (null); } else {//from w w w . j a va 2s.c om XElement e = (XElement) document.getRoot(); Namespace elementNamespace = e.getNamespace(); int index = findInList(elementNamespace, allNamespaces); if (index > -1) { allNamespaces.remove(index); } //sort out the attributes if (e.attributeCount() > 0) { XAttribute[] atts = e.getAttributes(); for (int cnt = 0; cnt < e.attributeCount(); ++cnt) { XAttribute a = atts[cnt]; Namespace attributeNamespace = a.getNamespace(); index = findInList(attributeNamespace, allNamespaces); if (index > -1) { allNamespaces.remove(index); } } } treeWalk(document.getRoot(), allNamespaces); } if (allNamespaces.size() == 0) { MessageHandler.showError(parent, "Cannot Find Any Unused Namespace Declarations", "Tools Remove Unused Namespace Error"); return (null); } else { boolean deleteAll = false; for (int cnt = 0; cnt < allNamespaces.size(); ++cnt) { // if the deleteAll flag is false, then ask the user about each individual object if (deleteAll == false) { //create the message for the user String message = "Are you sure you want to delete:\n "; Namespace temp = (Namespace) allNamespaces.get(cnt); if ((temp.getPrefix().length() > 0) && (temp.getPrefix() != null)) { message += "xmlns:" + temp.getPrefix() + "=" + temp.getURI(); } else { message += "xmlns=" + temp.getURI(); } //ask the question int questionResult = -1; if (allNamespaces.size() > 1) { questionResult = MessageHandler.showConfirmYesNoAll(parent, message); } else { questionResult = MessageHandler.showConfirm(parent, message); } //if the user answered All, don't do anything for now and delete them all later if (questionResult == MessageHandler.CONFIRM_ALL_OPTION) { if (!removeNamespaceDeclaration(document.getRoot(), (Namespace) allNamespaces.get(cnt))) { //then walk the tree treeWalk(document.getRoot(), (Namespace) allNamespaces.get(cnt)); } deleteAll = true; } //user choose to delete this object, remove it from the list else if (questionResult == JOptionPane.YES_OPTION) { if (!removeNamespaceDeclaration(document.getRoot(), (Namespace) allNamespaces.get(cnt))) { //then walk the tree treeWalk(document.getRoot(), (Namespace) allNamespaces.get(cnt)); } } } else { if (!removeNamespaceDeclaration(document.getRoot(), (Namespace) allNamespaces.get(cnt))) { //then walk the tree treeWalk(document.getRoot(), (Namespace) allNamespaces.get(cnt)); } } /* //System.out.println(allNamespaces.get(cnt)); int questionResult=-1; if(!deleteAll) { String message = "Are you sure you want to delete:\n "; Namespace temp = (Namespace)allNamespaces.get(cnt); if((temp.getPrefix().length()>0) && (temp.getPrefix()!=null)) { message += "xmlns:"+temp.getPrefix()+"="+temp.getURI(); } else { message += "xmlns="+temp.getURI(); } questionResult = MessageHandler.showConfirmYesNoAll(parent,message); if(questionResult==MessageHandler.CONFIRM_ALL_OPTION) deleteAll=true; if(questionResult==JOptionPane.YES_OPTION) { //delete these namespace declarations //first from the root if(!removeNamespaceDeclaration(document.getRoot(),(Namespace)allNamespaces.get(cnt))) { //then walk the tree treeWalk(document.getRoot(),(Namespace)allNamespaces.get(cnt)); } } }*/ } //now need to check if all was pressed after the first dialog was shown, //in which case need to act on the initial entries /*if(deleteAll) { for(int cnt=0;cnt<allNamespaces.size();++cnt) { if(!removeNamespaceDeclaration(document.getRoot(),(Namespace)allNamespaces.get(cnt))) { //then walk the tree treeWalk(document.getRoot(),(Namespace)allNamespaces.get(cnt)); } } }*/ document.update(); return (document.getText()); } }