Package org.exist.memtree

Examples of org.exist.memtree.MemTreeBuilder


            final Sequence contentSeq = content.eval(contextSequence, contextItem);

            if(contentSeq.isEmpty())
              {result = Sequence.EMPTY_SEQUENCE;}
            else {
                final MemTreeBuilder builder = context.getDocumentBuilder();
                context.proceed(this, builder);
                final StringBuilder buf = new StringBuilder();
                for(final SequenceIterator i = contentSeq.iterate(); i.hasNext(); ) {
                    context.proceed(this, builder);
                    final Item next = i.nextItem();
                    if(buf.length() > 0)
                        {buf.append(' ');}
                    buf.append(next.toString());
                }
                //It is possible for a text node constructor to construct a text node containing a zero-length string.
                //However, if used in the content of a constructed element or document node,
                //such a text node will be deleted or merged with another text node.
                if (!newDocumentContext && buf.length() == 0)
                    {result = Sequence.EMPTY_SEQUENCE;}
                else {
                    final int nodeNr = builder.characters(buf);
                    result = builder.getDocument().getNode(nodeNr);
                }
            }
        } finally {
            if (newDocumentContext)
                {context.popDocumentContext();}
View Full Code Here


                // in-memory DOM implementation
                reader = context.getBroker().getBrokerPool().getParserPool().borrowXMLReader();
                LOG.debug( "Parsing XML response ..." );

                // TODO : we should be able to cope with context.getBaseURI()
                final MemTreeBuilder builder = context.getDocumentBuilder();
                final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver( builder, true );
                reader.setContentHandler(receiver);
                reader.setProperty("http://xml.org/sax/properties/lexical-handler", receiver);
                reader.parse(inputSource);
                final Document doc = receiver.getDocument();
View Full Code Here

            try {
                // try and construct xml document from input stream, we use eXist's
                // in-memory DOM implementation

                // TODO : we should be able to cope with context.getBaseURI()
                final MemTreeBuilder builder = context.getDocumentBuilder();
                final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
                reader.setContentHandler(receiver);
                reader.parse(src.getInputSource());
                final Document doc = receiver.getDocument();
                return((NodeValue)doc);
View Full Code Here

        if (newDocumentContext)
            {context.pushDocumentContext();}
       
        NodeImpl node;
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            builder.setReplaceAttributeFlag(replaceAttribute);
            context.proceed(this, builder);

            final Sequence nameSeq = qnameExpr.eval(contextSequence, contextItem);
            if(!nameSeq.hasOne())
              {throw new XPathException(this, "The name expression should evaluate to a single value");}

            final Item qnItem = nameSeq.itemAt(0);
            QName qn;
            if (qnItem.getType() == Type.QNAME)
                {qn = ((QNameValue) qnItem).getQName();}
            else
              try {
                qn = QName.parse(context, nameSeq.getStringValue(), null);
          } catch (final IllegalArgumentException e) {
          throw new XPathException(this, ErrorCodes.XPTY0004, "'" + nameSeq.getStringValue() + "' is not a valid attribute name");
        }

            //Not in the specs but... makes sense
            if(!XMLChar.isValidName(qn.getLocalName()))
              {throw new XPathException(this, ErrorCodes.XPTY0004, "'" + qn.getLocalName() + "' is not a valid attribute name");}
           
            if ("xmlns".equals(qn.getLocalName()) && qn.getNamespaceURI().isEmpty())
              {throw new XPathException(this, ErrorCodes.XQDY0044, "'" + qn.getLocalName() + "' is not a valid attribute name");}

            String value;
            final Sequence valueSeq = valueExpr.eval(contextSequence, contextItem);
            if(valueSeq.isEmpty())
              {value = "";}
            else {
                final StringBuilder buf = new StringBuilder();
                for(final SequenceIterator i = valueSeq.iterate(); i.hasNext(); ) {
                    final Item next = i.nextItem();
                    buf.append(next.getStringValue());
                    if(i.hasNext())
                        {buf.append(' ');}
                }
                value = buf.toString();
            }
           
            value = DynamicAttributeConstructor.normalize(this, qn, value);
           
            node = null;
            try {
                final int nodeNr = builder.addAttribute(qn, value);
                node = builder.getDocument().getAttribute(nodeNr);
            } catch (final DOMException e) {
                throw new XPathException(this, ErrorCodes.XQDY0025, "element has more than one attribute '" + qn + "'");
            }
        } finally {
            if (newDocumentContext)
View Full Code Here

    Item contextItem)
    throws XPathException {
        if (newDocumentContext)
            {context.pushDocumentContext();}
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            final int nodeNr = builder.processingInstruction(target, data);
            final NodeImpl node = builder.getDocument().getNode(nodeNr);
            return node;
        } finally {
            if (newDocumentContext)
                {context.popDocumentContext();}
        }
View Full Code Here

        }

        if (newDocumentContext)
            {context.pushDocumentContext();}
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
           
            int nodeNr;
            if (literalCharacters) {
              //Empty CDATA sections generate no text nodes
                if (cdata.isEmpty())
                    {return Sequence.EMPTY_SEQUENCE;}
               
              nodeNr = builder.characters(cdata);
            } else {
              nodeNr = builder.cdataSection(cdata);
            }
          final NodeImpl node = builder.getDocument().getNode(nodeNr);

            if (context.getProfiler().isEnabled())
                {context.getProfiler().end(this, "", node);}

            return node;
View Full Code Here

        }

        context.pushDocumentContext();

        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            final int            nodeNr  = builder.startElement( DIRECTORY_ELEMENT, null );

            if( dir.isDirectory() && dir.canRead() ) {
                final Pattern pattern = Pattern.compile( BackupDirectory.FILE_REGEX );
                final Matcher matcher = pattern.matcher( "" );
                final File[]  files   = dir.listFiles();

                for( int i = 0; i < files.length; i++ ) {
                    matcher.reset( files[i].getName() );

                    if( matcher.matches() ) {
                        BackupDescriptor descriptor;

                        try {

                            if( files[i].getName().endsWith( ".zip" ) ) {
                                descriptor = new ZipArchiveBackupDescriptor( files[i] );
                            } else {
                              final File descriptorFile = new File(new File(files[i], "db"), BackupDescriptor.COLLECTION_DESCRIPTOR);
                                descriptor = new FileSystemBackupDescriptor( descriptorFile );
                            }
                            final Properties properties = descriptor.getProperties();

                            if( properties != null ) {
                                final AttributesImpl attrs = new AttributesImpl();
                                attrs.addAttribute( "", "file", "file", "CDATA", files[i].getName() );
                                builder.startElement( BACKUP_ELEMENT, attrs );

                                for( final Iterator<Object> iter = properties.keySet().iterator(); iter.hasNext(); ) {
                                    final String key = iter.next().toString();
                                    builder.startElement( new QName( key, Namespaces.EXIST_NS, "" ), null );
                                    builder.characters( (String)properties.get( key ) );
                                    builder.endElement();
                                }
                                builder.endElement();
                            }
                        }
                        catch( final IOException e ) {
                        }
                    }
                }
            }
            builder.endElement();
            return( builder.getDocument().getNode( nodeNr ) );
        }
        finally {
            context.popDocumentContext();
        }
    }
View Full Code Here

        context.expressionStart(this);
    context.pushInScopeNamespaces();
        if (newDocumentContext)
            {context.pushDocumentContext();}
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            // declare namespaces
            if(namespaceDecls != null) {
                for(int i = 0; i < namespaceDecls.length; i++) {
                    //if ("".equals(namespaceDecls[i].getNamespaceURI())) {
                        // TODO: the specs are unclear here: should we throw XQST0085 or not?
                    //  context.inScopeNamespaces.remove(namespaceDecls[i].getLocalName());
//          if (context.inScopeNamespaces.remove(namespaceDecls[i].getLocalName()) == null)
//                throw new XPathException(getAS      TNode(), "XQST0085 : can not undefine '" + namespaceDecls[i] + "'");
                    //} else
                        context.declareInScopeNamespace(namespaceDecls[i].getLocalName(), namespaceDecls[i].getNamespaceURI());
                }
            }
            // process attributes
            final AttributesImpl attrs = new AttributesImpl();
            if(attributes != null) {
                AttributeConstructor constructor;
                Sequence attrValues;
                QName attrQName;
                // first, search for xmlns attributes and declare in-scope namespaces
                for (int i = 0; i < attributes.length; i++) {
                    constructor = attributes[i];
                    if(constructor.isNamespaceDeclaration()) {
                        final int p = constructor.getQName().indexOf(':');
                        if(p == Constants.STRING_NOT_FOUND)
                            {context.declareInScopeNamespace("", constructor.getLiteralValue());}
                        else {
                            final String prefix = constructor.getQName().substring(p + 1);
                            context.declareInScopeNamespace(prefix, constructor.getLiteralValue());
                        }
                    }
                }
                String v = null;
                // process the remaining attributes
                for (int i = 0; i < attributes.length; i++) {
                    context.proceed(this, builder);
                    constructor = attributes[i];
                    attrValues = constructor.eval(contextSequence, contextItem);
                    attrQName = QName.parse(context, constructor.getQName(), "");
                   
                    final String namespaceURI = attrQName.getNamespaceURI();
                if (namespaceURI != null && !namespaceURI.isEmpty() && attrQName.getPrefix() == null) {
                  String prefix = context.getPrefixForURI(namespaceURI);
                 
                  if (prefix != null) {
                    attrQName.setPrefix(prefix);
                  } else {
                    //generate prefix
                    for (final int n = 1; i < 100; i++) {
                      prefix = "eXnsp"+n;
                            if (context.getURIForPrefix(prefix) == null) {
                              attrQName.setPrefix(prefix);
                              break;
                            }
                           
                            prefix = null;
                    }
                    if (prefix == null)
                                {throw new XPathException(this, "Prefix can't be generate.");}
                  }
                }
                   
                    if (attrs.getIndex(attrQName.getNamespaceURI(), attrQName.getLocalName()) != -1)
                        {throw new XPathException(this, ErrorCodes.XQST0040, "'" + attrQName.getLocalName() + "' is a duplicate attribute name");}
                   
                    v = DynamicAttributeConstructor.normalize(this, attrQName, attrValues.getStringValue());
                   
                    attrs.addAttribute(attrQName.getNamespaceURI(), attrQName.getLocalName(),
                            attrQName.getStringValue(), "CDATA", v);
                }
            }
            context.proceed(this, builder);

            // create the element
            final Sequence qnameSeq = qnameExpr.eval(contextSequence, contextItem);
            if(!qnameSeq.hasOne())
              {throw new XPathException(this, ErrorCodes.XPTY0004, "Type error: the node name should evaluate to a single item");}
            final Item qnitem = qnameSeq.itemAt(0);
            QName qn;
            if (qnitem instanceof QNameValue) {
                qn = ((QNameValue)qnitem).getQName();
            } else {
                //Do we have the same result than Atomize there ? -pb
              try {
                qn = QName.parse(context, qnitem.getStringValue());
              } catch (final IllegalArgumentException e) {
              throw new XPathException(this, ErrorCodes.XPTY0004, "" + qnitem.getStringValue() + "' is not a valid element name");
              } catch (final XPathException e) {
                e.setLocation(getLine(), getColumn(), getSource());
                throw e;
        }
             
                //Use the default namespace if specified
                /*
                 if (qn.getPrefix() == null && context.inScopeNamespaces.get("xmlns") != null) {
                     qn.setNamespaceURI((String)context.inScopeNamespaces.get("xmlns"));
                 }
                 */
                if (qn.getPrefix() == null && context.getInScopeNamespace("") != null) {
                     qn.setNamespaceURI(context.getInScopeNamespace(""));
                }
             }

            //Not in the specs but... makes sense
            if(!XMLChar.isValidName(qn.getLocalName()))
              {throw new XPathException(this, ErrorCodes.XPTY0004, "'" + qnitem.getStringValue() + "' is not a valid element name");}

            // add namespace declaration nodes
            final int nodeNr = builder.startElement(qn, attrs);
            if(namespaceDecls != null) {
                for(int i = 0; i < namespaceDecls.length; i++) {
                    builder.namespaceNode(namespaceDecls[i]);
                }
            }
            // do we need to add a namespace declaration for the current node?
            if (qn.needsNamespaceDecl()) {
                if (context.getInScopePrefix(qn.getNamespaceURI()) == null) {
                    String prefix = qn.getPrefix();
                    if (prefix == null || prefix.length() == 0)
                        {prefix = "";}
                    context.declareInScopeNamespace(prefix, qn.getNamespaceURI());
                    builder.namespaceNode(new QName(prefix, qn.getNamespaceURI(), "xmlns"));
                }
            } else if ((qn.getPrefix() == null || qn.getPrefix().length() == 0) &&
                context.getInheritedNamespace("") != null) {
                context.declareInScopeNamespace("", "");
                builder.namespaceNode(new QName("", "", "xmlns"));
            }
            // process element contents
            if(content != null) {
                content.eval(contextSequence, contextItem);
            }
            builder.endElement();
            final NodeImpl node = builder.getDocument().getNode(nodeNr);
            return node;
        } finally {
            context.popInScopeNamespaces();
            if (newDocumentContext)
                {context.popDocumentContext();}
View Full Code Here

    Item contextItem)
    throws XPathException {
        if (newDocumentContext)
            {context.pushDocumentContext();}
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            final int nodeNr = builder.comment(data);
            final NodeImpl node = builder.getDocument().getNode(nodeNr);
            return node;
        } finally {
            if (newDocumentContext)
                {context.popDocumentContext();}
        }
View Full Code Here

            }
        }

        context.pushDocumentContext();
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
            for (final SequenceIterator i = temp.iterate(); i.hasNext(); ) {
                final Item next = i.nextItem();
                if (Type.subTypeOf(next.getType(), Type.NODE)) {
                    next.copyTo(context.getBroker(), receiver);
View Full Code Here

TOP

Related Classes of org.exist.memtree.MemTreeBuilder

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.