Package org.exist.util.serializer

Examples of org.exist.util.serializer.SAXSerializer


            XmldbURI diffUri = XmldbURI.createInternal(documentPath.toString() + '.' + revision);

            vCollection.setTriggersEnabled(false);

            StringWriter writer = new StringWriter();
            SAXSerializer sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(
                SAXSerializer.class);
            Properties outputProperties = new Properties();
            outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            outputProperties.setProperty(OutputKeys.INDENT, "no");
            sax.setOutput(writer, outputProperties);

            sax.startDocument();
            sax.startElement(ELEMENT_VERSION, null);
            writeProperties(sax, getVersionProperties(revision, documentPath, activeSubject));

            if (remove) {
              sax.startElement(ELEMENT_REMOVED, null);
              sax.endElement(ELEMENT_REMOVED);
            } else {

              //Diff
              if(document instanceof BinaryDocument) {
                //create a copy of the last Binary revision
                XmldbURI binUri = XmldbURI.create(diffUri.toString() + BINARY_SUFFIX);
                broker.copyResource(transaction, document, vCollection, binUri);

                //Create metadata about the last Binary Version
                sax.startElement(ELEMENT_REPLACED_BINARY, null);
                sax.attribute(ATTRIBUTE_REF, binUri.toString());
                sax.endElement(ELEMENT_REPLACED_BINARY);
              } else if(lastRev instanceof BinaryDocument) {
                //create a copy of the last XML revision
                XmldbURI xmlUri = XmldbURI.create(diffUri.toString() + XML_SUFFIX);
                broker.copyResource(transaction, document, vCollection, xmlUri);

                //Create metadata about the last Binary Version
                sax.startElement(ELEMENT_REPLACED_XML, null);
                sax.attribute(ATTRIBUTE_REF, xmlUri.toString());
                sax.endElement(ELEMENT_REPLACED_XML);
              } else {
                //Diff the XML versions
                Diff diff = new StandardDiff(broker);
                diff.diff(lastRev, document);
                diff.diff2XML(sax);
              }

              sax.endElement(ELEMENT_VERSION);

              sax.endDocument();
              String editscript = writer.toString();

              if (removeLast) {
                if(lastRev instanceof BinaryDocument) {
                  vCollection.removeBinaryResource(transaction, broker, lastRev.getFileURI());
View Full Code Here


            qr.touch();
            final Serializer serializer = broker.getSerializer();
            serializer.reset();
            serializer.setProperties(qr.serialization);
           
            final SAXSerializer handler = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
            final StringWriter writer = new StringWriter();
            handler.setOutput(writer, getProperties(parameters));
           
//      serialize results
            handler.startDocument();
            handler.startPrefixMapping("exist", Namespaces.EXIST_NS);
            final AttributesImpl attribs = new AttributesImpl();
            attribs.addAttribute(
                    "",
                    "hitCount",
                    "hitCount",
                    "CDATA",
                    Integer.toString(qr.result.getItemCount()));
            handler.startElement(
                Namespaces.EXIST_NS,
                    "result",
                    "exist:result",
                    attribs);
            Item current;
            char[] value;
            for(final SequenceIterator i = qr.result.iterate(); i.hasNext(); ) {
                current = i.nextItem();
                if(Type.subTypeOf(current.getType(), Type.NODE))
                    {current.toSAX(broker, handler, null);}
                else {
                    value = current.toString().toCharArray();
                    handler.characters(value, 0, value.length);
                }
            }
            handler.endElement(Namespaces.EXIST_NS, "result", "exist:result");
            handler.endPrefixMapping("exist");
            handler.endDocument();
            SerializerPool.getInstance().returnObject(handler);
            return writer.toString();
        } finally {
            factory.getBrokerPool().release(broker);
        }
View Full Code Here

        serializer.reset();
        for (final Map.Entry<Object, Object> entry : qr.serialization.entrySet()) {
          parameters.put(entry.getKey().toString(), entry.getValue().toString());
        }
        serializer.setProperties(parameters);
        final SAXSerializer handler = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);

        final HashMap<String, Object> result = new HashMap<String, Object>();
        VirtualTempFile vtempFile = new VirtualTempFile(MAX_DOWNLOAD_CHUNK_SIZE,MAX_DOWNLOAD_CHUNK_SIZE);
        vtempFile.setTempPrefix("eXistRPCC");
        vtempFile.setTempPostfix(".xml");

        OutputStream os=null;
        if("yes".equals(compression)) {
          LOG.debug("get result with compression");
          os = new DeflaterOutputStream(vtempFile);
        } else {
          os = vtempFile;
        }
        try {
          final Writer writer = new OutputStreamWriter(os, encoding);
          try {
            handler.setOutput(writer, getProperties(parameters));

            // serialize results
            handler.startDocument();
            handler.startPrefixMapping("exist", Namespaces.EXIST_NS);
            final AttributesImpl attribs = new AttributesImpl();
            attribs.addAttribute(
                "",
                "hitCount",
                "hitCount",
                "CDATA",
                Integer.toString(qr.result.getItemCount()));
            handler.startElement(
                Namespaces.EXIST_NS,
                "result",
                "exist:result",
                attribs);
            Item current;
            char[] value;
            for(final SequenceIterator i = qr.result.iterate(); i.hasNext(); ) {
              current = i.nextItem();
              if(Type.subTypeOf(current.getType(), Type.NODE))
                {((NodeValue)current).toSAX(broker, handler, null);}
              else {
                value = current.toString().toCharArray();
                handler.characters(value, 0, value.length);
              }
            }
            handler.endElement(Namespaces.EXIST_NS, "result", "exist:result");
            handler.endPrefixMapping("exist");
            handler.endDocument();
            SerializerPool.getInstance().returnObject(handler);
          } finally {
            writer.close();
          }
        } finally {
View Full Code Here

            dialog.setResourceCount( resources.length );
        }
        final Writer        contents   = output.newContents();

        // serializer writes to __contents__.xml
        final SAXSerializer serializer = (SAXSerializer)SerializerPool.getInstance().borrowObject( SAXSerializer.class );
        serializer.setOutput( contents, contentsOutputProps );

        serializer.startDocument();
        serializer.startPrefixMapping( "", Namespaces.EXIST_NS );

        // write <collection> element
        final CollectionImpl cur  = (CollectionImpl)current;
        final AttributesImpl attr = new AttributesImpl();

        //The name should have come from an XmldbURI.toString() call
        attr.addAttribute( Namespaces.EXIST_NS, "name", "name", "CDATA", current.getName() );
        writeUnixStylePermissionAttributes(attr, currentPerms);
        attr.addAttribute( Namespaces.EXIST_NS, "created", "created", "CDATA", "" + new DateTimeValue( cur.getCreationTime() ) );
        attr.addAttribute( Namespaces.EXIST_NS, "version", "version", "CDATA", String.valueOf( currVersion ) );
       
        serializer.startElement( Namespaces.EXIST_NS, "collection", "collection", attr );

        if(currentPerms instanceof ACLPermission) {
            writeACLPermission(serializer, (ACLPermission)currentPerms);
        }

        // scan through resources
        Resource       resource;
        OutputStream   os;
        BufferedWriter writer;
        SAXSerializer  contentSerializer;

        for( int i = 0; i < resources.length; i++ ) {

            try {

                if( "__contents__.xml".equals(resources[i]) ) {

                    //Skipping resources[i]
                    continue;
                }
                resource = current.getResource( resources[i] );

                if( dialog != null ) {
                    dialog.setResource( resources[i] );
                    dialog.setProgress( i );
                }
               
                final String name   = resources[i];
                String filename = encode( URIUtils.urlDecodeUtf8( resources[i] ) );
               
                // Check for special resource names which cause problems as filenames, and if so, replace the filename with a generated filename
               
                if( ".".equals(name.trim()) ) {
                  filename = EXIST_GENERATED_FILENAME_DOT_FILENAME + i;
                } else if( "..".equals(name.trim()) ) {
                  filename = EXIST_GENERATED_FILENAME_DOTDOT_FILENAME + i;
                }

                os = output.newEntry( filename );

                if( resource instanceof ExtendedResource ) {
                    ( (ExtendedResource)resource ).getContentIntoAStream( os );
                } else {
                    writer            = new BufferedWriter( new OutputStreamWriter( os, "UTF-8" ) );

                    // write resource to contentSerializer
                    contentSerializer = (SAXSerializer)SerializerPool.getInstance().borrowObject( SAXSerializer.class );
                    contentSerializer.setOutput( writer, defaultOutputProperties );
                    ( (EXistResource)resource ).setLexicalHandler( contentSerializer );
                    ( (XMLResource)resource ).getContentAsSAX( contentSerializer );
                    SerializerPool.getInstance().returnObject( contentSerializer );
                    writer.flush();
                }
View Full Code Here

    public TestResultWriter(String outFile) throws EXistException {
        File file = new File(outFile);
        try {
            writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file)), "UTF-8");
            serializer = new SAXSerializer(writer, defaultProperties);
            serializer.startDocument();
            AttrList attribs = new AttrList();
            serializer.startElement(ROOT_ELEMENT, attribs);
        } catch (Exception e) {
            throw new EXistException("error while configuring test output file: " + file.getAbsolutePath(), e);
View Full Code Here

                Node node = resource.getContentAsDOM();
        System.out.println("Attempting serialization using eXist's SAX serializer");
        StringWriter writer = new StringWriter();
        Properties outputProperties = new Properties();
        outputProperties.setProperty("indent", "yes");
        SAXSerializer serializer = new SAXSerializer(writer, outputProperties);
        resource.getContentAsSAX(serializer);

        System.out.println("Using org.exist.util.serializer.SAXSerializer");
        System.out.println("---------------------");
        System.out.println(writer.toString());
View Full Code Here

        Properties outputProperties = new Properties();
        outputProperties.setProperty("indent", "yes");
        testCollection.setProperty("stylesheet", "test.xsl");
        testCollection.setProperty("stylesheet-param.testparam", "TEST");
        StringWriter writer = new StringWriter();
        SAXSerializer serializer = new SAXSerializer(writer, outputProperties);
        resource.getContentAsSAX(serializer);

        System.out.println("Using org.exist.util.serializer.SAXSerializer");
        System.out.println("---------------------");
        System.out.println(writer.toString());
View Full Code Here

        try {
            final Writer        contents   = output.newContents();

            // serializer writes to __contents__.xml
            final SAXSerializer serializer = (SAXSerializer)SerializerPool.getInstance().borrowObject( SAXSerializer.class );
            serializer.setOutput( contents, contentsOutputProps );

            serializer.startDocument();
            serializer.startPrefixMapping( "", Namespaces.EXIST_NS );
            final AttributesImpl attr = new AttributesImpl();
            attr.addAttribute( Namespaces.EXIST_NS, "name", "name", "CDATA", "/db/__lost_and_found__" );
            attr.addAttribute( Namespaces.EXIST_NS, "version", "version", "CDATA", String.valueOf( currVersion ) );
            attr.addAttribute( Namespaces.EXIST_NS, "owner", "owner", "CDATA", org.exist.security.SecurityManager.DBA_USER );
            attr.addAttribute( Namespaces.EXIST_NS, "group", "group", "CDATA", org.exist.security.SecurityManager.DBA_GROUP );
            attr.addAttribute( Namespaces.EXIST_NS, "mode", "mode", "CDATA", "0771" );
            serializer.startElement( Namespaces.EXIST_NS, "collection", "collection", attr );

            final DocumentCallback docCb = new DocumentCallback( output, serializer, null, null, docs, true );
            broker.getResourcesFailsafe( docCb, directAccess );

            serializer.endElement( Namespaces.EXIST_NS, "collection", "collection" );
            serializer.endPrefixMapping( "" );
            serializer.endDocument();
            output.closeContents();
        }
        catch( final Exception e ) {
            e.printStackTrace();
View Full Code Here

        try {
            final Writer        contents   = output.newContents();

            // serializer writes to __contents__.xml
            final SAXSerializer serializer = (SAXSerializer)SerializerPool.getInstance().borrowObject( SAXSerializer.class );
            serializer.setOutput( contents, contentsOutputProps );

            final Permission perm = current.getPermissionsNoLock();
           
            serializer.startDocument();
            serializer.startPrefixMapping( "", Namespaces.EXIST_NS );
            final XmldbURI       uri  = current.getURI();
            final AttributesImpl attr = new AttributesImpl();
            attr.addAttribute( Namespaces.EXIST_NS, "name", "name", "CDATA", uri.toString() );
            attr.addAttribute( Namespaces.EXIST_NS, "version", "version", "CDATA", String.valueOf( currVersion ) );
            Backup.writeUnixStylePermissionAttributes(attr, perm);
            try {
                attr.addAttribute( Namespaces.EXIST_NS, "created", "created", "CDATA", new DateTimeValue( new Date( current.getCreationTime() ) ).getStringValue() );
            }
            catch( final XPathException e ) {
                e.printStackTrace();
            }
           
            bh.backup(current, attr);
           
            serializer.startElement( Namespaces.EXIST_NS, "collection", "collection", attr );

            if(perm instanceof ACLPermission) {
              Backup.writeACLPermission(serializer, (ACLPermission)perm);
            }
           
            bh.backup(current, serializer);

            final int docsCount = current.getDocumentCountNoLock(broker);
            int count     = 0;

            for( final Iterator<DocumentImpl> i = current.iteratorNoLock( broker ); i.hasNext(); count++ ) {
                final DocumentImpl doc = i.next();

                if( isDamaged( doc, errorList ) ) {
                    reportError( "Skipping damaged document " + doc.getFileURI(), null );
                    continue;
                }

                if( doc.getFileURI().equalsInternal( CONTENTS_URI ) || doc.getFileURI().equalsInternal( LOST_URI ) ) {
                    continue; // skip __contents__.xml documents
                }
                exportDocument( bh, output, date, prevBackup, serializer, docsCount, count, doc );
                docs.add( doc, false );
            }

            for( final Iterator<XmldbURI> i = current.collectionIteratorNoLock(broker); i.hasNext(); ) {
                final XmldbURI childUri = i.next();

                if( childUri.equalsInternal( TEMP_COLLECTION ) ) {
                    continue;
                }

                if( isDamagedChild( childUri, errorList ) ) {
                    reportError( "Skipping damaged child collection " + childUri, null );
                    continue;
                }
                attr.clear();
                attr.addAttribute( Namespaces.EXIST_NS, "name", "name", "CDATA", childUri.toString() );
                attr.addAttribute( Namespaces.EXIST_NS, "filename", "filename", "CDATA", Backup.encode( URIUtils.urlDecodeUtf8( childUri.toString() ) ) );
                serializer.startElement( Namespaces.EXIST_NS, "subcollection", "subcollection", attr );
                serializer.endElement( Namespaces.EXIST_NS, "subcollection", "subcollection" );
            }

            if( prevBackup != null ) {

                // Check which collections and resources have been deleted since
                // the
                // last backup
                final CheckDeletedHandler check = new CheckDeletedHandler( current, serializer );

                try {
                    prevBackup.parse( check );
                }
                catch( final Exception e ) {
                    LOG.error( "Caught exception while trying to parse previous backup descriptor: " + prevBackup.getSymbolicPath(), e );
                }
            }

            // close <collection>
            serializer.endElement( Namespaces.EXIST_NS, "collection", "collection" );
            serializer.endPrefixMapping( "" );
            serializer.endDocument();
            output.closeContents();
        }
        finally {

//            if( !current.getURI().equalsInternal( XmldbURI.ROOT_COLLECTION_URI ) ) {
View Full Code Here

                    broker.readBinaryResource( (BinaryDocument)doc, os );
                } else {
                    final BufferedWriter writer            = new BufferedWriter( new OutputStreamWriter( os, "UTF-8" ) );

                    // write resource to contentSerializer
                    final SAXSerializer  contentSerializer = (SAXSerializer)SerializerPool.getInstance().borrowObject( SAXSerializer.class );
                    contentSerializer.setOutput( writer, defaultOutputProperties );
                    writeXML( doc, contentSerializer );
                    SerializerPool.getInstance().returnObject( contentSerializer );
                    writer.flush();
                }
            }
View Full Code Here

TOP

Related Classes of org.exist.util.serializer.SAXSerializer

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.