Search in sources :

Example 1 with ReferenceNode

use of org.exist.dom.memtree.ReferenceNode in project exist by eXist-db.

the class Serializer method hasXSLPi.

/**
 * Check if the document has an xml-stylesheet processing instruction
 * that references an XSLT stylesheet. Return the link to the stylesheet.
 *
 * @param doc the document
 * @return link to the stylesheet
 */
public String hasXSLPi(final Document doc) {
    final boolean applyXSLPI = outputProperties.getProperty(EXistOutputKeys.PROCESS_XSL_PI, "no").equalsIgnoreCase("yes");
    if (!applyXSLPI) {
        return null;
    }
    NodeList docChildren = doc.getChildNodes();
    if (docChildren.getLength() == 1) {
        final Node onlyChild = docChildren.item(0);
        if (onlyChild.getNodeType() == NodeImpl.REFERENCE_NODE) {
            // if this is a reference to a persistent document node then we must expand it
            final NodeProxy nodeProxy = ((ReferenceNode) onlyChild).getReference();
            if (nodeProxy.getNodeType() == Node.DOCUMENT_NODE) {
                // switch docChildren to the children of the dereferencedNode
                final Node dereferencedNode = nodeProxy.getNode();
                docChildren = dereferencedNode.getChildNodes();
            }
        }
    }
    for (int i = 0; i < docChildren.getLength(); i++) {
        final Node node = docChildren.item(i);
        if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE && "xml-stylesheet".equals(((ProcessingInstruction) node).getTarget())) {
            // found <?xml-stylesheet?>
            final String xsl = ((ProcessingInstruction) node).getData();
            final String type = XMLUtil.parseValue(xsl, "type");
            if (type != null && (type.equals(MimeType.XML_TYPE.getName()) || type.equals(MimeType.XSL_TYPE.getName()) || type.equals(MimeType.XSLT_TYPE.getName()))) {
                final String href = XMLUtil.parseValue(xsl, "href");
                if (href == null) {
                    continue;
                }
                return href;
            }
        }
    }
    return null;
}
Also used : ReferenceNode(org.exist.dom.memtree.ReferenceNode) NodeList(org.w3c.dom.NodeList) StoredNode(org.exist.dom.persistent.StoredNode) Node(org.w3c.dom.Node) ReferenceNode(org.exist.dom.memtree.ReferenceNode) NodeProxy(org.exist.dom.persistent.NodeProxy) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 2 with ReferenceNode

use of org.exist.dom.memtree.ReferenceNode in project exist by eXist-db.

the class Eval method initContext.

/**
 * Read to optional static-context fragment to initialize
 * the context.
 *
 * @param root
 * @param innerContext
 * @throws XPathException
 */
private Sequence initContext(final Node root, final XQueryContext innerContext) throws XPathException {
    final NodeList cl = root.getChildNodes();
    Sequence result = null;
    for (int i = 0; i < cl.getLength(); i++) {
        final Node child = cl.item(i);
        // TODO : more check on attributes existence and on their values
        if (child.getNodeType() == Node.ELEMENT_NODE && "variable".equals(child.getLocalName())) {
            final Element elem = (Element) child;
            final String qname = elem.getAttribute("name");
            final String source = elem.getAttribute("source");
            NodeValue value;
            if (isNotEmpty(source)) {
                // load variable contents from URI
                value = loadVarFromURI(source);
            } else {
                value = (NodeValue) elem.getFirstChild();
                if (value instanceof ReferenceNode) {
                    value = ((ReferenceNode) value).getReference();
                }
            }
            final String type = elem.getAttribute("type");
            if (type != null && Type.subTypeOf(Type.getType(type), Type.ATOMIC)) {
                innerContext.declareVariable(qname, value.atomize().convertTo(Type.getType(type)));
            } else {
                innerContext.declareVariable(qname, value);
            }
        } else if (child.getNodeType() == Node.ELEMENT_NODE && "output-size-limit".equals(child.getLocalName())) {
            final Element elem = (Element) child;
            // TODO : error check
            innerContext.getWatchDog().setMaxNodes(Integer.parseInt(elem.getAttribute("value")));
        } else if (child.getNodeType() == Node.ELEMENT_NODE && "timeout".equals(child.getLocalName())) {
            final Element elem = (Element) child;
            // TODO : error check
            innerContext.getWatchDog().setTimeout(Long.parseLong(elem.getAttribute("value")));
        } else if (child.getNodeType() == Node.ELEMENT_NODE && "current-dateTime".equals(child.getLocalName())) {
            final Element elem = (Element) child;
            // TODO : error check
            final DateTimeValue dtv = new DateTimeValue(elem.getAttribute("value"));
            innerContext.setCalendar(dtv.calendar);
        } else if (child.getNodeType() == Node.ELEMENT_NODE && "implicit-timezone".equals(child.getLocalName())) {
            final Element elem = (Element) child;
            // TODO : error check
            final Duration duration = TimeUtils.getInstance().newDuration(elem.getAttribute("value"));
            innerContext.setTimeZone(new SimpleTimeZone((int) duration.getTimeInMillis(new Date()), "XQuery context"));
        } else if (child.getNodeType() == Node.ELEMENT_NODE && "unbind-namespace".equals(child.getLocalName())) {
            final Element elem = (Element) child;
            // TODO : error check
            if (elem.getAttribute("uri") != null) {
                innerContext.removeNamespace(elem.getAttribute("uri"));
            }
        } else if (child.getNodeType() == Node.ELEMENT_NODE && "staticallyKnownDocuments".equals(child.getLocalName())) {
            final Element elem = (Element) child;
            // TODO : iterate over the children
            NodeValue value = (NodeValue) elem.getFirstChild();
            if (value instanceof ReferenceNode) {
                value = ((ReferenceNode) value).getReference();
            }
            final XmldbURI[] pathes = new XmldbURI[1];
            // TODO : aggregate !
            // TODO : cleanly seperate the statically know docollection and documents
            pathes[0] = XmldbURI.create(value.getStringValue());
            innerContext.setStaticallyKnownDocuments(pathes);
        } else /*else if (child.getNodeType() == Node.ELEMENT_NODE &&	"mapModule".equals(child.getLocalPart())) {
				Element elem = (Element) child;
				//TODO : error check
				if (elem.getAttribute("namespace") != null && elem.getAttribute("uri") != null) {
					innerContext.mapModule(elem.getAttribute("namespace"),
							XmldbURI.create(elem.getAttribute("uri")));
				}
			} */
        if (child.getNodeType() == Node.ELEMENT_NODE && "default-context".equals(child.getLocalName())) {
            final Element elem = (Element) child;
            final NodeValue nodevalue = (NodeValue) elem;
            result = nodevalue.toSequence();
        }
    }
    return result;
}
Also used : ReferenceNode(org.exist.dom.memtree.ReferenceNode) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ReferenceNode(org.exist.dom.memtree.ReferenceNode) Element(org.w3c.dom.Element) Duration(javax.xml.datatype.Duration) FunSubSequence(org.exist.xquery.functions.fn.FunSubSequence) Date(java.util.Date) SimpleTimeZone(java.util.SimpleTimeZone) XmldbURI(org.exist.xmldb.XmldbURI)

Example 3 with ReferenceNode

use of org.exist.dom.memtree.ReferenceNode in project exist by eXist-db.

the class DOMStreamer method serialize.

/**
 * Serialize the given node and all its descendants to SAX. If
 * callDocumentEvents is set to false, startDocument/endDocument
 * events will not be fired.
 *
 * @param node the node to serialize
 * @param callDocumentEvents whether we shoiuld call the document events startDocument/endDocument
 * @throws SAXException if an error occurs during serialization.
 */
public void serialize(Node node, final boolean callDocumentEvents) throws SAXException {
    if (callDocumentEvents) {
        contentHandler.startDocument();
    }
    final Node top = node;
    while (node != null) {
        startNode(node);
        Node nextNode = node.getFirstChild();
        // TODO : make it happy
        if (node instanceof ReferenceNode) {
            nextNode = null;
        }
        while (nextNode == null) {
            endNode(node);
            if (top != null && top.equals(node)) {
                break;
            }
            nextNode = node.getNextSibling();
            if (nextNode == null) {
                node = node.getParentNode();
                if (node == null || (top != null && top.equals(node))) {
                    endNode(node);
                    // nextNode = null;
                    break;
                }
            }
        }
        node = nextNode;
    }
    if (callDocumentEvents) {
        contentHandler.endDocument();
    }
}
Also used : ReferenceNode(org.exist.dom.memtree.ReferenceNode) ReferenceNode(org.exist.dom.memtree.ReferenceNode) Node(org.w3c.dom.Node)

Aggregations

ReferenceNode (org.exist.dom.memtree.ReferenceNode)3 Node (org.w3c.dom.Node)3 NodeList (org.w3c.dom.NodeList)2 Date (java.util.Date)1 SimpleTimeZone (java.util.SimpleTimeZone)1 Duration (javax.xml.datatype.Duration)1 NodeProxy (org.exist.dom.persistent.NodeProxy)1 StoredNode (org.exist.dom.persistent.StoredNode)1 XmldbURI (org.exist.xmldb.XmldbURI)1 FunSubSequence (org.exist.xquery.functions.fn.FunSubSequence)1 Element (org.w3c.dom.Element)1 ProcessingInstruction (org.w3c.dom.ProcessingInstruction)1