List of usage examples for javax.xml.namespace QName getPrefix
public String getPrefix()
Get the prefix of this QName
.
The prefix assigned to a QName
might NOT be valid in a different context.
From source file:org.apache.hadoop.gateway.filter.rewrite.impl.xml.XmlFilterReader.java
private void processEndElement(EndElement event) throws XPathExpressionException, IOException { //System.out.println( "EE=" + event ); boolean buffering = currentlyBuffering(); Level child = stack.pop();/*ww w . jav a2 s. c o m*/ if (buffering) { if (child.node == child.scopeNode) { processBufferedElement(child); } } else { if (!isEmptyElement) { QName n = event.getName(); writer.write("</"); String p = n.getPrefix(); if (p != null && !p.isEmpty()) { writer.write(p); writer.write(":"); } writer.write(n.getLocalPart()); writer.write(">"); } child.node.getParentNode().removeChild(child.node); } }
From source file:org.apache.hadoop.gateway.filter.rewrite.impl.xml.XmlFilterReader.java
private Element bufferElement(StartElement event) { QName qname = event.getName(); String prefix = qname.getPrefix(); String uri = qname.getNamespaceURI(); Element element;/* w ww .jav a 2 s . c o m*/ if (uri == null || uri.isEmpty()) { element = document.createElement(qname.getLocalPart()); } else { element = document.createElementNS(qname.getNamespaceURI(), qname.getLocalPart()); if (prefix != null && !prefix.isEmpty()) { element.setPrefix(prefix); } } // Always need to buffer the namespaces regardless of what else happens so that XPath will work on attributes // namespace qualified attributes. bufferNamespaces(event, element); return element; }
From source file:org.apache.hadoop.gateway.filter.rewrite.impl.xml.XmlFilterReader.java
private Attr bufferAttribute(Element element, Attribute attribute) { QName name = attribute.getName(); String prefix = name.getPrefix(); String uri = name.getNamespaceURI(); Attr node;/*from w w w.j a v a 2s. c o m*/ if (uri == null || uri.isEmpty()) { node = document.createAttribute(name.getLocalPart()); element.setAttributeNode(node); } else { node = document.createAttributeNS(uri, name.getLocalPart()); if (prefix != null && !prefix.isEmpty()) { node.setPrefix(prefix); } element.setAttributeNodeNS(node); } node.setTextContent(attribute.getValue()); return node; }
From source file:org.apache.hadoop.gateway.filter.rewrite.impl.xml.XmlFilterReader.java
private void streamAttribute(Element element, Attribute attribute) throws XPathExpressionException { Attr node;//from www . j a va 2 s . c o m QName name = attribute.getName(); String prefix = name.getPrefix(); String uri = name.getNamespaceURI(); if (uri == null || uri.isEmpty()) { node = document.createAttribute(name.getLocalPart()); element.setAttributeNode(node); } else { node = document.createAttributeNS(uri, name.getLocalPart()); if (prefix != null && !prefix.isEmpty()) { node.setPrefix(prefix); } element.setAttributeNodeNS(node); } String value = attribute.getValue(); Level level = stack.peek(); if ((level.scopeConfig) == null || (level.scopeConfig.getSelectors().isEmpty())) { value = filterAttribute(null, attribute.getName(), value, null); node.setValue(value); } else { UrlRewriteFilterPathDescriptor path = pickFirstMatchingPath(level); if (path instanceof UrlRewriteFilterApplyDescriptor) { String rule = ((UrlRewriteFilterApplyDescriptor) path).rule(); value = filterAttribute(null, attribute.getName(), value, rule); node.setValue(value); } } //dump( document ); if (prefix == null || prefix.isEmpty()) { writer.write(" "); writer.write(name.getLocalPart()); } else { writer.write(" "); writer.write(prefix); writer.write(":"); writer.write(name.getLocalPart()); } writer.write("=\""); writer.write(value); writer.write("\""); element.removeAttributeNode(node); }
From source file:org.apache.myfaces.trinidadbuild.plugin.tagdoc.TagdocReport.java
private String _getPrefix(QName qName) { if ((qName.getPrefix() != null) && !"".equals(qName.getPrefix())) return qName.getPrefix(); String namespace = qName.getNamespaceURI(); if (namespace == null) return null; for (Iterator<Map.Entry> i = taglibs.entrySet().iterator(); i.hasNext();) { Map.Entry entry = i.next(); if (namespace.equals(entry.getValue())) return (String) entry.getKey(); }/* www. ja va 2s. c om*/ return "unknown"; }
From source file:org.apache.ode.bpel.compiler.v1.xpath10.jaxp.JaxpVariableResolver.java
public Object resolveVariable(QName variableName) { __log.debug("JAXP compiler: Resolving variable " + variableName); // Custom variables if ("ode".equals(variableName.getPrefix()) || Namespaces.ODE_EXTENSION_NS.equals(variableName.getNamespaceURI())) { if ("pid".equals(variableName.getLocalPart())) return ""; }//from w w w . ja va 2s . c o m try { String name = variableName.getLocalPart(); if (_oxpath instanceof OXPath10ExpressionBPEL20 && ((OXPath10ExpressionBPEL20) _oxpath).isJoinExpression) { // these resolve to links OLink olink = _cctx.resolveLink(name); _oxpath.links.put(name, olink); return Boolean.TRUE; } else { int dot = name.indexOf('.'); if (dot != -1) name = name.substring(0, dot); OScope.Variable var = _cctx.resolveVariable(name); _oxpath.vars.put(name, var); return extractValue(var, var.type); } } catch (CompilationException e) { throw new WrappedResolverException(e); } }
From source file:org.apache.ode.bpel.compiler.v2.xquery10.compiler.XQuery10ExpressionCompilerImpl.java
private void doJaxpCompile(OXQuery10ExpressionBPEL20 out, Expression source) throws CompilationException { String xqueryStr;/*from w ww . j av a 2 s .c o m*/ Node node = source.getExpression(); if (node == null) { throw new IllegalStateException("XQuery string and xpath node are both null"); } if (node.getNodeType() != Node.TEXT_NODE && node.getNodeType() != Node.ELEMENT_NODE && node.getNodeType() != Node.CDATA_SECTION_NODE) { throw new CompilationException(__msgs.errUnexpectedNodeTypeForXPath(DOMUtils.domToString(node))); } xqueryStr = DOMUtils.domToString(node); xqueryStr = xqueryStr.trim(); if (xqueryStr.length() == 0) { throw new CompilationException(__msgs.warnXPath20Syntax(DOMUtils.domToString(node), "empty string")); } try { XQDataSource xqds = new SaxonXQDataSource(); XQConnection xqconn = xqds.getConnection(); __log.debug("Compiling expression " + xqueryStr); Configuration configuration = ((SaxonXQConnection) xqconn).getConfiguration(); configuration.setAllNodesUntyped(true); configuration.setHostLanguage(Configuration.XQUERY); XQStaticContext staticContext = xqconn.getStaticContext(); JaxpFunctionResolver funcResolver = new JaxpFunctionResolver(_compilerContext, out, source.getNamespaceContext(), _bpelNS); JaxpVariableResolver variableResolver = new JaxpVariableResolver(_compilerContext, out); XQueryDeclarations declarations = new XQueryDeclarations(); NSContext nsContext = source.getNamespaceContext(); Set<String> prefixes = nsContext.getPrefixes(); if (!nsContext.getUriSet().contains(Namespaces.ODE_EXTENSION_NS)) { nsContext.register("ode", Namespaces.ODE_EXTENSION_NS); } for (String prefix : prefixes) { String uri = nsContext.getNamespaceURI(prefix); staticContext.declareNamespace(prefix, uri); if ("".equals(prefix)) { declarations.declareDefaultElementNamespace(uri); } else if ("bpws".equals(prefix)) { declarations.declareNamespace("bpws", "java:" + Constants.XQUERY_FUNCTION_HANDLER_COMPILER); } else { declarations.declareNamespace(prefix, uri); } } declarations.declareVariable(getQName(nsContext, Namespaces.ODE_EXTENSION_NS, "pid"), getQName(nsContext, Namespaces.XML_SCHEMA, "integer")); Map<URI, Source> schemaDocuments = _compilerContext.getSchemaSources(); for (URI schemaUri : schemaDocuments.keySet()) { Source schemaSource = schemaDocuments.get(schemaUri); // Don't add schema sources, since our Saxon library is not schema-aware. // configuration.addSchemaSource(schemaSource); } configuration.setSchemaValidationMode(Validation.SKIP); List<OScope.Variable> variables = _compilerContext.getAccessibleVariables(); Map<QName, QName> variableTypes = new HashMap<QName, QName>(); for (String variableName : getVariableNames(xqueryStr)) { OScope.Variable variable = getVariable(variables, variableName); if (variable == null) { continue; } OVarType type = variable.type; QName nameQName = getNameQName(variableName); QName typeQName = getTypeQName(variableName, type); variableTypes.put(nameQName, typeQName); String prefix = typeQName.getPrefix(); if (prefix == null || "".equals(prefix.trim())) { prefix = getPrefixForUri(nsContext, typeQName.getNamespaceURI()); } // don't declare typed variables, as our engine is not schema-aware // declarations.declareVariable(variable.name, typeQName); declarations.declareVariable(variableName); } // Add implicit declarations as prolog to the user-defined XQuery out.xquery = declarations.toString() + xqueryStr; // Check the XQuery for compilation errors xqconn.setStaticContext(staticContext); XQPreparedExpression exp = xqconn.prepareExpression(out.xquery); // Pre-evaluate variables and functions by executing query node.setUserData(XQuery10BpelFunctions.USER_DATA_KEY_FUNCTION_RESOLVER, funcResolver, null); exp.bindItem(XQConstants.CONTEXT_ITEM, xqconn.createItemFromNode(node, xqconn.createNodeType())); // Bind external variables to dummy runtime values for (QName variable : exp.getAllUnboundExternalVariables()) { QName typeQName = variableTypes.get(variable); Object value = variableResolver.resolveVariable(variable); if (typeQName != null) { if (value.getClass().getName().startsWith("java.lang")) { exp.bindAtomicValue(variable, value.toString(), xqconn.createAtomicType(XQItemType.XQBASETYPE_ANYATOMICTYPE)); } else if (value instanceof Node) { exp.bindNode(variable, (Node) value, xqconn.createNodeType()); } else if (value instanceof NodeList) { NodeList nodeList = (NodeList) value; ArrayList nodeArray = new ArrayList(); for (int i = 0; i < nodeList.getLength(); i++) { nodeArray.add(nodeList.item(i)); } XQSequence sequence = xqconn.createSequence(nodeArray.iterator()); exp.bindSequence(variable, sequence); } } } // evaluate the expression so as to initialize the variables try { exp.executeQuery(); } catch (XQException xpee) { // swallow errors caused by uninitialized variables } } catch (XQException xqe) { __log.debug(xqe); __log.info("Couldn't validate properly expression " + xqueryStr); } catch (WrappedResolverException wre) { if (wre._compilationMsg != null) throw new CompilationException(wre._compilationMsg, wre); if (wre.getCause() instanceof CompilationException) throw (CompilationException) wre.getCause(); throw wre; } }
From source file:org.apache.ode.bpel.elang.xquery10.compiler.XQuery10ExpressionCompilerImpl.java
private void doJaxpCompile(OXQuery10ExpressionBPEL20 out, Expression source) throws CompilationException { String xqueryStr;//from ww w . jav a2 s . c om Node node = source.getExpression(); if (node == null) { throw new CompilationException(__msgs.errEmptyExpression(source.getURI(), new QName(source.getElement().getNamespaceURI(), source.getElement().getNodeName()))); } if (node.getNodeType() != Node.TEXT_NODE && node.getNodeType() != Node.ELEMENT_NODE && node.getNodeType() != Node.CDATA_SECTION_NODE) { throw new CompilationException(__msgs.errUnexpectedNodeTypeForXPath(DOMUtils.domToString(node))); } xqueryStr = DOMUtils.domToString(node); xqueryStr = xqueryStr.trim(); if (xqueryStr.length() == 0) { throw new CompilationException(__msgs.warnXPath20Syntax(DOMUtils.domToString(node), "empty string")); } try { XQDataSource xqds = new SaxonXQDataSource(new Configuration()); XQConnection xqconn = xqds.getConnection(); __log.debug("Compiling expression " + xqueryStr); Configuration configuration = ((SaxonXQConnection) xqconn).getConfiguration(); configuration.setAllNodesUntyped(true); configuration.setHostLanguage(Configuration.XQUERY); XQStaticContext staticContext = xqconn.getStaticContext(); JaxpFunctionResolver funcResolver = new JaxpFunctionResolver(_compilerContext, out, source.getNamespaceContext(), _bpelNS); JaxpVariableResolver variableResolver = new JaxpVariableResolver(_compilerContext, out); XQueryDeclarations declarations = new XQueryDeclarations(); NSContext nsContext = source.getNamespaceContext(); Set<String> prefixes = nsContext.getPrefixes(); if (!nsContext.getUriSet().contains(Namespaces.ODE_EXTENSION_NS)) { nsContext.register("ode", Namespaces.ODE_EXTENSION_NS); } for (String prefix : prefixes) { String uri = nsContext.getNamespaceURI(prefix); staticContext.declareNamespace(prefix, uri); if ("".equals(prefix)) { declarations.declareDefaultElementNamespace(uri); } else if ("bpws".equals(prefix)) { declarations.declareNamespace("bpws", "java:" + Constants.XQUERY_FUNCTION_HANDLER_COMPILER); } else { declarations.declareNamespace(prefix, uri); } } declarations.declareVariable(getQName(nsContext, Namespaces.ODE_EXTENSION_NS, "pid"), getQName(nsContext, Namespaces.XML_SCHEMA, "integer")); // Map<URI, Source> schemaDocuments = _compilerContext.getSchemaSources(); // for (URI schemaUri : schemaDocuments.keySet()) { // Source schemaSource = schemaDocuments.get(schemaUri); // // Don't add schema sources, since our Saxon library is not schema-aware. // // configuration.addSchemaSource(schemaSource); // } configuration.setSchemaValidationMode(Validation.SKIP); List<OScope.Variable> variables = _compilerContext.getAccessibleVariables(); Map<QName, QName> variableTypes = new HashMap<QName, QName>(); for (String variableName : getVariableNames(xqueryStr)) { OScope.Variable variable = getVariable(variables, variableName); if (variable == null) { continue; } OVarType type = variable.type; QName nameQName = getNameQName(variableName); QName typeQName = getTypeQName(variableName, type); variableTypes.put(nameQName, typeQName); String prefix = typeQName.getPrefix(); if (prefix == null || "".equals(prefix.trim())) { prefix = getPrefixForUri(nsContext, typeQName.getNamespaceURI()); } // don't declare typed variables, as our engine is not schema-aware // declarations.declareVariable(variable.name, typeQName); declarations.declareVariable(variableName); } // Add implicit declarations as prolog to the user-defined XQuery out.xquery = declarations.toString() + xqueryStr; // Check the XQuery for compilation errors xqconn.setStaticContext(staticContext); XQPreparedExpression exp = xqconn.prepareExpression(out.xquery); // Pre-evaluate variables and functions by executing query node.setUserData(XQuery10BpelFunctions.USER_DATA_KEY_FUNCTION_RESOLVER, funcResolver, null); exp.bindItem(XQConstants.CONTEXT_ITEM, xqconn.createItemFromNode(node, xqconn.createNodeType())); // Bind external variables to dummy runtime values for (QName variable : exp.getAllUnboundExternalVariables()) { QName typeQName = variableTypes.get(variable); Object value = variableResolver.resolveVariable(variable); if (typeQName != null) { if (value.getClass().getName().startsWith("java.lang")) { exp.bindAtomicValue(variable, value.toString(), xqconn.createAtomicType(XQItemType.XQBASETYPE_ANYATOMICTYPE)); } else if (value instanceof Node) { exp.bindNode(variable, (Node) value, xqconn.createNodeType()); } else if (value instanceof NodeList) { NodeList nodeList = (NodeList) value; ArrayList nodeArray = new ArrayList(); for (int i = 0; i < nodeList.getLength(); i++) { nodeArray.add(nodeList.item(i)); } XQSequence sequence = xqconn.createSequence(nodeArray.iterator()); exp.bindSequence(variable, sequence); } } } // evaluate the expression so as to initialize the variables try { exp.executeQuery(); } catch (XQException xpee) { // swallow errors caused by uninitialized variables } finally { // reset the expression's user data, in order to avoid // serializing the function resolver in the compiled bpel file. if (node != null) { node.setUserData(XQuery10BpelFunctions.USER_DATA_KEY_FUNCTION_RESOLVER, null, null); } } } catch (XQException xqe) { __log.debug(xqe); __log.info("Couldn't validate properly expression " + xqueryStr); throw new CompilationException( __msgs.errXQuery10Syntax(xqueryStr, "Couldn't validate XQuery expression")); } catch (WrappedResolverException wre) { if (wre._compilationMsg != null) throw new CompilationException(wre._compilationMsg, wre); if (wre.getCause() instanceof CompilationException) throw (CompilationException) wre.getCause(); throw wre; } }
From source file:org.apache.ode.utils.DOMUtils.java
public static String getQualifiedName(QName qName) { String prefix = qName.getPrefix(), localPart = qName.getLocalPart(); return (prefix == null || "".equals(prefix)) ? localPart : (prefix + ":" + localPart); }
From source file:org.apache.padaf.xmpbox.parser.XMPDocumentBuilder.java
/** * Build a property with the specific type defined in schema or complex * property and add it to the object representation * /*from www. j a v a2 s. c om*/ * @param metadata * Metadata to attach new elements * @param propertyName * The fully qualified name of the property * @param stype * Type of the property * @param container * the entity where place the property representation * @throws XmpUnknownPropertyTypeException * Value Type property is incorrect or the basic value type * can't be treat at the moment * @throws XmpPropertyFormatException * Unexpected type found (IllegalArgumentException) * @throws XMLStreamException * When error during reading the rest of xmp stream */ protected void parseXmpSimpleProperty(XMPMetadata metadata, QName propertyName, XmpPropertyType stype, ComplexPropertyContainer container) throws XmpUnknownPropertyTypeException, XmpPropertyFormatException, XMLStreamException { try { AbstractSimpleProperty prop = null; ArrayList<Attribute> attributes = new ArrayList<Attribute>(); int cpt = reader.get().getAttributeCount(); for (int i = 0; i < cpt; i++) { attributes.add(new Attribute(null, reader.get().getAttributePrefix(i), reader.get().getAttributeLocalName(i), reader.get().getAttributeValue(i))); } if (stype == XmpPropertyType.Text) { prop = new TextType(metadata, propertyName.getPrefix(), propertyName.getLocalPart(), reader.get().getElementText()); } else if (stype == XmpPropertyType.Integer) { prop = new IntegerType(metadata, propertyName.getPrefix(), propertyName.getLocalPart(), reader.get().getElementText()); } else if (stype == XmpPropertyType.Date) { prop = new DateType(metadata, propertyName.getPrefix(), propertyName.getLocalPart(), reader.get().getElementText()); } else if (stype == XmpPropertyType.Boolean) { prop = new BooleanType(metadata, propertyName.getPrefix(), propertyName.getLocalPart(), reader.get().getElementText()); } else if (stype == XmpPropertyType.Real) { prop = new RealType(metadata, propertyName.getPrefix(), propertyName.getLocalPart(), reader.get().getElementText()); } if (prop != null) { container.addProperty(prop); // ADD ATTRIBUTES for (Attribute att : attributes) { prop.setAttribute(att); } } else { throw new XmpUnknownPropertyTypeException("Unknown simple type found"); } } catch (IllegalArgumentException e) { throw new XmpPropertyFormatException( "Unexpected type found for the property '" + propertyName.getLocalPart() + "'", e); } }