Package org.exist.memtree

Examples of org.exist.memtree.MemTreeBuilder


        DocumentImpl doc1 = ((NodeProxy)nv1).getDocument();
        DocumentImpl doc2 = ((NodeProxy)nv2).getDocument();

        context.pushDocumentContext();
        try {
            MemTreeBuilder builder = context.getDocumentBuilder();
            DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
            Properties properties = new Properties();
            properties.setProperty("document", doc1.getURI().toString());
            properties.setProperty("revision", "");
            properties.setProperty("date", new DateTimeValue(new Date()).getStringValue());
            properties.setProperty("user", context.getUser().getName());

            int nodeNr = builder.startElement(VersioningTrigger.ELEMENT_VERSION, null);
            VersioningTrigger.writeProperties(receiver, properties);

            Diff diff = new StandardDiff(context.getBroker());
            diff.diff(doc1, doc2);
            diff.diff2XML(receiver);

            builder.endElement();
            return builder.getDocument().getNode(nodeNr);
        } catch (SAXException e) {
            throw new XPathException(this, "Caugt error while generating diff: " + e.getMessage(), e);
        } catch (DiffException e) {
            throw new XPathException(this, "Caugt error while generating diff: " + e.getMessage(), e);
        } finally {
View Full Code Here


      CallableStatement statement = null;
      ResultSet resultSet = null;
     
      try {
        MemTreeBuilder builder = context.getDocumentBuilder();
        int iRow = 0;
       
        statement = connection.prepareCall(plSql);
        if(haveReturnCode)
        {
          statement.registerOutParameter(1, Types.NUMERIC);
        }     
        if(resultSetPos != 0)
        {
          statement.registerOutParameter(resultSetPos, OracleTypes.CURSOR);
        }
        if(!args[2].isEmpty())
        {
          setParametersOnPreparedStatement(statement, parameters);
        }
             
        statement.execute();
       
        if(haveReturnCode) {
          int returnCode = statement.getInt(1);
          if(returnCode != plSqlSuccess) {
            LOG.error(plSql + " failed [" + returnCode + "]");
            return( Sequence.EMPTY_SEQUENCE );
          }
        }     
       
        if(resultSetPos != 0) {
          // iterate through the result set building an XML document
          builder.startDocument();
           
          builder.startElement(new QName("result", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
          builder.addAttribute(new QName("count", null, null), String.valueOf(-1 ));
           
          resultSet = (ResultSet)statement.getObject(resultSetPos);
           
          ResultSetMetaData rsmd = resultSet.getMetaData();
          int iColumns = rsmd.getColumnCount();
           
          while (resultSet.next())
          {
            builder.startElement(new QName("row", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
                builder.addAttribute(new QName("index", null, null), String.valueOf(resultSet.getRow()));

            // get each tuple in the row
            for(int i = 0; i < iColumns; i++)
            {
              String columnName = rsmd.getColumnLabel(i + 1);
              if(columnName != null)
              {
                String colValue = resultSet.getString(i + 1);
                 
                String colElement = "field";

                if(((BooleanValue)args[4].itemAt(0)).effectiveBooleanValue() && columnName.length() > 0)
                {
                  // use column names as the XML node

                  /**
                   * Spaces in column names are replaced with
                   * underscore's
                   */
                   
                  colElement = SQLUtils.escapeXmlAttr(columnName.replace(' ', '_'));
                }
               
                builder.startElement(new QName(colElement, OracleModule.NAMESPACE_URI, OracleModule.PREFIX ), null);
               
                if(!((BooleanValue)args[4].itemAt(0)).effectiveBooleanValue() || columnName.length() <= 0)
                {
                  String name;
                 
                  if(columnName.length() > 0) {
                    name = SQLUtils.escapeXmlAttr(columnName);
                  } else {
                    name = "Column: " + String.valueOf(i + 1);
                  }
                 
                  builder.addAttribute(new QName("name", null, null), name);
                }
               
                builder.addAttribute(new QName("type", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), rsmd.getColumnTypeName(i + 1));
                builder.addAttribute(new QName("type", Namespaces.SCHEMA_NS, "xs"), Type.getTypeName(SQLUtils.sqlTypeToXMLType(rsmd.getColumnType(i + 1))));
               
                if(resultSet.wasNull())
                {
                  // Add a null indicator attribute if the value was SQL Null
                  builder.addAttribute(new QName("null", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), "true");
                }
               
                if(colValue != null)
                {
                  builder.characters(SQLUtils.escapeXmlText(colValue));
                }
               
                builder.endElement();
              }
            }

            builder.endElement();
             
            iRow++;
          }
          builder.endElement();
         
          // Change the root element count attribute to have the correct value
         
          NodeValue node = (NodeValue)builder.getDocument().getDocumentElement();

          Node count = node.getNode().getAttributes().getNamedItem("count");
         
          if(count != null)
          {
            count.setNodeValue(String.valueOf(iRow));
          }
          builder.endDocument();
         
          // return the XML result set
          return(node);
        }
        else
        {
          // there was no result set so just return an empty sequence
          return( Sequence.EMPTY_SEQUENCE );
        }
      }
      catch(SQLException sqle) {
       
        LOG.error("oracle:execute() Caught SQLException \"" + sqle.getMessage() + "\" for PL/SQL: \"" + plSql + "\"", sqle);
       
        //return details about the SQLException
        MemTreeBuilder builder = context.getDocumentBuilder();
       
        builder.startDocument();
        builder.startElement(new QName("exception", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
       
        boolean recoverable = false;
        if(sqle instanceof SQLRecoverableException)
        {
          recoverable = true;
        }
        builder.addAttribute(new QName("recoverable", null, null), String.valueOf(recoverable));
             
        builder.startElement(new QName("state", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
        String sqlState = sqle.getSQLState();
        if(sqlState != null) {
          builder.characters(sqle.getSQLState());
        }
        else {
          builder.characters("null");
        }
                       
        builder.endElement();
       
        builder.startElement(new QName("message", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
        builder.characters(sqle.getMessage());
        builder.endElement();
       
        builder.startElement(new QName("stack-trace", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
        ByteArrayOutputStream bufStackTrace = new ByteArrayOutputStream();
        sqle.printStackTrace(new PrintStream(bufStackTrace));
        builder.characters(new String(bufStackTrace.toByteArray()));
        builder.endElement();
       
        builder.startElement(new QName("oracle", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
        builder.characters(SQLUtils.escapeXmlText(plSql));
        builder.endElement();
       
          int line = getLine();
          int column = getColumn();
         
          builder.startElement(new QName("xquery", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
          builder.addAttribute(new QName("line", null, null), String.valueOf(line));
          builder.addAttribute(new QName("column", null, null), String.valueOf(column));
          builder.endElement();

        builder.endElement();
        builder.endDocument();
       
        return (NodeValue)builder.getDocument().getDocumentElement();   
      }
      finally {
        release(connection, statement, resultSet);
      }
        }
View Full Code Here

                //return the metadata
                context.pushDocumentContext();
                try
                {
                        MemTreeBuilder builder = context.getDocumentBuilder();
                        DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
                        DOMStreamer streamer = new DOMStreamer();
                        streamer.setContentHandler(receiver);
                        streamer.serialize(nMetadata);
                        Document docMetadata = receiver.getDocument();
View Full Code Here

        }

//        if (newDocumentContext)
//            context.pushDocumentContext();
        try {
            MemTreeBuilder builder = context.getDocumentBuilder();
            context.proceed(this, builder);

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

//            Item nameItem = nameSeq.itemAt(0);
//            if(!(nameItem.getType() == Type.STRING || nameItem.getType() == Type.NCNAME ||
//                    nameItem.getType() == Type.UNTYPED_ATOMIC))
//                throw new XPathException(this, "The name expression should evaluate to a " +
//                        Type.getTypeName(Type.STRING) + " or a " + Type.getTypeName(Type.NCNAME) +
//                        " or a " + Type.getTypeName(Type.UNTYPED_ATOMIC) + ". Got: " +
//                        Type.getTypeName(nameItem.getType()));

//            if(!XMLChar.isValidNCName(nameSeq.getStringValue()))
//      throw new XPathException(this, ErrorCodes.XQDY0041, nameSeq.getStringValue() + "' is not a valid processing instruction name", nameSeq);

//            if (nameSeq.getStringValue().equalsIgnoreCase("XML"))
//                throw new XPathException(this, ErrorCodes.XQDY0064, nameSeq.getStringValue() + "' is not a valid processing instruction name", nameSeq);

            if(!XMLChar.isValidNCName(attr_name))
              throw new XPathException(this, ErrorCodes.XQDY0041, "'"+attr_name + "' is not a valid processing instruction name");

            if (attr_name.equalsIgnoreCase("XML"))
                throw new XPathException(this, ErrorCodes.XQDY0064, "'"+attr_name+"' is not a valid processing instruction name");

            String contentString;
            Sequence contentSeq;
            if (select != null)
              contentSeq = select.eval(contextSequence, contextItem);
            else
              contentSeq = super.eval(contextSequence, contextItem);
           
            if(contentSeq.isEmpty())
          contentString = "";
            else {
                StringBuilder buf = new StringBuilder();
                for(SequenceIterator i = contentSeq.iterate(); i.hasNext(); ) {
                    context.proceed(this, builder);
                    Item next = i.nextItem();
                    if(buf.length() > 0)
                        buf.append(' ');
                    buf.append(next.getStringValue());
                }
                while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0)))
                    buf.deleteCharAt(0);
                contentString = buf.toString();
            }

            if (contentString.indexOf("?>") != Constants.STRING_NOT_FOUND)
                throw new XPathException(this, ErrorCodes.XQDY0026, contentString + "' is not a valid processing intruction content", contentSeq);

            int nodeNr = builder.processingInstruction(attr_name, contentString);

            Sequence result = ((DocumentImpl)builder.getDocument()).getNode(nodeNr);

            if (context.getProfiler().isEnabled())
                context.getProfiler().end(this, "", result);

            return result;
View Full Code Here

        } finally {
            context.popDocumentContext();
        }
       
    // create the output
    MemTreeBuilder builder = context.getDocumentBuilder();
    DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);   
    try {
      SequenceIterator i = result.iterate();
      Item next = i.nextItem();   
      StringBuilder buf = null;
View Full Code Here

         
            context.setContextSequencePosition(pos, selected);
          Sequence res = xslt.templates(selected, item);
          if (res == null) {
                if (item instanceof Text) {
                      MemTreeBuilder builder = context.getDocumentBuilder();
                  builder.characters(item.getStringValue());
                  result.add(item);
                } else if (item instanceof Node || item instanceof NodeValue) {
                  res = eval(selected, item); //item.toSequence();//
                } else
                  throw new XPathException("not supported item type");
View Full Code Here

        } else if (obj instanceof XMLResource) {
            return getNode(context.getBroker(), (XMLResource)obj);
        } else if (obj instanceof Node) {
            final DOMStreamer streamer = (DOMStreamer) SerializerPool.getInstance().borrowObject(DOMStreamer.class);
            try {
                final MemTreeBuilder builder = new MemTreeBuilder(context);
                builder.startDocument();
                final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(
                        builder);
                streamer.setContentHandler(receiver);
                streamer.serialize((Node) obj, false);
                return builder.getDocument().getNode(1);
            } catch (final SAXException e) {
                throw new XPathException(
                        "Failed to transform node into internal model: "
                                + e.getMessage());
            } finally {
                SerializerPool.getInstance().returnObject(streamer);
            }
        } else if (obj instanceof List<?>) {
            boolean createNodeSequence = true;
            Object next;
            for (final Iterator<?> i = ((List<?>) obj).iterator(); i.hasNext();) {
                next = i.next();
                if (!(next instanceof NodeProxy))
                    {createNodeSequence = false;}
            }
            Sequence seq = null;
            if (createNodeSequence)
                {seq = new AVLTreeNodeSet();}
            else
                {seq = new ValueSequence();}
            for (final Iterator<?> i = ((List<?>) obj).iterator(); i.hasNext();) {
                seq.add((Item) javaObjectToXPath(i.next(), context, expandChars));
            }
            return seq;
        } else if (obj instanceof NodeList) {
            final DOMStreamer streamer = (DOMStreamer) SerializerPool.getInstance().borrowObject(DOMStreamer.class);
            try {
                final MemTreeBuilder builder = new MemTreeBuilder();
                builder.startDocument();
                final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(
                        builder);
                streamer.setContentHandler(receiver);
                final ValueSequence seq = new ValueSequence();
                final NodeList nl = (NodeList) obj;
                int last = builder.getDocument().getLastNode();
                for (int i = 0; i < nl.getLength(); i++) {
                    final Node n = nl.item(i);
                    streamer.serialize(n, false);
                    final NodeImpl created = builder.getDocument().getNode(last + 1);
                    seq.add(created);
                    last = builder.getDocument().getLastNode();
                }
                return seq;
            } catch (final SAXException e) {
                throw new XPathException(
                        "Failed to transform node into internal model: "
View Full Code Here

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

        context.popDocumentContext();
        context.pushDocumentContext();

        final MemTreeBuilder builder = context.getDocumentBuilder(true);
        final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);

        try {
            if(!contentSeq.isEmpty()) {
                StringBuilder buf = null;
                final SequenceIterator i = contentSeq.iterate();
                Item next = i.nextItem();
                while(next != null) {
                    context.proceed(this, builder);
                    if (next.getType() == Type.ATTRIBUTE || next.getType() == Type.NAMESPACE)
                        {throw new XPathException(this, "Found a node of type " +
                            Type.getTypeName(next.getType()) + " inside a document constructor");}
                    // if item is an atomic value, collect the string values of all
                    // following atomic values and seperate them by a space.
                    if (Type.subTypeOf(next.getType(), Type.ATOMIC)) {
                        if(buf == null)
                            {buf = new StringBuilder();}
                        else if (buf.length() > 0)
                            {buf.append(' ');}
                        buf.append(next.getStringValue());
                        next = i.nextItem();
                    // if item is a node, flush any collected character data and
                    // copy the node to the target doc.
                    } else if (next.getType() == Type.DOCUMENT) {
                        if (buf != null && buf.length() > 0) {
                            receiver.characters(buf);
                            buf.setLength(0);
                        }
                        next.copyTo(context.getBroker(), receiver);
                        next = i.nextItem();
                    } else if (Type.subTypeOf(next.getType(), Type.NODE)) {
                        if (buf != null && buf.length() > 0) {
                            receiver.characters(buf);
                            buf.setLength(0);
                        }
                        next.copyTo(context.getBroker(), receiver);
                        next = i.nextItem();
                    }
                }
                // flush remaining character data
                if (buf != null && buf.length() > 0) {
                    receiver.characters(buf);
                    buf.setLength(0);
                }
            }
        } catch(final SAXException e) {
            throw new XPathException(this,
                "Encountered SAX exception while processing document constructor: " +
                ExpressionDumper.dump(this));
        }
        context.popDocumentContext();
        final NodeImpl node =  builder.getDocument();
        if (context.getProfiler().isEnabled())
            {context.getProfiler().end(this, "", node);}
        return node;
    }
View Full Code Here

        if (isWhitespaceOnly && context.stripWhitespace())
            {return Sequence.EMPTY_SEQUENCE;}
        if (newDocumentContext)
            {context.pushDocumentContext();}
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            context.proceed(this, builder);
            final int nodeNr = builder.characters(text);
            final NodeImpl node = builder.getDocument().getNode(nodeNr);
            return node;
        } finally {
            if (newDocumentContext)
                {context.popDocumentContext();}
        }
View Full Code Here

            result = super.eval(contextSequence, null);
        } finally {
            context.popDocumentContext();
        }
        // create the output
        final MemTreeBuilder builder = context.getDocumentBuilder();
        final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
        receiver.checkNS = true;
        try {
            final SequenceIterator i = result.iterate();
            Item next = i.nextItem();
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.