use of org.exist.dom.QName in project exist by eXist-db.
the class Expand method eval.
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
// apply serialization options set on the XQuery context
final Properties serializeOptions = new Properties();
serializeOptions.setProperty(EXistOutputKeys.EXPAND_XINCLUDES, "yes");
serializeOptions.setProperty(EXistOutputKeys.HIGHLIGHT_MATCHES, "elements");
if (getArgumentCount() == 2) {
final String serOpts = args[1].getStringValue();
final String[] contents = Option.tokenize(serOpts);
for (String content : contents) {
final String[] pair = Option.parseKeyValuePair(content);
if (pair == null) {
throw new XPathException(this, "Found invalid serialization option: " + content);
}
logger.debug("Setting serialization property: {} = {}", pair[0], pair[1]);
serializeOptions.setProperty(pair[0], pair[1]);
}
} else {
context.checkOptions(serializeOptions);
}
context.pushDocumentContext();
try {
final InMemoryNodeSet result = new InMemoryNodeSet();
final MemTreeBuilder builder = new MemTreeBuilder(getContext());
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
int attrNr = -1;
for (final SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
final NodeValue next = (NodeValue) i.nextItem();
final short nodeType = ((INodeHandle) next).getNodeType();
builder.startDocument();
if (nodeType == Node.ATTRIBUTE_NODE) {
// NOTE: Attributes nodes need special handling as they cannot be directly serialized via SAX to a ContentHandler
final Attr attr = (Attr) next.getNode();
String ns = attr.getNamespaceURI();
if (ns == null || ns.isEmpty()) {
ns = XMLConstants.NULL_NS_URI;
}
attrNr = builder.addAttribute(new QName(attr.getLocalName(), ns), attr.getValue());
} else {
next.toSAX(context.getBroker(), receiver, serializeOptions);
}
builder.endDocument();
if (Node.DOCUMENT_NODE == nodeType) {
result.add(builder.getDocument());
} else if (Node.ATTRIBUTE_NODE == nodeType) {
result.add(builder.getDocument().getAttribute(attrNr));
} else {
result.add(builder.getDocument().getNode(1));
}
builder.reset(getContext());
}
return result;
} catch (final SAXException e) {
throw new XPathException(this, e);
} finally {
context.popDocumentContext();
}
}
use of org.exist.dom.QName in project exist by eXist-db.
the class ResourceFunctionExecutorImpl method findFunction.
/**
* Lookup a Function in an XQuery given a Function Signature
*
* @param xquery The XQuery to interrogate
* @param functionSignature The Function Signature to use to match a Function
*
* @return The Function from the XQuery matching the Function Signature
*/
private UserDefinedFunction findFunction(final CompiledXQuery xquery, final FunctionSignature functionSignature) throws XPathException {
final QName fnName = QName.fromJavaQName(functionSignature.getName());
final int arity = functionSignature.getArgumentCount();
return xquery.getContext().resolveFunction(fnName, arity);
}
use of org.exist.dom.QName in project exist by eXist-db.
the class LuceneIndexConfig method match.
public boolean match(NodePath other) {
if (isQNameIndex) {
final QName qn1 = path.getLastComponent();
final QName qn2 = other.getLastComponent();
return qn1.getNameType() == qn2.getNameType() && qn2.equals(qn1);
}
return path.match(other);
}
use of org.exist.dom.QName in project exist by eXist-db.
the class LuceneIndexConfig method parseQName.
protected static QName parseQName(String name, Map<String, String> namespaces) throws DatabaseConfigurationException {
boolean isAttribute = false;
if (name.startsWith("@")) {
isAttribute = true;
name = name.substring(1);
}
try {
String prefix = QName.extractPrefix(name);
String localName = QName.extractLocalName(name);
String namespaceURI = "";
if (prefix != null) {
namespaceURI = namespaces.get(prefix);
if (namespaceURI == null) {
throw new DatabaseConfigurationException("No namespace defined for prefix: " + prefix + " in index definition");
}
}
final QName qname;
if (isAttribute) {
qname = new QName(localName, namespaceURI, prefix, ElementValue.ATTRIBUTE);
} else {
qname = new QName(localName, namespaceURI, prefix);
}
return qname;
} catch (QName.IllegalQNameException e) {
throw new DatabaseConfigurationException("Lucene index configuration error: " + e.getMessage(), e);
}
}
use of org.exist.dom.QName in project exist by eXist-db.
the class ZipFileFunctions method extractEntries.
private Sequence extractEntries(XmldbURI uri) throws XPathException {
ZipFileSource zipFileSource = new ZipFileFromDb(uri);
ZipInputStream zis = null;
Sequence xmlResponse = null;
context.pushDocumentContext();
try {
MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(new QName("file", ZipModule.NAMESPACE_URI, ZipModule.PREFIX), null);
builder.addAttribute(new QName("href", null, null), uri.toString());
try {
zis = zipFileSource.getStream();
ZipEntry zipEntry;
while ((zipEntry = zis.getNextEntry()) != null) {
if (zipEntry.isDirectory()) {
builder.startElement(new QName("dir", ZipModule.NAMESPACE_URI, ZipModule.PREFIX), null);
builder.addAttribute(new QName("name", null, null), zipEntry.toString());
builder.endElement();
} else {
logger.debug("file: {}", zipEntry.getName());
builder.startElement(new QName("entry", ZipModule.NAMESPACE_URI, ZipModule.PREFIX), null);
builder.addAttribute(new QName("name", null, null), zipEntry.toString());
builder.endElement();
}
}
} catch (PermissionDeniedException pde) {
logger.error(pde.getMessage(), pde);
throw new XPathException("Permission denied to read the source zip");
} catch (IOException ioe) {
logger.error(ioe.getMessage(), ioe);
throw new XPathException("IO exception while reading the source zip");
}
builder.endElement();
xmlResponse = (NodeValue) builder.getDocument().getDocumentElement();
return (xmlResponse);
} finally {
context.popDocumentContext();
}
}
Aggregations