Example usage for org.xml.sax ContentHandler endDocument

List of usage examples for org.xml.sax ContentHandler endDocument

Introduction

In this page you can find the example usage for org.xml.sax ContentHandler endDocument.

Prototype

public void endDocument() throws SAXException;

Source Link

Document

Receive notification of the end of a document.

Usage

From source file:org.kalypsodeegree_impl.io.sax.test.SaxParserTestUtils.java

public static <T> void marshallDocument(final XMLReader reader, final AbstractMarshaller<T> marshaller,
        final T objectToMarshall) throws SAXException {
    final ContentHandler contentHandler = reader.getContentHandler();
    contentHandler.startDocument();/*w ww  .ja va 2s .com*/
    marshaller.marshall(objectToMarshall);
    contentHandler.endDocument();
}

From source file:org.nuxeo.ecm.diff.content.adapter.HtmlContentDiffer.java

public List<Blob> getContentDiff(Blob leftBlob, Blob rightBlob, Locale locale) throws ContentDiffException {

    try {/*from  w  w  w  .  j  a  v a2 s.  c om*/
        List<Blob> blobResults = new ArrayList<Blob>();
        StringWriter sw = new StringWriter();

        SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance();
        TransformerHandler transformHandler = stf.newTransformerHandler();
        transformHandler.setResult(new StreamResult(sw));

        XslFilter htmlHeaderXslFilter = new XslFilter();

        StringBuilder sb = new StringBuilder("xslfilter/htmldiffheader");
        sb.append("_");
        sb.append(locale.getLanguage());
        sb.append(".xsl");
        String htmlHeaderXslPath = sb.toString();
        ContentHandler postProcess;
        try {
            postProcess = htmlHeaderXslFilter.xsl(transformHandler, htmlHeaderXslPath);
        } catch (IllegalStateException ise) {
            LOGGER.error(String.format(
                    "Could not find the HTML diff header xsl file '%s', falling back on the default one.",
                    htmlHeaderXslPath), ise);
            postProcess = htmlHeaderXslFilter.xsl(transformHandler, "xslfilter/htmldiffheader.xsl");
        }

        String prefix = "diff";

        HtmlCleaner cleaner = new HtmlCleaner();

        InputSource leftIS = new InputSource(leftBlob.getStream());
        InputSource rightIS = new InputSource(rightBlob.getStream());

        DomTreeBuilder leftHandler = new DomTreeBuilder();
        cleaner.cleanAndParse(leftIS, leftHandler);
        TextNodeComparator leftComparator = new TextNodeComparator(leftHandler, locale);

        DomTreeBuilder rightHandler = new DomTreeBuilder();
        cleaner.cleanAndParse(rightIS, rightHandler);
        TextNodeComparator rightComparator = new TextNodeComparator(rightHandler, locale);

        postProcess.startDocument();
        postProcess.startElement("", "diffreport", "diffreport", new AttributesImpl());
        postProcess.startElement("", "diff", "diff", new AttributesImpl());
        HtmlSaxDiffOutput output = new HtmlSaxDiffOutput(postProcess, prefix);

        HTMLDiffer differ = new HTMLDiffer(output);
        differ.diff(leftComparator, rightComparator);

        postProcess.endElement("", "diff", "diff");
        postProcess.endElement("", "diffreport", "diffreport");
        postProcess.endDocument();

        String stringBlob = sw.toString().replaceAll(NUXEO_DEFAULT_CONTEXT_PATH,
                VirtualHostHelper.getContextPathProperty());
        Blob mainBlob = Blobs.createBlob(stringBlob);
        sw.close();

        mainBlob.setFilename("contentDiff.html");
        mainBlob.setMimeType("text/html");

        blobResults.add(mainBlob);
        return blobResults;

    } catch (Exception e) {
        throw new ContentDiffException(e);
    }
}

From source file:org.onehippo.cms7.autoexport.Exporter.java

private void exportDeltaXML(InitializeItem item, ContentHandler handler)
        throws SAXException, RepositoryException {
    DeltaInstruction rootInstruction = item.getDelta().getRootInstruction();
    handler.startDocument();/*from  ww w. j a va  2  s .  c  o m*/
    handler.startPrefixMapping(SV_PREFIX, SV_URI);
    handler.startPrefixMapping(DELTA_PREFIX, DELTA_URI);
    exportInstruction(rootInstruction, handler);
    handler.endDocument();
}

From source file:org.orbeon.oxf.processor.LDAPProcessor.java

private void serialize(List results, Config config, ContentHandler ch) {
    try {/*from  w  ww. jav a2 s  . c om*/
        ch.startDocument();
        ch.startElement("", "results", "results", SAXUtils.EMPTY_ATTRIBUTES);
        for (Iterator i = results.iterator(); i.hasNext();) {
            SearchResult sr = (SearchResult) i.next();

            ch.startElement("", "result", "result", SAXUtils.EMPTY_ATTRIBUTES);
            addElement(ch, "name", sr.getName());
            try {
                addElement(ch, "fullname", sr.getNameInNamespace());
            } catch (UnsupportedOperationException e) {
                // This seems to be the only  way to know if sr contains a name!
            }
            Attributes attr = sr.getAttributes();
            NamingEnumeration attrEn = attr.getAll();
            while (attrEn.hasMoreElements()) {
                Attribute a = (Attribute) attrEn.next();
                if (config.getAttributes().isEmpty() || config.getAttributes().contains(a.getID())) {
                    ch.startElement("", "attribute", "attribute", SAXUtils.EMPTY_ATTRIBUTES);
                    addElement(ch, "name", a.getID());
                    NamingEnumeration aEn = a.getAll();
                    while (aEn.hasMoreElements()) {
                        Object o = aEn.next();
                        addElement(ch, "value", o.toString());
                    }
                    ch.endElement("", "attribute", "attribute");
                }
            }
            ch.endElement("", "result", "result");
        }
        ch.endElement("", "results", "results");
        ch.endDocument();
    } catch (Exception e) {
        throw new OXFException(e);
    }
}

From source file:org.orbeon.oxf.processor.LDAPProcessor.java

private void outputSuccess(ContentHandler ch, String operationName) {
    try {/*from  w  w w . j  a v  a2  s.c  o m*/
        ch.startDocument();
        addElement(ch, operationName, "success");
        ch.endDocument();
    } catch (Exception e) {
        throw new OXFException(e);
    }
}

From source file:uk.ac.cam.caret.sakai.rwiki.component.service.impl.XSLTEntityHandler.java

/**
 * {@inheritDoc}/* w w  w . jav a  2s . c  o  m*/
 */
public void outputContent(final Entity entity, final Entity sideBar, final HttpServletRequest request,
        final HttpServletResponse res) {
    if (!isAvailable())
        return;
    if (!(entity instanceof RWikiEntity))
        return;

    try {
        String skin = ServerConfigurationService.getString("skin.default"); //$NON-NLS-1$
        String skinRepo = ServerConfigurationService.getString("skin.repo"); //$NON-NLS-1$
        request.setAttribute("sakai.skin.repo", skinRepo); //$NON-NLS-1$
        request.setAttribute("sakai.skin", skin); //$NON-NLS-1$

        HttpSession s = request.getSession();
        PageVisits pageVisits = (PageVisits) s
                .getAttribute(XSLTEntityHandler.class.getName() + this.getMinorType() + "_visits");

        boolean withBreadcrumbs = !"0".equals(request.getParameter(breadCrumbParameter));
        if (pageVisits == null) {
            pageVisits = new PageVisits();
            s.setAttribute(XSLTEntityHandler.class.getName() + this.getMinorType() + "_visits", pageVisits);
        }
        RWikiEntity rwe = (RWikiEntity) entity;
        if (!rwe.isContainer()) {
            RWikiObject rwo = rwe.getRWikiObject();
            pageVisits.addPage(rwo.getName());
        }

        setCurrentRequest(request);

        if (responseHeaders != null) {
            for (Iterator i = responseHeaders.keySet().iterator(); i.hasNext();) {
                String name = (String) i.next();
                String value = (String) responseHeaders.get(name);
                res.setHeader(name, value);

            }

        }

        OutputStream out = res.getOutputStream();

        ContentHandler opch = getOutputHandler(out);
        ContentHandler ch = null;
        if (false) {
            ch = new DebugContentHandler(opch);
        } else {
            ch = opch;
        }

        Attributes dummyAttributes = new AttributesImpl();

        ch.startDocument();
        ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ENTITYSERVICE, SchemaNames.EL_NSENTITYSERVICE,
                dummyAttributes);

        AttributesImpl propA = new AttributesImpl();
        propA.addAttribute("", SchemaNames.ATTR_REQUEST_PATH_INFO, //$NON-NLS-1$
                SchemaNames.ATTR_REQUEST_PATH_INFO, "string", request //$NON-NLS-1$
                        .getPathInfo());
        propA.addAttribute("", SchemaNames.ATTR_REQUEST_USER, //$NON-NLS-1$
                SchemaNames.ATTR_REQUEST_USER, "string", request //$NON-NLS-1$
                        .getRemoteUser());
        propA.addAttribute("", SchemaNames.ATTR_REQUEST_PROTOCOL, //$NON-NLS-1$
                SchemaNames.ATTR_REQUEST_PROTOCOL, "string", request //$NON-NLS-1$
                        .getProtocol());
        propA.addAttribute("", SchemaNames.ATTR_REQUEST_SERVER_NAME, //$NON-NLS-1$
                SchemaNames.ATTR_REQUEST_SERVER_NAME, "string", request //$NON-NLS-1$
                        .getServerName());
        propA.addAttribute("", SchemaNames.ATTR_REQUEST_SERVER_PORT, //$NON-NLS-1$
                SchemaNames.ATTR_REQUEST_SERVER_PORT, "string", String //$NON-NLS-1$
                        .valueOf(request.getServerPort()));
        propA.addAttribute("", SchemaNames.ATTR_REQUEST_REQUEST_URL, //$NON-NLS-1$
                SchemaNames.ATTR_REQUEST_REQUEST_URL, "string", String //$NON-NLS-1$
                        .valueOf(request.getRequestURL()));

        ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_PROPERTIES,
                SchemaNames.EL_NSREQUEST_PROPERTIES, propA);
        addRequestAttributes(ch, request);
        addRequestParameters(ch, request);

        ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_PROPERTIES,
                SchemaNames.EL_NSREQUEST_PROPERTIES);

        if (withBreadcrumbs) {
            ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_PAGEVISITS, SchemaNames.EL_NSPAGEVISITS,
                    dummyAttributes);

            List<String[]> pv = pageVisits.getPageNames(this.getMinorType());

            for (Iterator<String[]> i = pv.iterator(); i.hasNext();) {
                String[] visit = i.next();
                propA = new AttributesImpl();
                propA.addAttribute("", SchemaNames.ATTR_URL, SchemaNames.ATTR_URL, "string", visit[0]);
                addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_PAGEVISIT, SchemaNames.EL_NSPAGEVISIT,
                        propA, visit[1]);
            }
            ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_PAGEVISITS, SchemaNames.EL_NSPAGEVISITS);
        }

        ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ENTITY, SchemaNames.EL_NSENTITY,
                dummyAttributes);
        ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTIES, SchemaNames.EL_NSXMLPROPERTIES,
                dummyAttributes);
        ResourceProperties rp = entity.getProperties();

        for (Iterator i = rp.getPropertyNames(); i.hasNext();) {
            Object key = i.next();
            String name = String.valueOf(key);
            String value = String.valueOf(rp.getProperty(name));
            propA = new AttributesImpl();
            propA.addAttribute("", SchemaNames.ATTR_NAME, //$NON-NLS-1$
                    SchemaNames.ATTR_NAME, "string", name); //$NON-NLS-1$
            addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTY, SchemaNames.EL_NSXMLPROPERTY,
                    propA, value);
        }
        propA = new AttributesImpl();
        propA.addAttribute("", SchemaNames.ATTR_NAME, //$NON-NLS-1$
                SchemaNames.ATTR_NAME, "string", "_handler"); //$NON-NLS-1$ //$NON-NLS-2$
        addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTY, SchemaNames.EL_NSXMLPROPERTY,
                propA, " XSLTEntity Handler"); //$NON-NLS-1$

        if (!rwe.isContainer()) {
            RWikiObject rwo = rwe.getRWikiObject();
            propA = new AttributesImpl();
            propA.addAttribute("", SchemaNames.ATTR_NAME, //$NON-NLS-1$
                    SchemaNames.ATTR_NAME, "string", "_title"); //$NON-NLS-1$ //$NON-NLS-2$
            addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTY, SchemaNames.EL_NSXMLPROPERTY,
                    propA, NameHelper.localizeName(rwo.getName(), rwo.getRealm()));

        } else {
            propA = new AttributesImpl();
            propA.addAttribute("", SchemaNames.ATTR_NAME, //$NON-NLS-1$
                    SchemaNames.ATTR_NAME, "string", "_title"); //$NON-NLS-1$ //$NON-NLS-2$
            addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTY, SchemaNames.EL_NSXMLPROPERTY,
                    propA, entity.getReference());

        }
        ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTIES, SchemaNames.EL_NSXMLPROPERTIES);

        /* http://jira.sakaiproject.org/browse/SAK-13281
         * escapeXML is controlled via config settings
         */

        if (!rwe.isContainer()) {
            RWikiObject rwo = rwe.getRWikiObject();
            ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_RENDEREDCONTENT,
                    SchemaNames.EL_NSRENDEREDCONTENT, dummyAttributes);

            renderToXML(rwo, ch, withBreadcrumbs, this.escaped);
            ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_RENDEREDCONTENT,
                    SchemaNames.EL_NSRENDEREDCONTENT);
        }
        ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ENTITY, SchemaNames.EL_NSENTITY);

        if (sideBar != null && sideBar instanceof RWikiEntity) {

            RWikiEntity sbrwe = (RWikiEntity) sideBar;
            RWikiObject sbrwo = sbrwe.getRWikiObject();
            ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_SIDEBAR, SchemaNames.EL_NSSIDEBAR,
                    dummyAttributes);

            ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTIES,
                    SchemaNames.EL_NSXMLPROPERTIES, dummyAttributes);
            propA = new AttributesImpl();
            propA.addAttribute("", SchemaNames.ATTR_NAME, //$NON-NLS-1$
                    SchemaNames.ATTR_NAME, "string", "_title"); //$NON-NLS-1$ //$NON-NLS-2$
            addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTY, SchemaNames.EL_NSXMLPROPERTY,
                    propA, NameHelper.localizeName(sbrwo.getName(), sbrwo.getRealm()));
            ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTIES,
                    SchemaNames.EL_NSXMLPROPERTIES);

            ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_RENDEREDCONTENT,
                    SchemaNames.EL_NSRENDEREDCONTENT, dummyAttributes);
            renderToXML(sbrwo, ch, withBreadcrumbs, this.escaped);
            ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_RENDEREDCONTENT,
                    SchemaNames.EL_NSRENDEREDCONTENT);

            ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_SIDEBAR, SchemaNames.EL_NSSIDEBAR);

        }

        ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ENTITYSERVICE, SchemaNames.EL_NSENTITYSERVICE);

        ch.endDocument();

    } catch (Throwable ex) {
        log.info("Failed to serialize " + ex.getMessage()); //$NON-NLS-1$
        ex.printStackTrace();
        throw new RuntimeException(Messages.getString("XSLTEntityHandler.68") //$NON-NLS-1$
                + ex.getLocalizedMessage(), ex);
    } finally {
        setCurrentRequest(null);
    }
}

From source file:uk.ac.ebi.arrayexpress.utils.saxon.FlatFileXMLReader.java

public void parse(InputSource input) throws IOException, SAXException {
    int headerRows = getIntOptionValue(OPTION_HEADER_ROWS, 0);
    int page = getIntOptionValue(OPTION_PAGE, 0);
    int pageSize = getIntOptionValue(OPTION_PAGE_SIZE, -1);
    Integer sortBy = getIntOptionValue(OPTION_SORT_BY, null);
    String sortOrder = getStringOptionValue(OPTION_SORT_ORDER, "a");

    ContentHandler ch = getContentHandler();
    if (null == ch) {
        return;//from  ww  w . j  av  a  2  s . co m
    }

    Reader inStream;
    if (input.getCharacterStream() != null) {
        inStream = input.getCharacterStream();
    } else if (input.getByteStream() != null) {
        inStream = new InputStreamReader(input.getByteStream());
    } else if (input.getSystemId() != null) {
        URL url = new URL(input.getSystemId());
        inStream = new InputStreamReader(url.openStream());
    } else {
        throw new SAXException("Invalid InputSource object");
    }

    CSVReader ffReader = new CSVReader(new BufferedReader(inStream), this.columnDelimiter,
            this.columnQuoteChar);

    List<String[]> ff = ffReader.readAll();
    int cols = ff.size() > 0 ? ff.get(0).length : 0;

    // verify that sort by column is with in range of columns
    // if not then sort will not be performed
    // else - switch from 1-based to 0-based index
    if (null != sortBy) {
        if (sortBy < 1 || sortBy > cols) {
            sortBy = null;
        } else {
            sortBy = sortBy - 1;
        }
    }

    // 1. removes all dodgy rows (that have less columns than the first one)
    // 2. determines if column to be sorted is numeric
    ColDataType sortColDataType = ColDataType.INTEGER;
    int colTypeSkipRows = headerRows;
    for (Iterator<String[]> iterator = ff.iterator(); iterator.hasNext();) {
        String[] row = iterator.next();
        if (row.length != cols || isRowBlank(row)) {
            iterator.remove();
        } else {
            if (null != sortBy && 0 == colTypeSkipRows && ColDataType.STRING != sortColDataType) {
                ColDataType dataType = getColDataType(row[sortBy]);

                // downgrade from int to decimal or string
                if (ColDataType.INTEGER == sortColDataType && ColDataType.INTEGER != dataType) {
                    sortColDataType = dataType;
                }
                // downgrade from decimal to string only
                if (ColDataType.DECIMAL == sortColDataType && ColDataType.STRING == dataType) {
                    sortColDataType = dataType;
                }
            }
            if (colTypeSkipRows > 0) {
                colTypeSkipRows--;
            }
        }
    }

    int rows = ff.size() > 0 ? ff.size() - headerRows : 0;

    if (-1 == pageSize) {
        page = 1;
        pageSize = rows;
    }

    ch.startDocument();

    AttributesImpl tableAttrs = new AttributesImpl();
    tableAttrs.addAttribute(EMPTY_NAMESPACE, "rows", "rows", CDATA_TYPE, String.valueOf(rows));
    ch.startElement(EMPTY_NAMESPACE, "table", "table", tableAttrs);

    for (Iterator<String[]> iterator = ff.iterator(); iterator.hasNext() && headerRows > 0; headerRows--) {
        String[] row = iterator.next();
        outputRow(ch, true, null, row);
        iterator.remove();
    }

    if (null != sortBy) {
        Collections.sort(ff, new SortColumnComparator(sortBy, sortOrder, sortColDataType));
    }

    int rowSeq = 1;
    for (String[] row : ff) {
        if (rowSeq > (pageSize * (page - 1)) && rowSeq <= (pageSize * page)) {
            outputRow(ch, false, String.valueOf(rowSeq), row);
        }
        ++rowSeq;
    }

    ch.endElement(EMPTY_NAMESPACE, "table", "table");
    ch.endDocument();
}