@Override public void relink_namespace(ThreadContext context) { Element e = (Element) node; e.getOwnerDocument().renameNode(e, e.lookupNamespaceURI(e.getPrefix()), e.getNodeName()); if (e.hasAttributes()) { NamedNodeMap attrs = e.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr = (Attr) attrs.item(i); String nsUri = ""; String prefix = attr.getPrefix(); String nodeName = attr.getNodeName(); if ("xml".equals(prefix)) { nsUri = "http://www.w3.org/XML/1998/namespace"; } else if ("xmlns".equals(prefix) || nodeName.equals("xmlns")) { nsUri = "http://www.w3.org/2000/xmlns/"; } else { nsUri = attr.lookupNamespaceURI(nodeName); } e.getOwnerDocument().renameNode(attr, nsUri, nodeName); } } if (e.hasChildNodes()) { ((XmlNodeSet) children(context)).relink_namespace(context); } }
private void handleNsAttribute( BpelEntity entity, BpelEntity parent, Map<String, String> prefixMap, Attr attr) { String namespaceURI = attr.getValue(); if (XMLNS.equals(attr.getName()) || (XMLNS.equals(attr.getPrefix()))) { if (namespaceURI.equals(entity.getPeer().getNamespaceURI())) { // do not touch namespace that correspond namespace of current element return; } ExNamespaceContext context = parent.getNamespaceContext(); Iterator<String> iterator = context.getPrefixes(); while (iterator.hasNext()) { String next = iterator.next(); String namespace = context.getNamespaceURI(next); if (namespaceURI.equals(namespace)) { String prefixName = attr.getLocalName(); // put prefix corresponding found namespace into map for changing it further if (!prefixName.equals(next)) { prefixMap.put(prefixName, next); } // remove namespace delcaration. ((BpelEntityImpl) entity) .setAttribute(attr.getName(), new PrefixAttribute(attr.getName()), null); } } } }
private void moveNSDeclarationToRoot(BpelEntity entity, Map<String, String> prefixMap) { if (!(entity instanceof BpelEntityImpl)) { return; } Element element = ((BpelEntityImpl) entity).getPeer(); NamedNodeMap map = element.getAttributes(); for (int i = 0; i < map.getLength(); i++) { Node node = map.item(i); assert node instanceof Attr; Attr attr = (Attr) node; if (attr.getValue() == null) { continue; } try { if (XMLNS.equals(attr.getName()) || (XMLNS.equals(attr.getPrefix()))) { SingletonMap pMap = moveNsDeclaration(entity, attr); if (!pMap.keyEqualsValue()) { prefixMap.put(pMap.getKey(), pMap.getValue()); } } } catch (InvalidNamespaceException e) { // This is the case // when namespace was originally incorrect and we do not move it // to root. We don't do anything. } } /* * we need to update prefixes for those declarations that changed * its perfix after lifing up decl. */ updatePrefixes(entity, prefixMap); for (BpelEntity child : entity.getChildren()) { moveNSDeclarationToRoot(child, prefixMap); } }
private void addNamespaces(Namespaces rv, Element element) { if (element == null) throw new RuntimeException("element must not be null"); String myDefaultNamespace = toUri(element.lookupNamespaceURI(null)); String parentDefaultNamespace = ""; if (element.getParentNode() != null) { parentDefaultNamespace = toUri(element.getParentNode().lookupNamespaceURI(null)); } if (!myDefaultNamespace.equals(parentDefaultNamespace) || !(element.getParentNode() instanceof Element)) { rv.declare(Namespace.create("", myDefaultNamespace)); } NamedNodeMap attributes = element.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); if (attr.getPrefix() != null && attr.getPrefix().equals("xmlns")) { rv.declare(Namespace.create(attr.getLocalName(), attr.getValue())); } } }
/** * Receive notification of a node. * * @param node The Node to be added to the document * @param namespaceResolver The NamespaceResolver can be used to resolve the namespace URI/prefix * of the node */ public void node( Node node, NamespaceResolver namespaceResolver, String newNamespace, String newName) { if (node.getNodeType() == Node.ATTRIBUTE_NODE) { Attr attr = (Attr) node; String resolverPfx = null; if (getNamespaceResolver() != null) { resolverPfx = this.getNamespaceResolver().resolveNamespaceURI(attr.getNamespaceURI()); } String namespaceURI = attr.getNamespaceURI(); // If the namespace resolver contains a prefix for the attribute's URI, // use it instead of what is set on the attribute if (resolverPfx != null) { attribute( attr.getNamespaceURI(), Constants.EMPTY_STRING, resolverPfx + Constants.COLON + attr.getLocalName(), attr.getNodeValue()); } else { attribute( attr.getNamespaceURI(), Constants.EMPTY_STRING, attr.getName(), attr.getNodeValue()); // May need to declare the URI locally if (attr.getNamespaceURI() != null) { namespaceDeclaration(attr.getPrefix(), attr.getNamespaceURI()); this.getNamespaceResolver().put(attr.getPrefix(), attr.getNamespaceURI()); } } } else if (node.getNodeType() == Node.TEXT_NODE) { characters(node.getNodeValue()); } else { try { WriterRecordContentHandler wrcHandler = new WriterRecordContentHandler(); XMLFragmentReader xfragReader = new XMLFragmentReader(namespaceResolver); xfragReader.setContentHandler(wrcHandler); xfragReader.setProperty("http://xml.org/sax/properties/lexical-handler", wrcHandler); xfragReader.parse(node, newNamespace, newName); } catch (SAXException sex) { throw XMLMarshalException.marshalException(sex); } } }
public QName getAttributeName(int i) { Attr at = getAttribute(i); String prefix = at.getPrefix(); String ln = getLocalName(at); // at.getNodeName(); String ns = at.getNamespaceURI(); if (prefix == null) { return new QName(ns, ln); } else { return new QName(ns, ln, prefix); } }
@Override public AttributeNode convert(Attr attr) { AttributeBuilder attributeBuilder = NodeBuilderFactory.newAttributeBuilder(); AttributeNode attributeNode = null; attributeNode = attributeBuilder // .nodeName(attr.getNodeName()) // .namespaceURI(attr.getNamespaceURI()) // .prefix(attr.getPrefix()) // .localPart(attr.getLocalName()) // .qualifiedName(null) // TODO .name(attr.getName()) // .value(attr.getValue()) // .build(); return attributeNode; }
private void unmarshalParams(Element curXPathElem) throws MarshalException { List<XPathType> list = new ArrayList<XPathType>(); while (curXPathElem != null) { String xPath = curXPathElem.getFirstChild().getNodeValue(); String filterVal = DOMUtils.getAttributeValue(curXPathElem, "Filter"); if (filterVal == null) { throw new MarshalException("filter cannot be null"); } XPathType.Filter filter = null; if (filterVal.equals("intersect")) { filter = XPathType.Filter.INTERSECT; } else if (filterVal.equals("subtract")) { filter = XPathType.Filter.SUBTRACT; } else if (filterVal.equals("union")) { filter = XPathType.Filter.UNION; } else { throw new MarshalException("Unknown XPathType filter type" + filterVal); } NamedNodeMap attributes = curXPathElem.getAttributes(); if (attributes != null) { int length = attributes.getLength(); Map<String, String> namespaceMap = new HashMap<String, String>(length); for (int i = 0; i < length; i++) { Attr attr = (Attr) attributes.item(i); String prefix = attr.getPrefix(); if (prefix != null && prefix.equals("xmlns")) { namespaceMap.put(attr.getLocalName(), attr.getValue()); } } list.add(new XPathType(xPath, filter, namespaceMap)); } else { list.add(new XPathType(xPath, filter)); } curXPathElem = DOMUtils.getNextSiblingElement(curXPathElem); } this.params = new XPathFilter2ParameterSpec(list); }
public static Map<QName, String> getAttributes(Node node) { Map<QName, String> atts = new HashMap<QName, String>(); NamedNodeMap nnm = node.getAttributes(); if (nnm != null) { for (int i = 0; i < nnm.getLength(); i++) { Attr att = (Attr) nnm.item(i); String uri = att.getBaseURI(); String localname = att.getLocalName(); String prefix = att.getPrefix(); QName name; if (uri == null) { name = new QName(localname); } else if (prefix == null) { name = new QName(uri, localname); } else { name = new QName(uri, localname, prefix); } if (prefix == null || !(prefix.equals("xmlns") || prefix.equals("xml"))) { atts.put(name, att.getValue()); } } } return atts; }
/** * Called to serialize a DOM element. Equivalent to calling {@link #startElement}, {@link * #endElement} and serializing everything inbetween, but better optimized. */ protected void serializeElement(Element elem) throws IOException { Attr attr; NamedNodeMap attrMap; int i; Node child; ElementState state; String name; String value; String tagName; String prefix, localUri; String uri; if (fNamespaces) { // local binder stores namespace declaration // that has been printed out during namespace fixup of // the current element fLocalNSBinder.reset(); // add new namespace context fNSBinder.pushContext(); } if (DEBUG) { System.out.println( "==>startElement: " + elem.getNodeName() + " ns=" + elem.getNamespaceURI()); } tagName = elem.getTagName(); state = getElementState(); if (isDocumentState()) { // If this is the root element handle it differently. // If the first root element in the document, serialize // the document's DOCTYPE. Space preserving defaults // to that of the output format. if (!_started) { startDocument(tagName); } } else { // For any other element, if first in parent, then // close parent's opening tag and use the parent's // space preserving. if (state.empty) _printer.printText('>'); // Must leave CData section first if (state.inCData) { _printer.printText("]]>"); state.inCData = false; } // Indent this element on a new line if the first // content of the parent element or immediately // following an element. if (_indenting && !state.preserveSpace && (state.empty || state.afterElement || state.afterComment)) _printer.breakLine(); } // Do not change the current element state yet. // This only happens in endElement(). fPreserveSpace = state.preserveSpace; int length = 0; attrMap = null; // retrieve attributes if (elem.hasAttributes()) { attrMap = elem.getAttributes(); length = attrMap.getLength(); } if (!fNamespaces) { // no namespace fixup should be performed // serialize element name _printer.printText('<'); _printer.printText(tagName); _printer.indent(); // For each attribute print it's name and value as one part, // separated with a space so the element can be broken on // multiple lines. for (i = 0; i < length; ++i) { attr = (Attr) attrMap.item(i); name = attr.getName(); value = attr.getValue(); if (value == null) value = ""; printAttribute(name, value, attr.getSpecified(), attr); } } else { // do namespace fixup // REVISIT: some optimization could probably be done to avoid traversing // attributes twice. // // --------------------------------------- // record all valid namespace declarations // before attempting to fix element's namespace // --------------------------------------- for (i = 0; i < length; i++) { attr = (Attr) attrMap.item(i); uri = attr.getNamespaceURI(); // check if attribute is a namespace decl if (uri != null && uri.equals(NamespaceContext.XMLNS_URI)) { value = attr.getNodeValue(); if (value == null) { value = XMLSymbols.EMPTY_STRING; } if (value.equals(NamespaceContext.XMLNS_URI)) { if (fDOMErrorHandler != null) { String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.XML_DOMAIN, "CantBindXMLNS", null); modifyDOMError(msg, DOMError.SEVERITY_ERROR, null, attr); boolean continueProcess = fDOMErrorHandler.handleError(fDOMError); if (!continueProcess) { // stop the namespace fixup and validation throw new RuntimeException( DOMMessageFormatter.formatMessage( DOMMessageFormatter.SERIALIZER_DOMAIN, "SerializationStopped", null)); } } } else { prefix = attr.getPrefix(); prefix = (prefix == null || prefix.length() == 0) ? XMLSymbols.EMPTY_STRING : fSymbolTable.addSymbol(prefix); String localpart = fSymbolTable.addSymbol(attr.getLocalName()); if (prefix == XMLSymbols.PREFIX_XMLNS) { // xmlns:prefix value = fSymbolTable.addSymbol(value); // record valid decl if (value.length() != 0) { fNSBinder.declarePrefix(localpart, value); } else { // REVISIT: issue error on invalid declarations // xmlns:foo = "" } continue; } else { // xmlns // empty prefix is always bound ("" or some string) value = fSymbolTable.addSymbol(value); fNSBinder.declarePrefix(XMLSymbols.EMPTY_STRING, value); continue; } } // end-else: valid declaration } // end-if: namespace declaration } // end-for // ----------------------- // get element uri/prefix // ----------------------- uri = elem.getNamespaceURI(); prefix = elem.getPrefix(); // ---------------------- // output element name // ---------------------- // REVISIT: this could be removed if we always convert empty string to null // for the namespaces. if ((uri != null && prefix != null) && uri.length() == 0 && prefix.length() != 0) { // uri is an empty string and element has some prefix // the namespace alg later will fix up the namespace attributes // remove element prefix prefix = null; _printer.printText('<'); _printer.printText(elem.getLocalName()); _printer.indent(); } else { _printer.printText('<'); _printer.printText(tagName); _printer.indent(); } // --------------------------------------------------------- // Fix up namespaces for element: per DOM L3 // Need to consider the following cases: // // case 1: <foo:elem xmlns:ns1="myURI" xmlns="default"/> // Assume "foo", "ns1" are declared on the parent. We should not miss // redeclaration for both "ns1" and default namespace. To solve this // we add a local binder that stores declaration only for current element. // This way we avoid outputing duplicate declarations for the same element // as well as we are not omitting redeclarations. // // case 2: <elem xmlns="" xmlns="default"/> // We need to bind default namespace to empty string, to be able to // omit duplicate declarations for the same element // // case 3: <xsl:stylesheet xmlns:xsl="http://xsl"> // We create another element body bound to the "http://xsl" namespace // as well as namespace attribute rebounding xsl to another namespace. // <xsl:body xmlns:xsl="http://another"> // Need to make sure that the new namespace decl value is changed to // "http://xsl" // // --------------------------------------------------------- // check if prefix/namespace is correct for current element // --------------------------------------------------------- if (uri != null) { // Element has a namespace uri = fSymbolTable.addSymbol(uri); prefix = (prefix == null || prefix.length() == 0) ? XMLSymbols.EMPTY_STRING : fSymbolTable.addSymbol(prefix); if (fNSBinder.getURI(prefix) == uri) { // The xmlns:prefix=namespace or xmlns="default" was declared at parent. // The binder always stores mapping of empty prefix to "". // (NOTE: local binder does not store this kind of binding!) // Thus the case where element was declared with uri="" (with or without a prefix) // will be covered here. } else { // the prefix is either undeclared // or // conflict: the prefix is bound to another URI if (fNamespacePrefixes) { printNamespaceAttr(prefix, uri); } fLocalNSBinder.declarePrefix(prefix, uri); fNSBinder.declarePrefix(prefix, uri); } } else { // Element has no namespace if (elem.getLocalName() == null) { // DOM Level 1 node! if (fDOMErrorHandler != null) { String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "NullLocalElementName", new Object[] {elem.getNodeName()}); modifyDOMError(msg, DOMError.SEVERITY_ERROR, null, elem); boolean continueProcess = fDOMErrorHandler.handleError(fDOMError); // REVISIT: should we terminate upon request? if (!continueProcess) { throw new RuntimeException( DOMMessageFormatter.formatMessage( DOMMessageFormatter.SERIALIZER_DOMAIN, "SerializationStopped", null)); } } } else { // uri=null and no colon (DOM L2 node) uri = fNSBinder.getURI(XMLSymbols.EMPTY_STRING); if (uri != null && uri.length() > 0) { // there is a default namespace decl that is bound to // non-zero length uri, output xmlns="" if (fNamespacePrefixes) { printNamespaceAttr(XMLSymbols.EMPTY_STRING, XMLSymbols.EMPTY_STRING); } fLocalNSBinder.declarePrefix(XMLSymbols.EMPTY_STRING, XMLSymbols.EMPTY_STRING); fNSBinder.declarePrefix(XMLSymbols.EMPTY_STRING, XMLSymbols.EMPTY_STRING); } } } // ----------------------------------------- // Fix up namespaces for attributes: per DOM L3 // check if prefix/namespace is correct the attributes // ----------------------------------------- for (i = 0; i < length; i++) { attr = (Attr) attrMap.item(i); value = attr.getValue(); name = attr.getNodeName(); uri = attr.getNamespaceURI(); // Fix attribute that was declared with a prefix and namespace="" if (uri != null && uri.length() == 0) { uri = null; // we must remove prefix for this attribute name = attr.getLocalName(); } if (DEBUG) { System.out.println("==>process attribute: " + attr.getNodeName()); } // make sure that value is never null. if (value == null) { value = XMLSymbols.EMPTY_STRING; } if (uri != null) { // attribute has namespace !=null prefix = attr.getPrefix(); prefix = prefix == null ? XMLSymbols.EMPTY_STRING : fSymbolTable.addSymbol(prefix); String localpart = fSymbolTable.addSymbol(attr.getLocalName()); // --------------------------------------------------- // print namespace declarations namespace declarations // --------------------------------------------------- if (uri != null && uri.equals(NamespaceContext.XMLNS_URI)) { // check if we need to output this declaration prefix = attr.getPrefix(); prefix = (prefix == null || prefix.length() == 0) ? XMLSymbols.EMPTY_STRING : fSymbolTable.addSymbol(prefix); localpart = fSymbolTable.addSymbol(attr.getLocalName()); if (prefix == XMLSymbols.PREFIX_XMLNS) { // xmlns:prefix localUri = fLocalNSBinder.getURI(localpart); // local prefix mapping value = fSymbolTable.addSymbol(value); if (value.length() != 0) { if (localUri == null) { // declaration was not printed while fixing element namespace binding // If the DOM Level 3 namespace-prefixes feature is set to false // do not print xmlns attributes if (fNamespacePrefixes) { printNamespaceAttr(localpart, value); } // case 4: <elem xmlns:xx="foo" xx:attr=""/> // where attribute is bound to "bar". // If the xmlns:xx is output here first, later we should not // redeclare "xx" prefix. Instead we would pick up different prefix // for the attribute. // final: <elem xmlns:xx="foo" NS1:attr="" xmlns:NS1="bar"/> fLocalNSBinder.declarePrefix(localpart, value); } } else { // REVISIT: issue error on invalid declarations // xmlns:foo = "" } continue; } else { // xmlns // empty prefix is always bound ("" or some string) uri = fNSBinder.getURI(XMLSymbols.EMPTY_STRING); localUri = fLocalNSBinder.getURI(XMLSymbols.EMPTY_STRING); value = fSymbolTable.addSymbol(value); if (localUri == null) { // declaration was not printed while fixing element namespace binding if (fNamespacePrefixes) { printNamespaceAttr(XMLSymbols.EMPTY_STRING, value); } // case 4 does not apply here since attributes can't use // default namespace } continue; } } uri = fSymbolTable.addSymbol(uri); // find if for this prefix a URI was already declared String declaredURI = fNSBinder.getURI(prefix); if (prefix == XMLSymbols.EMPTY_STRING || declaredURI != uri) { // attribute has no prefix (default namespace decl does not apply to attributes) // OR // attribute prefix is not declared // OR // conflict: attr URI does not match the prefix in scope name = attr.getNodeName(); // Find if any prefix for attributes namespace URI is available // in the scope String declaredPrefix = fNSBinder.getPrefix(uri); if (declaredPrefix != null && declaredPrefix != XMLSymbols.EMPTY_STRING) { // use the prefix that was found prefix = declaredPrefix; name = prefix + ":" + localpart; } else { if (DEBUG) { System.out.println("==> cound not find prefix for the attribute: " + prefix); } if (prefix != XMLSymbols.EMPTY_STRING && fLocalNSBinder.getURI(prefix) == null) { // the current prefix is not null and it has no in scope declaration // use this prefix } else { // find a prefix following the pattern "NS" +index (starting at 1) // make sure this prefix is not declared in the current scope. int counter = 1; prefix = fSymbolTable.addSymbol(PREFIX + counter++); while (fLocalNSBinder.getURI(prefix) != null) { prefix = fSymbolTable.addSymbol(PREFIX + counter++); } name = prefix + ":" + localpart; } // add declaration for the new prefix if (fNamespacePrefixes) { printNamespaceAttr(prefix, uri); } value = fSymbolTable.addSymbol(value); fLocalNSBinder.declarePrefix(prefix, value); fNSBinder.declarePrefix(prefix, uri); } // change prefix for this attribute } printAttribute( name, (value == null) ? XMLSymbols.EMPTY_STRING : value, attr.getSpecified(), attr); } else { // attribute uri == null if (attr.getLocalName() == null) { if (fDOMErrorHandler != null) { String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "NullLocalAttrName", new Object[] {attr.getNodeName()}); modifyDOMError(msg, DOMError.SEVERITY_ERROR, null, attr); boolean continueProcess = fDOMErrorHandler.handleError(fDOMError); if (!continueProcess) { // stop the namespace fixup and validation throw new RuntimeException( DOMMessageFormatter.formatMessage( DOMMessageFormatter.SERIALIZER_DOMAIN, "SerializationStopped", null)); } } printAttribute(name, value, attr.getSpecified(), attr); } else { // uri=null and no colon // no fix up is needed: default namespace decl does not // apply to attributes printAttribute(name, value, attr.getSpecified(), attr); } } } // end loop for attributes } // end namespace fixup algorithm // If element has children, then serialize them, otherwise // serialize en empty tag. if (elem.hasChildNodes()) { // Enter an element state, and serialize the children // one by one. Finally, end the element. state = enterElementState(null, null, tagName, fPreserveSpace); state.doCData = _format.isCDataElement(tagName); state.unescaped = _format.isNonEscapingElement(tagName); child = elem.getFirstChild(); while (child != null) { serializeNode(child); child = child.getNextSibling(); } if (fNamespaces) { fNSBinder.popContext(); } endElementIO(null, null, tagName); } else { if (DEBUG) { System.out.println("==>endElement: " + elem.getNodeName()); } if (fNamespaces) { fNSBinder.popContext(); } _printer.unindent(); _printer.printText("/>"); // After element but parent element is no longer empty. state.afterElement = true; state.afterComment = false; state.empty = false; if (isDocumentState()) _printer.flush(); } }
@Override protected void doParse(Element element, ParserContext ctx, BeanDefinitionBuilder bean) { boolean isAbstract = false; boolean publish = true; NamedNodeMap atts = element.getAttributes(); String bus = element.getAttribute("bus"); if (StringUtils.isEmpty(bus)) { addBusWiringAttribute(bean, BusWiringType.CONSTRUCTOR); } else { bean.addConstructorArgReference(bus); } for (int i = 0; i < atts.getLength(); i++) { Attr node = (Attr) atts.item(i); String val = node.getValue(); String pre = node.getPrefix(); String name = node.getLocalName(); if ("createdFromAPI".equals(name)) { bean.setAbstract(true); isAbstract = true; } else if (isAttribute(pre, name) && !"publish".equals(name) && !"bus".equals(name)) { if ("endpointName".equals(name) || "serviceName".equals(name)) { QName q = parseQName(element, val); bean.addPropertyValue(name, q); } else if ("depends-on".equals(name)) { bean.addDependsOn(val); } else if (IMPLEMENTOR.equals(name)) { loadImplementor(bean, val); } else if (!"name".equals(name)) { mapToProperty(bean, name, val); } } else if ("abstract".equals(name)) { bean.setAbstract(true); isAbstract = true; } else if ("publish".equals(name)) { publish = "true".equals(val); } } Element elem = DOMUtils.getFirstElement(element); while (elem != null) { String name = elem.getLocalName(); if ("properties".equals(name)) { Map<?, ?> map = ctx.getDelegate().parseMapElement(elem, bean.getBeanDefinition()); bean.addPropertyValue("properties", map); } else if ("binding".equals(name)) { setFirstChildAsProperty(elem, ctx, bean, "bindingConfig"); } else if ("inInterceptors".equals(name) || "inFaultInterceptors".equals(name) || "outInterceptors".equals(name) || "outFaultInterceptors".equals(name) || "features".equals(name) || "schemaLocations".equals(name) || "handlers".equals(name)) { List<?> list = ctx.getDelegate().parseListElement(elem, bean.getBeanDefinition()); bean.addPropertyValue(name, list); } else if (IMPLEMENTOR.equals(name)) { ctx.getDelegate().parseConstructorArgElement(elem, bean.getBeanDefinition()); } else { setFirstChildAsProperty(elem, ctx, bean, name); } elem = DOMUtils.getNextElement(elem); } if (!isAbstract) { if (publish) { bean.setInitMethodName("publish"); } bean.setDestroyMethodName("stop"); } // We don't want to delay the registration of our Server bean.setLazyInit(false); }
public SequenceIterator iterate(XPathContext context) throws XPathException { Controller controller = context.getController(); ObjectValue ov = (ObjectValue) controller.getParameter("{" + Test.TE_NS + "}core"); TECore core = (TECore) ov.getObject(); Expression[] argExpressions = getArguments(); String xml = "<params>\n"; List<QName> params = fe.getParams(); for (int i = 0; i < params.size(); i++) { QName param = params.get(i); xml += "<param"; xml += " local-name=\"" + param.getLocalPart() + "\""; xml += " namespace-uri=\"" + param.getNamespaceURI() + "\""; xml += " prefix=\"" + param.getPrefix() + "\""; ValueRepresentation vr = ExpressionTool.eagerEvaluate(argExpressions[i], context); // ValueRepresentation vr = // ExpressionTool.lazyEvaluate(argExpressions[i], context, 1); Value v = Value.asValue(vr); try { Node n = (Node) v.convertToJava(Node.class, context); int type = n.getNodeType(); if (type == Node.ATTRIBUTE_NODE) { xml += ">\n"; Attr attr = (Attr) n; xml += "<value " + attr.getNodeName() + "=\"" + attr.getValue().replace("&", "&") + "\""; if (attr.getPrefix() != null) { xml += " xmlns:" + attr.getPrefix() + "=\"" + attr.getNamespaceURI() + "\""; } xml += "/>\n"; // } else if (type == Node.ELEMENT_NODE || type == // Node.DOCUMENT_NODE) { // xml += ">\n"; // xml += "<value>"; // xml += DomUtils.serializeNode(n); // xml += "</value>\n"; } else if (type == Node.ELEMENT_NODE) { xml += " type=\"node()\">\n"; xml += "<value>"; xml += DomUtils.serializeNode(n); xml += "</value>\n"; } else if (type == Node.DOCUMENT_NODE) { xml += " type=\"document-node()\">\n"; xml += "<value>"; xml += DomUtils.serializeNode(n); xml += "</value>\n"; } else { ItemType it = v.getItemType(context.getConfiguration().getTypeHierarchy()); xml += " type=\"" + getTypeName(it) + "\">\n"; xml += "<value>" + n.getNodeValue() + "</value>\n"; } } catch (Exception e) { ItemType it = v.getItemType(context.getConfiguration().getTypeHierarchy()); xml += " type=\"" + getTypeName(it) + "\">\n"; xml += "<value>" + v.getStringValue() + "</value>\n"; } xml += "</param>\n"; } xml += "</params>"; // System.out.println(xml); Source src = new StreamSource(new CharArrayReader(xml.toCharArray())); // XdmValue result = null; NodeInfo result = null; try { // result = core.executeTemplate(fe, Globals.builder.build(src), // context); NodeInfo paramsNode = core.getEngine().getBuilder().build(src).getUnderlyingNode(); result = core.executeXSLFunction(context, fe, paramsNode); } catch (Exception e) { throw new RuntimeException(e); } if (result == null) { return EmptyIterator.getInstance(); } else { // Value v = Value.asValue(result); // return v.iterate(); return result.iterateAxis(Axis.CHILD); } }
private void write(Writer writer, NSStack nsstack, Node node, int depth) throws IOException { nsstack = new NSStack(nsstack); if (node.getPrefix() != null) { String nsuri = nsstack.getNamespace(node.getPrefix()); if (nsuri == null) { nsstack.addNamespace(node.getPrefix(), node.getNamespaceURI()); } } writer.write(pad(depth)); writer.write("<" + node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); if (attrs != null) { writer.write(" "); // writer.write("\n"); for (int i = 0; i < attrs.getLength(); i++) { Attr attr = (Attr) attrs.item(i); if (attr.getPrefix() != null) { String nsuri = nsstack.getNamespace(attr.getPrefix()); if (nsuri == null) { nsstack.addNamespace(attr.getPrefix(), attr.getNamespaceURI()); } } else if (attr.getNodeName().startsWith("xmlns:")) { String prefix = attr.getNodeName().substring(6); String nsuri = attr.getNodeValue(); nsstack.addNamespace(prefix, nsuri); } String value = attr.getValue(); // writer.write(pad(depth+1)); writer.write(attr.getName() + "='" + value + "'"); if (i < attrs.getLength()) writer.write(" "); // writer.write("\n"); if (value != null && value.indexOf(":") > -1 && value.indexOf("://") == -1) { String prefix = value.substring(0, value.indexOf(":")); String nsuri = nsstack.getNamespace(prefix); if (nsuri == null) { System.err.println("WARNING: Use of undeclared namespace prefix : " + prefix); } } } // writer.write(pad(depth)); } Map<String, String> namespaces = nsstack.getNamespaces(); if (namespaces != null) { Iterator<String> nsit = namespaces.keySet().iterator(); while (nsit.hasNext()) { String prefix = nsit.next(); String nsuri = namespaces.get(prefix); Attr attr = (Attr) node.getAttributes().getNamedItem("xmlns:" + prefix); if (attr == null) { // writer.write(pad(depth+1)); writer.write("xmlns:" + prefix + "='" + nsuri + "'"); // writer.write("\n"); } } // writer.write(pad(depth)); } writer.write(">"); NodeList children = node.getChildNodes(); if (children.getLength() == 1 && children.item(0).getNodeType() == Node.TEXT_NODE) { Node text = children.item(0); writer.write(text.getNodeValue()); } else if (children.getLength() > 0) { writer.write("\n"); for (int i = 0; i < children.getLength(); i++) { write(writer, nsstack, children.item(i), depth + 1); } writer.write(pad(depth)); } writer.write("</" + node.getNodeName() + ">\n"); }