Package org.exist.memtree

Examples of org.exist.memtree.ElementImpl


                query = getParameter(request, Query);
            }
        }
        final String _var = getParameter(request, Variables);
        List /*<Namespace>*/ namespaces = null;
        ElementImpl variables = null;
        try {
            if (_var != null) {
                final NamespaceExtractor nsExtractor = new NamespaceExtractor();
                variables = parseXML(_var, nsExtractor);
                namespaces = nsExtractor.getNamespaces();
View Full Code Here


            // third, normal POST: read the request content and check if
            // it is an XUpdate or a query request.
            int howmany = 10;
            int start = 1;
            boolean typed = false;
            ElementImpl variables = null;
            boolean enclose = true;
            boolean cache = false;
            String query = null;

            final TransactionManager transact = broker.getBrokerPool().getTransactionManager();
            final Txn transaction = transact.beginTransaction();

            try {
                final String content = getRequestContent(request);
                final NamespaceExtractor nsExtractor = new NamespaceExtractor();
                final ElementImpl root = parseXML(content, nsExtractor);
                final String rootNS = root.getNamespaceURI();
               
                if (rootNS != null && rootNS.equals(Namespaces.EXIST_NS)) {

                    if (Query.xmlKey().equals(root.getLocalName())) {
                        // process <query>xpathQuery</query>
                        String option = root.getAttribute(Start.xmlKey());
                        if (option != null) {
                            try {
                                start = Integer.parseInt(option);
                            } catch (final NumberFormatException e) {
                                //
                            }
                        }

                        option = root.getAttribute(Max.xmlKey());
                        if (option != null) {
                            try {
                                howmany = Integer.parseInt(option);
                            } catch (final NumberFormatException e) {
                                //
                            }
                        }

                        option = root.getAttribute(Enclose.xmlKey());
                        if (option != null) {
                            if ("no".equals(option)) {
                                enclose = false;
                            }
                        } else {
                            option = root.getAttribute(Wrap.xmlKey());
                            if (option != null) {
                                if ("no".equals(option)) {
                                    enclose = false;
                                }
                            }
                        }

                        option = root.getAttribute(Method.xmlKey());
                        if ((option != null) && (!"".equals(option))) {
                            outputProperties.setProperty(SERIALIZATION_METHOD_PROPERTY, option);
                        }

                        option = root.getAttribute(Typed.xmlKey());
                        if (option != null) {
                            if ("yes".equals(option)) {
                                typed = true;
                            }
                        }

                        option = root.getAttribute(Mime.xmlKey());
                        if ((option != null) && (!"".equals(option))) {
                            mimeType = option;
                        }

                        if ((option = root.getAttribute(Cache.xmlKey())) != null) {
                            cache = "yes".equals(option);
                        }

                        if ((option = root.getAttribute(Session.xmlKey())) != null
                                && option.length() > 0) {
                            outputProperties.setProperty(
                                    Serializer.PROPERTY_SESSION_ID, option);
                        }

                        final NodeList children = root.getChildNodes();
                        for (int i = 0; i < children.getLength(); i++) {

                            final Node child = children.item(i);
                            if (child.getNodeType() == Node.ELEMENT_NODE
                                    && child.getNamespaceURI().equals(Namespaces.EXIST_NS)) {

                                if (Text.xmlKey().equals(child.getLocalName())) {
                                    final StringBuilder buf = new StringBuilder();
                                    Node next = child.getFirstChild();
                                    while (next != null) {
                                        if (next.getNodeType() == Node.TEXT_NODE
                                                || next.getNodeType() == Node.CDATA_SECTION_NODE) {
                                            buf.append(next.getNodeValue());
                                        }
                                        next = next.getNextSibling();
                                    }
                                    query = buf.toString();

                                } else if (Variables.xmlKey().equals(child.getLocalName())) {
                                    variables = (ElementImpl) child;

                                } else if (Properties.xmlKey().equals(child.getLocalName())) {
                                    Node node = child.getFirstChild();
                                    while (node != null) {
                                        if (node.getNodeType() == Node.ELEMENT_NODE
                                                && node.getNamespaceURI().equals(Namespaces.EXIST_NS)
                                                && Property.xmlKey().equals(node.getLocalName())) {

                                            final Element property = (Element) node;
                                            final String key = property.getAttribute("name");
                                            final String value = property.getAttribute("value");
                                            LOG.debug(key + " = " + value);

                                            if (key != null && value != null) {
                                                outputProperties.setProperty(key, value);
                                            }
                                        }
                                        node = node.getNextSibling();
                                    }
                                }
                            }
                        }
                    }
                   
                    // execute query
                    if (query != null) {

                        try {
                            search(broker, query, path, nsExtractor.getNamespaces(), variables,
                                    howmany, start, typed, outputProperties,
                                    enclose, cache, request, response);

                            transact.commit(transaction);

                        } catch (final XPathException e) {
                            if (MimeType.XML_TYPE.getName().equals(mimeType)) {
                                writeXPathException(response, HttpServletResponse.SC_ACCEPTED,
                                        encoding, null, path, e);
                            } else {
                                writeXPathExceptionHtml(response, HttpServletResponse.SC_ACCEPTED,
                                        encoding, null, path, e);
                            }
                        }

                    } else {
                        transact.abort(transaction);
                        throw new BadRequestException("No query specified");
                    }

                } else if (rootNS != null && rootNS.equals(XUpdateProcessor.XUPDATE_NS)) {

                    LOG.debug("Got xupdate request: " + content);
                    final MutableDocumentSet docs = new DefaultDocumentSet();
                    final Collection collection = broker.getCollection(pathUri);
                    if (collection != null) {
                        collection.allDocs(broker, docs, true);

                    } else {
                        final DocumentImpl xupdateDoc = broker.getResource(pathUri, Permission.READ);

                        if (xupdateDoc != null) {
                            docs.add(xupdateDoc);

                        } else {
                            broker.getAllXMLResources(docs);
                        }
                    }

                    final XUpdateProcessor processor = new XUpdateProcessor(broker, docs, AccessContext.REST);
                    final Modification modifications[] = processor.parse(new InputSource(new StringReader(content)));
                    long mods = 0;
                    for (int i = 0; i < modifications.length; i++) {
                        mods += modifications[i].process(transaction);
                        broker.flush();
                    }

                    transact.commit(transaction);

                    // FD : Returns an XML doc
                    writeXUpdateResult(response, encoding, mods);
                    // END FD

                } else {
                    transact.abort(transaction);
                    throw new BadRequestException("Unknown XML root element: " + root.getNodeName());
                }

            } catch (final SAXException e) {
                transact.abort(transaction);
                Exception cause = e;
View Full Code Here

        final ElementImpl variables) throws XPathException {

        final ValueSequence varSeq = new ValueSequence();
        variables.selectChildren(new NameTest(Type.ELEMENT, new QName(Variable.xmlKey(), Namespaces.EXIST_NS)), varSeq);
        for (final SequenceIterator i = varSeq.iterate(); i.hasNext();) {
            final ElementImpl variable = (ElementImpl) i.nextItem();
            // get the QName of the variable
            final ElementImpl qname = (ElementImpl) variable.getFirstChild(new NameTest(Type.ELEMENT, new QName("qname", Namespaces.EXIST_NS)));
            String localname = null, prefix = null, uri = null;
            NodeImpl child = (NodeImpl) qname.getFirstChild();
            while (child != null) {
                if ("localname".equals(child.getLocalName())) {
                    localname = child.getStringValue();

                } else if ("namespace".equals(child.getLocalName())) {
View Full Code Here

            {throw new XMLStreamException("Root element should be a " + SEQ_ELEMENT_QNAME);}
        final ValueSequence result = new ValueSequence();
        final InMemoryNodeSet values = new InMemoryNodeSet();
        node.selectChildren(new NameTest(Type.ELEMENT, VALUE_QNAME), values);
        for (final SequenceIterator i = values.iterate(); i.hasNext();) {
            final ElementImpl child = (ElementImpl) i.nextItem();
            final String typeName = child.getAttribute(ATTR_TYPE);
            if (typeName != null) {
                final int type = Type.getType(typeName);
                Item item;
                if (Type.subTypeOf(type, Type.NODE)) {
                    item = (Item) child.getFirstChild();
                    if (type == Type.DOCUMENT) {
                        final NodeImpl n = (NodeImpl) item;
                        final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver();
                        try {
                            receiver.startDocument();
                            n.getDocument().copyTo(n, receiver);
                            receiver.endDocument();
                        } catch (final SAXException e) {
                            throw new XPathException("Error while demarshalling node: " + e.getMessage(), e);
                        }
                        item = (Item) receiver.getDocument();
                    }
                } else {
                    final StringBuilder data = new StringBuilder();
                    Node txt = child.getFirstChild();
                    while (txt != null) {
                        if (!(txt.getNodeType() == Node.TEXT_NODE || txt.getNodeType() == Node.CDATA_SECTION_NODE))
                            {throw new XMLStreamException("sx:value should only contain text if type is " + typeName);}
                        data.append(txt.getNodeValue());
                        txt = txt.getNextSibling();
View Full Code Here

TOP

Related Classes of org.exist.memtree.ElementImpl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.