Package org.exist.util.serializer

Examples of org.exist.util.serializer.SAXSerializer


    {
        if( dest != null ) {
            final Properties outputProperties = new Properties();
            outputProperties.setProperty( OutputKeys.INDENT, "yes" );

            final SAXSerializer serializer = (SAXSerializer)SerializerPool.getInstance().borrowObject( SAXSerializer.class );

            Writer        writer     = null;

            if( dest.isDirectory() ) {

                if( !dest.exists() ) {
                    dest.mkdirs();
                }
                String fname = resource.getId();

                if( !fname.endsWith( ".xml" ) ) {
                    fname += ".xml";
                }
                final File file = new File( dest, fname );
                writer = new OutputStreamWriter( new FileOutputStream( file ), encoding );
            } else {
                writer = new OutputStreamWriter( new FileOutputStream( dest ), encoding );
            }

            serializer.setOutput( writer, outputProperties );
            resource.getContentAsSAX( serializer );
            writer.close();

            SerializerPool.getInstance().returnObject( serializer );
View Full Code Here


            long qtime = System.currentTimeMillis() - start;
            start = System.currentTimeMillis();
           
            Properties outputProperties = new Properties();
            outputProperties.setProperty(OutputKeys.INDENT, "yes");
            SAXSerializer serializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
            serializer.setOutput(new OutputStreamWriter(System.out), outputProperties);
           
            for ( int i = 0; i < (int) result.getSize(); i++ ) {
                XMLResource resource = (XMLResource) result.getResource( (long) i );
                resource.getContentAsSAX(serializer);
            }
View Full Code Here

    }

    @Override
    public void serialize(OutputStream out, Properties params) throws HttpClientException {
       
        SAXSerializer sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        params.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
       
        try {
            SequenceIterator itSequence = SequenceIterator.EMPTY_ITERATOR;
            if(sequence != null) {
                itSequence = sequence.iterate();
            }
           
            String encoding = params.getProperty(OutputKeys.ENCODING, "UTF-8");
            Writer writer = new OutputStreamWriter(out, encoding);
            sax.setOutput(writer, params);
            Serializer serializer = context.getBroker().getSerializer();
            serializer.reset();
            serializer.setProperties(params);
            serializer.setSAXHandlers(sax, sax);

            sax.startDocument();
           
            while(itSequence.hasNext()) {
               NodeValue next = (NodeValue)itSequence.nextItem();
               serializer.toSAX(next)
            }
           
            sax.endDocument();
            writer.close();
        } catch(SAXException saxe) {
            throw new HttpClientException("A problem occurred while serializing the node set: " + saxe.getMessage(), saxe);
        } catch(IOException ioe) {
            throw new HttpClientException("A problem occurred while serializing the node set: " + ioe.getMessage(), ioe);
View Full Code Here

    }
   
    private void serialize(SequenceIterator siNode, Properties outputProperties, OutputStream os) throws XPathException
    {
        // serialize the node set
        final SAXSerializer sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        outputProperties.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
        try
        {
            final String encoding = outputProperties.getProperty(OutputKeys.ENCODING, "UTF-8");
            final Writer writer = new OutputStreamWriter(os, encoding);
            sax.setOutput(writer, outputProperties);
            final Serializer serializer = context.getBroker().getSerializer();
            serializer.reset();
            serializer.setProperties(outputProperties);
            serializer.setSAXHandlers(sax, sax);

            sax.startDocument();
           
            while(siNode.hasNext())
            {
             final NodeValue next = (NodeValue)siNode.nextItem();
               serializer.toSAX(next)
            }
           
            sax.endDocument();
            writer.close();
        }
        catch(final SAXException e)
        {
            throw new XPathException(this, "A problem occurred while serializing the node set: " + e.getMessage(), e);
View Full Code Here

          resource.setContent(((BinaryValue)item).toJavaObject());

        } else if(Type.subTypeOf(item.getType(), Type.NODE)) {
          if(binary) {
            final StringWriter writer = new StringWriter();
            final SAXSerializer serializer = new SAXSerializer();
            serializer.setOutput(writer, null);
            item.toSAX(context.getBroker(), serializer, SERIALIZATION_PROPERTIES);
            resource.setContent(writer.toString());
          } else {
            final ContentHandler handler = ((XMLResource)resource).setContentAsSAX();
            handler.startDocument();
View Full Code Here

               
                else
                    {response.setContentType(mediaType);}
            }

            final SAXSerializer sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
            final Writer writer = new BufferedWriter(response.getWriter());
            sax.setOutput(writer, properties);

            final SAXResult result = new SAXResult(sax);
            handler.setResult(result);
           
            final Serializer serializer = broker.getSerializer();
View Full Code Here

            if (broker.isReadOnly()) {
                //database in read-only mode & there no configuration file,
                //create in memory document & configuration
                try {
                    final StringWriter writer = new StringWriter();
                    final SAXSerializer serializer = new SAXSerializer(writer, null);
                    serializer.startDocument();
                    serialize(instance, serializer);
                    serializer.endDocument();
                    final String data = writer.toString();
                    if (data == null || data.length() == 0) {
                        return null;
                    }
                    return parse(new ByteArrayInputStream(data.getBytes(UTF_8)));
View Full Code Here

    protected static Set<FullXmldbURI> saving = new HashSet<FullXmldbURI>();

    public static DocumentAtExist save(final Configurable instance, final DBBroker broker, final Collection collection, final XmldbURI uri) throws IOException, ConfigurationException {
       
        final StringWriter writer = new StringWriter();
        final SAXSerializer serializer = new SAXSerializer(writer, null);
       
        try {
            serializer.startDocument();
            serialize(instance, serializer);
            serializer.endDocument();
           
        } catch (final SAXException saxe) {
            throw new ConfigurationException(saxe.getMessage(), saxe);
        }
       
View Full Code Here

            broker.readBinaryResource((BinaryDocument) resource, os);
            os.flush();
        } else {
            // xml resource

            SAXSerializer sax = null;
            final Serializer serializer = broker.getSerializer();
            serializer.reset();

            //setup the http context
            final HttpContext httpContext = serializer.new HttpContext();
            final HttpRequestWrapper reqw = new HttpRequestWrapper(request, formEncoding, containerEncoding);
            httpContext.setRequest(reqw);
            httpContext.setSession(reqw.getSession(false));
            serializer.setHttpContext(httpContext);


            // Serialize the document
            try {
                sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);

                // use a stylesheet if specified in query parameters
                if (stylesheet != null) {
                    serializer.setStylesheet(resource, stylesheet);
                }
                serializer.setProperties(outputProperties);
                serializer.prepareStylesheets(resource);

                if (asMimeType != null) { // was a mime-type specified?
                    response.setContentType(asMimeType + "; charset=" + encoding);
                } else {
                    if (serializer.isStylesheetApplied()
                            || serializer.hasXSLPi(resource) != null) {

                        asMimeType = serializer.getStylesheetProperty(OutputKeys.MEDIA_TYPE);
                        if (!useDynamicContentType || asMimeType == null) {
                            asMimeType = MimeType.HTML_TYPE.getName();
                        }

                        if (LOG.isDebugEnabled()) {
                            LOG.debug("media-type: " + asMimeType);
                        }

                        response.setContentType(asMimeType + "; charset=" + encoding);
                    } else {
                        asMimeType = resource.getMetadata().getMimeType();
                        response.setContentType(asMimeType + "; charset=" + encoding);
                    }
                }
                if (asMimeType.equals(MimeType.HTML_TYPE.getName())) {
                    outputProperties.setProperty("method", "xhtml");
                    outputProperties.setProperty("media-type", "text/html; charset=" + encoding);
                    outputProperties.setProperty("indent", "yes");
                    outputProperties.setProperty("omit-xml-declaration", "no");
                }

                final OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream(), encoding);
                sax.setOutput(writer, outputProperties);
                serializer.setSAXHandlers(sax, sax);

                serializer.toSAX(resource);

                writer.flush();
View Full Code Here

        setCreatedAndLastModifiedHeaders(response, collection.getCreationTime(), collection.getCreationTime());

        final OutputStreamWriter writer =
                new OutputStreamWriter(response.getOutputStream(), encoding);

        SAXSerializer serializer = null;

        try {
            serializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);

            serializer.setOutput(writer, defaultProperties);
            final AttributesImpl attrs = new AttributesImpl();

            serializer.startDocument();
            serializer.startPrefixMapping("exist", Namespaces.EXIST_NS);
            serializer.startElement(Namespaces.EXIST_NS, "result",
                    "exist:result", attrs);

            attrs.addAttribute("", "name", "name", "CDATA", collection.getURI()
                    .toString());
            // add an attribute for the creation date as an xs:dateTime
            try {
                final DateTimeValue dtCreated =
                        new DateTimeValue(new Date(collection.getCreationTime()));
                attrs.addAttribute("", "created", "created", "CDATA",
                        dtCreated.getStringValue());
            } catch (final XPathException e) {
                // fallback to long value
                attrs.addAttribute("", "created", "created", "CDATA",
                        String.valueOf(collection.getCreationTime()));
            }

            addPermissionAttributes(attrs, collection.getPermissionsNoLock());

            serializer.startElement(Namespaces.EXIST_NS, "collection",
                    "exist:collection", attrs);

            for (final Iterator<XmldbURI> i = collection.collectionIterator(broker); i.hasNext();) {
                final XmldbURI child = i.next();
                final Collection childCollection = broker.getCollection(collection
                        .getURI().append(child));
                if (childCollection != null
                        && childCollection.getPermissionsNoLock().validate(broker.getSubject(), Permission.READ)) {
                    attrs.clear();
                    attrs.addAttribute("", "name", "name", "CDATA", child.toString());

                    // add an attribute for the creation date as an xs:dateTime
                    try {
                        final DateTimeValue dtCreated =
                                new DateTimeValue(new Date(childCollection.getCreationTime()));
                        attrs.addAttribute("", "created", "created", "CDATA", dtCreated.getStringValue());
                    } catch (final XPathException e) {
                        // fallback to long value
                        attrs.addAttribute("", "created", "created", "CDATA",
                                String.valueOf(childCollection.getCreationTime()));
                    }

                    addPermissionAttributes(attrs, childCollection.getPermissionsNoLock());
                    serializer.startElement(Namespaces.EXIST_NS, "collection", "exist:collection", attrs);
                    serializer.endElement(Namespaces.EXIST_NS, "collection", "exist:collection");
                }
            }

            for (final Iterator<DocumentImpl> i = collection.iterator(broker); i.hasNext();) {
                final DocumentImpl doc = i.next();
                if (doc.getPermissions().validate(broker.getSubject(), Permission.READ)) {
                    final XmldbURI resource = doc.getFileURI();
                    final DocumentMetadata metadata = doc.getMetadata();
                    attrs.clear();
                    attrs.addAttribute("", "name", "name", "CDATA", resource.toString());

                    // add an attribute for the creation date as an xs:dateTime
                    try {
                        final DateTimeValue dtCreated =
                                new DateTimeValue(new Date(metadata.getCreated()));
                        attrs.addAttribute("", "created", "created", "CDATA",
                                dtCreated.getStringValue());
                    } catch (final XPathException e) {
                        // fallback to long value
                        attrs.addAttribute("", "created", "created", "CDATA",
                                String.valueOf(metadata.getCreated()));
                    }

                    // add an attribute for the last modified date as an
                    // xs:dateTime
                    try {
                        final DateTimeValue dtLastModified = new DateTimeValue(
                                new Date(metadata.getLastModified()));
                        attrs.addAttribute("", "last-modified",
                                "last-modified", "CDATA", dtLastModified.getStringValue());
                    } catch (final XPathException e) {
                        // fallback to long value
                        attrs.addAttribute("", "last-modified",
                                "last-modified", "CDATA", String.valueOf(metadata.getLastModified()));
                    }

                    addPermissionAttributes(attrs, doc.getPermissions());
                    serializer.startElement(Namespaces.EXIST_NS, "resource", "exist:resource", attrs);
                    serializer.endElement(Namespaces.EXIST_NS, "resource", "exist:resource");
                }
            }

            serializer.endElement(Namespaces.EXIST_NS, "collection", "exist:collection");
            serializer.endElement(Namespaces.EXIST_NS, "result", "exist:result");

            serializer.endDocument();

            writer.flush();
            writer.close();

        } catch (final SAXException e) {
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.