List of usage examples for org.dom4j Namespace getURI
public String getURI()
From source file:architecture.common.xml.XmlWriter.java
License:Apache License
protected void writeNamespace(Namespace namespace) throws IOException { if (namespace != null) { writeNamespace(namespace.getPrefix(), namespace.getURI()); }// ww w.jav a 2 s . com }
From source file:architecture.common.xml.XmlWriter.java
License:Apache License
/** * Writes the attributes of the given element * *//* ww w . j a v a2 s . c om*/ 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); namespaceStack.push(ns); } } // If the attribute is a namespace declaration, check if we have // already // written that declaration elsewhere (if that's the case, it must // be // in the namespace stack String attName = attribute.getName(); if (attName.startsWith("xmlns:")) { String prefix = attName.substring(6); if (namespaceStack.getNamespaceForPrefix(prefix) == null) { String uri = attribute.getValue(); namespaceStack.push(prefix, uri); writeNamespace(prefix, uri); } } else if (attName.equals("xmlns")) { if (namespaceStack.getDefaultNamespace() == null) { String uri = attribute.getValue(); namespaceStack.push(null, uri); writeNamespace(null, uri); } } else { char quote = format.getAttributeQuoteCharacter(); writer.write(" "); writer.write(attribute.getQualifiedName()); writer.write("="); writer.write(quote); writeEscapeAttributeEntities(attribute.getValue()); writer.write(quote); } } }
From source file:architecture.common.xml.XmlWriter.java
License:Apache 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 av a 2 s .c o m*/ } } return false; }
From source file:architecture.ee.util.xml.XmlWriter.java
License:Apache License
/** Writes the attributes of the given element */* w ww .j a v a 2s . co m*/ */ 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); namespaceStack.push(ns); } } // If the attribute is a namespace declaration, check if we have already // written that declaration elsewhere (if that's the case, it must be // in the namespace stack String attName = attribute.getName(); if (attName.startsWith("xmlns:")) { String prefix = attName.substring(6); if (namespaceStack.getNamespaceForPrefix(prefix) == null) { String uri = attribute.getValue(); namespaceStack.push(prefix, uri); writeNamespace(prefix, uri); } } else if (attName.equals("xmlns")) { if (namespaceStack.getDefaultNamespace() == null) { String uri = attribute.getValue(); namespaceStack.push(null, uri); writeNamespace(null, uri); } } else { char quote = format.getAttributeQuoteCharacter(); writer.write(" "); writer.write(attribute.getQualifiedName()); writer.write("="); writer.write(quote); writeEscapeAttributeEntities(attribute.getValue()); writer.write(quote); } } }
From source file:com.buddycloud.channeldirectory.search.ChannelDirectoryComponent.java
License:Apache License
@Override protected IQ handleIQGet(IQ iq) throws Exception { LOGGER.debug("R: " + iq.toXML()); Element queryElement = iq.getElement().element("query"); if (queryElement == null) { return XMPPUtils.error(iq, "IQ does not contain query element.", LOGGER); }/*ww w. j a v a2 s. c o m*/ Namespace namespace = queryElement.getNamespace(); QueryHandler queryHandler = queryHandlers.get(namespace.getURI()); if (queryHandler == null) { return XMPPUtils.error(iq, "QueryHandler not found for namespace: " + namespace, LOGGER); } return queryHandler.handle(iq); }
From source file:com.christophermrossi.jpt.PageTemplateImpl.java
License:Open Source License
private void processElement(Element element, ContentHandler contentHandler, LexicalHandler lexicalHandler, Interpreter beanShell, Stack slotStack) throws SAXException, PageTemplateException, IOException { // Get attributes Expressions expressions = new Expressions(); AttributesImpl attributes = getAttributes(element, expressions); // Process instructions // evaluate expression if (expressions.evaluate != null) { Expression.evaluate(expressions.evaluate, beanShell); }//from www . ja va2 s . com // use macro if (expressions.useMacro != null) { processMacro(expressions.useMacro, element, contentHandler, lexicalHandler, beanShell, slotStack); return; } // fill slot if (expressions.defineSlot != null) { //System.err.println( "fill slot: " + expressions.defineSlot ); if (!slotStack.isEmpty()) { Map slots = (Map) slotStack.pop(); Slot slot = (Slot) slots.get(expressions.defineSlot); //System.err.println( "slot: " + slot ); if (slot != null) { slot.process(contentHandler, lexicalHandler, beanShell, slotStack); slotStack.push(slots); return; } // else { use content in macro } slotStack.push(slots); } else { throw new PageTemplateException("slot definition not allowed outside of macro"); } } // define if (expressions.define != null) { processDefine(expressions.define, beanShell); } // condition if (expressions.condition != null && !Expression.evaluateBoolean(expressions.condition, beanShell)) { // Skip this element (and children) return; } // repeat Loop loop = new Loop(expressions.repeat, beanShell); while (loop.repeat(beanShell)) { // content or replace Object jptContent = null; if (expressions.content != null) { jptContent = processContent(expressions.content, beanShell); } // attributes if (expressions.attributes != null) { processAttributes(attributes, expressions.attributes, beanShell); } // omit-tag boolean jptOmitTag = false; if (expressions.omitTag != null) { if (expressions.omitTag.equals("")) { jptOmitTag = true; } else { jptOmitTag = Expression.evaluateBoolean(expressions.omitTag, beanShell); } } // Declare element Namespace namespace = element.getNamespace(); if (!jptOmitTag) { contentHandler.startElement(namespace.getURI(), element.getName(), element.getQualifiedName(), attributes); } // Process content if (jptContent != null && jptContent != DEFAULT) { // Content for this element has been generated dynamically if (jptContent instanceof HTMLFragment) { HTMLFragment html = (HTMLFragment) jptContent; html.toXhtml(contentHandler, lexicalHandler); } // plain text else { char[] text = ((String) jptContent).toCharArray(); contentHandler.characters(text, 0, text.length); } } else { defaultContent(element, contentHandler, lexicalHandler, beanShell, slotStack); } // End element if (!jptOmitTag) { contentHandler.endElement(namespace.getURI(), element.getName(), element.getQualifiedName()); } } }
From source file:com.christophermrossi.jpt.PageTemplateImpl.java
License:Open Source License
private void defaultContent(Element element, ContentHandler contentHandler, LexicalHandler lexicalHandler, Interpreter beanShell, Stack slotStack) throws SAXException, PageTemplateException, IOException { // Use default template content for (Iterator i = element.nodeIterator(); i.hasNext();) { Node node = (Node) i.next(); switch (node.getNodeType()) { case Node.ELEMENT_NODE: processElement((Element) node, contentHandler, lexicalHandler, beanShell, slotStack); break; case Node.TEXT_NODE: char[] text = node.getText().toCharArray(); contentHandler.characters(text, 0, text.length); break; case Node.COMMENT_NODE: char[] comment = node.getText().toCharArray(); lexicalHandler.comment(comment, 0, comment.length); break; case Node.CDATA_SECTION_NODE: lexicalHandler.startCDATA(); char[] cdata = node.getText().toCharArray(); contentHandler.characters(cdata, 0, cdata.length); lexicalHandler.endCDATA();// ww w.j a v a 2s . c o m break; case Node.NAMESPACE_NODE: Namespace declared = (Namespace) node; //System.err.println( "Declared namespace: " + declared.getPrefix() + ":" + declared.getURI() ); namespaces.put(declared.getPrefix(), declared.getURI()); //if ( declared.getURI().equals( TAL_NAMESPACE_URI ) ) { // this.talNamespacePrefix = declared.getPrefix(); //} //else if (declared.getURI().equals( METAL_NAMESPACE_URI ) ) { // this.metalNamespacePrefix = declared.getPrefix(); //} break; case Node.ATTRIBUTE_NODE: // Already handled break; case Node.DOCUMENT_TYPE_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.PROCESSING_INSTRUCTION_NODE: default: //System.err.println( "WARNING: Node type not supported: " + node.getNodeTypeName() ); } } }
From source file:com.christophermrossi.jpt.PageTemplateImpl.java
License:Open Source License
AttributesImpl getAttributes(Element element, Expressions expressions) throws PageTemplateException { AttributesImpl attributes = new AttributesImpl(); for (Iterator i = element.attributeIterator(); i.hasNext();) { Attribute attribute = (Attribute) i.next(); Namespace namespace = attribute.getNamespace(); //String prefix = namespace.getPrefix(); //System.err.println( "attribute: name=" + attribute.getName() + "\t" + // "qualified name=" + attribute.getQualifiedName() + "\t" + // "ns prefix=" + namespace.getPrefix() + "\t" + // "ns uri=" + namespace.getURI() ); //String qualifiedName = attribute.getName(); //String name = qualifiedName; //if ( qualifiedName.startsWith( prefix + ":" ) ) { // name = qualifiedName.substring( prefix.length() + 1 ); //}//from w w w . ja v a2s. c o m String name = attribute.getName(); // Handle JPT attributes //if ( prefix.equals( talNamespacePrefix ) ) { if (TAL_NAMESPACE_URI.equals(namespace.getURI())) { // tal:define if (name.equals("define")) { expressions.define = attribute.getValue(); } // tal:condition else if (name.equals("condition")) { expressions.condition = attribute.getValue(); } // tal:repeat else if (name.equals("repeat")) { expressions.repeat = attribute.getValue(); } // tal:content else if (name.equals("content")) { expressions.content = attribute.getValue(); } // tal:replace else if (name.equals("replace")) { if (expressions.omitTag == null) { expressions.omitTag = ""; } expressions.content = attribute.getValue(); } // tal:attributes else if (name.equals("attributes")) { expressions.attributes = attribute.getValue(); } // tal:omit-tag else if (name.equals("omit-tag")) { expressions.omitTag = attribute.getValue(); } // tal:evaluate else if (name.equals("evaluate")) { expressions.evaluate = attribute.getValue(); } // error else { throw new PageTemplateException("unknown tal attribute: " + name); } } //else if ( prefix.equals( metalNamespacePrefix ) ) else if (METAL_NAMESPACE_URI.equals(namespace.getURI())) { // metal:use-macro if (name.equals("use-macro")) { expressions.useMacro = attribute.getValue(); } // metal:define-slot else if (name.equals("define-slot")) { expressions.defineSlot = attribute.getValue(); } // metal:define-macro // metal:fill-slot else if (name.equals("define-macro") || name.equals("fill-slot")) { // these are ignored here, as they don't affect processing of current // template, but are called from other templates } // error else { throw new PageTemplateException("unknown metal attribute: " + name); } } // Pass on all other attributes else { //String qualifiedName = namespace.getPrefix() + ":" + name; attributes.addAttribute(namespace.getURI(), name, attribute.getQualifiedName(), "CDATA", attribute.getValue()); //attributes.addAttribute( getNamespaceURIFromPrefix(prefix), name, qualifiedName, "CDATA", attribute.getValue() ); } } return attributes; }
From source file:com.christophermrossi.jpt.PageTemplateImpl.java
License:Open Source License
/** * With all of our namespace woes, getting an XPath expression * to work has proven futile, so we'll recurse through the tree * ourselves to find what we need.//from w w w. j a v a 2s . c o m */ private void findMacros(Element element, Map macros) { // Process any declared namespaces for (Iterator i = element.declaredNamespaces().iterator(); i.hasNext();) { Namespace namespace = (Namespace) i.next(); namespaces.put(namespace.getPrefix(), namespace.getURI()); //if ( namespace.getURI().equals( TAL_NAMESPACE_URI ) ) { // this.talNamespacePrefix = namespace.getPrefix(); //} //else if ( namespace.getURI().equals( METAL_NAMESPACE_URI ) ) { // this.metalNamespacePrefix = namespace.getPrefix(); //} } // Look for our attribute //String qualifiedAttributeName = this.metalNamespacePrefix + ":define-macro"; //String name = element.attributeValue( qualifiedAttributeName ); String name = element.attributeValue("define-macro"); //if ( name == null ) { // name = element.attributeValue // ( new QName( "define-macro", new Namespace( metalNamespacePrefix, METAL_NAMESPACE_URI ) ) ); //} if (name != null) { macros.put(name, new MacroImpl(element)); } // Recurse into child elements for (Iterator i = element.elementIterator(); i.hasNext();) { findMacros((Element) i.next(), macros); } }
From source file:com.cladonia.schema.dtd.DTDDocument.java
License:Open Source License
public void updatePrefixes(Vector declarations) { Vector allElements = getElements(); 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); // // for ( int k = 0; k < declarations.size(); k++) { // Namespace ns = (Namespace)declarations.elementAt(k); ////w w w . j a v a 2s .c o m // if ( ns.getURI().equals( child.getNamespace())) {// && ns.getPrefix() != null && ns.getPrefix().trim().length() > 0) { // child.setPrefix( ns.getPrefix()); // break; // } // } // } // } 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; } } } }