Example usage for org.xml.sax XMLReader setContentHandler

List of usage examples for org.xml.sax XMLReader setContentHandler

Introduction

In this page you can find the example usage for org.xml.sax XMLReader setContentHandler.

Prototype

public void setContentHandler(ContentHandler handler);

Source Link

Document

Allow an application to register a content event handler.

Usage

From source file:org.kalypso.gml.test.GmlParsingTester.java

protected GMLWorkspace parseGml(final InputStream is, final URL context)
        throws ParserConfigurationException, SAXException, IOException, GMLException {
    final SAXParser saxParser = m_saxFactory.newSAXParser();
    // make namespace-prefxes visible to content handler
    // used to allow necessary schemas from gml document
    // saxParser.setProperty( "http://xml.org/sax/features/namespace-prefixes", Boolean.TRUE );
    final XMLReader reader = saxParser.getXMLReader();

    // TODO: also set an error handler here
    // TODO: use progress-monitors to show progress and let the user cancel parsing

    final GMLorExceptionContentHandler exceptionHandler = new GMLorExceptionContentHandler(reader, null,
            context, null);/*from  w  ww  .  j  av a  2  s  .co m*/
    reader.setContentHandler(exceptionHandler);

    final InputSource inputSource = new InputSource(is);
    reader.parse(inputSource);

    return exceptionHandler.getWorkspace();
}

From source file:org.kalypso.gmlschema.types.AbstractOldFormatMarshallingTypeHandlerAdapter.java

/**
 * @see org.kalypso.gmlschema.types.IMarshallingTypeHandler#marshal(java.lang.Object, org.xml.sax.ContentHandler,
 *      org.xml.sax.ext.LexicalHandler, java.net.URL)
 *//* w  w w  .ja v a 2 s .c om*/
@Override
public void marshal(final Object value, final XMLReader reader, final URL context, final String gmlVersion)
        throws SAXException {
    try {
        final Document document = m_builder.newDocument();
        final Node node = marshall(value, document, context);
        // value is encoded in xml in document object
        final StringWriter writer = new StringWriter();
        // REMARK: we do not write the dom formatted here (argument false), because later the
        // content handler will probably do the formatting (IndentContentHandler). If we format here, we will get empty
        // lines later.
        XMLHelper.writeDOM(node, "UTF-8", writer, false); //$NON-NLS-1$
        IOUtils.closeQuietly(writer);
        // value is encoded in string
        final String xmlString = writer.toString();
        final String xmlStringNoHeader = XMLUtilities.removeXMLHeader(xmlString);
        final InputSource input = new InputSource(new StringReader(xmlStringNoHeader));

        SAX_FACTORY.setNamespaceAware(true);
        final SAXParser saxParser = SAX_FACTORY.newSAXParser();
        final XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setContentHandler(reader.getContentHandler());
        xmlReader.parse(input);
    } catch (final SAXException e) {
        throw e;
    } catch (final Exception e) {
        throw new SAXException(e);
    }
}

From source file:org.kalypso.gmlschema.types.AbstractOldFormatMarshallingTypeHandlerAdapter.java

@Override
public void unmarshal(final XMLReader reader, final URL context, final UnmarshallResultEater marshalResultEater,
        final String gmlVersion) throws TypeRegistryException {
    // xml to memory
    try {/*from   ww  w  .  ja va  2 s .c  om*/
        final UnmarshallResultEater eater = new UnmarshallResultEater() {
            @Override
            public void unmarshallSuccesful(final Object value) throws SAXParseException {
                final Node node = (Node) value;
                if (node == null)
                    marshalResultEater.unmarshallSuccesful(null);
                else {
                    Object object = null;
                    try {
                        object = unmarshall(node, context, UrlResolverSingleton.getDefault());
                    } catch (final TypeRegistryException e) {
                        e.printStackTrace();
                    }
                    marshalResultEater.unmarshallSuccesful(object);
                }
            }
        };

        final Document document = m_builder.newDocument();
        final DOMConstructor domBuilderContentHandler = new DOMConstructor(document, eater);
        // simulate property-tag for dombuilder
        reader.setContentHandler(domBuilderContentHandler);
    } catch (final Exception e) {
        throw new TypeRegistryException(e);
    }
}

From source file:org.kalypso.gmlschema.types.SimpleDOMTypeHandler.java

@Override
public void marshal(final Object value, final XMLReader reader, final URL context, final String gmlVersion)
        throws SAXException {
    try {/*from  ww  w .  jav a2 s.co m*/
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        final DocumentBuilder builder = factory.newDocumentBuilder();
        final Document document = builder.newDocument();
        final Node node = internalMarshall(value, document, context);

        // value is encoded in xml in document object
        final StringWriter writer = new StringWriter();
        XMLHelper.writeDOM(node, "UTF-8", writer); //$NON-NLS-1$
        IOUtils.closeQuietly(writer);

        // value is encoded in string
        final String xmlString = writer.toString();
        final InputSource input = new InputSource(new StringReader(xmlString));
        final SAXParserFactory saxFac = SAXParserFactory.newInstance();
        saxFac.setNamespaceAware(true);

        final SAXParser saxParser = saxFac.newSAXParser();
        final XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setContentHandler(reader.getContentHandler());
        xmlReader.parse(input);
    } catch (final SAXException saxe) {
        throw saxe;
    } catch (final Exception e) {
        throw new SAXException(e);
    }
}

From source file:org.kalypso.gmlschema.types.SimpleDOMTypeHandler.java

@Override
public void unmarshal(final XMLReader reader, final URL context, final UnmarshallResultEater marshalResultEater,
        final String gmlVersion) throws TypeRegistryException {
    try {//ww w  .j  ava2  s .  c  o m
        final UnmarshallResultEater eater = new UnmarshallResultEater() {
            @Override
            public void unmarshallSuccesful(final Object value) throws SAXParseException {
                final Node node = (Node) value;
                if (node == null)
                    marshalResultEater.unmarshallSuccesful(null);
                else {
                    try {
                        final Object object = internalUnmarshall(node);
                        marshalResultEater.unmarshallSuccesful(object);
                    } catch (final TypeRegistryException e) {
                        throw new SAXParseException(e.getLocalizedMessage(), null, e);
                    }
                }
            }
        };

        // FIXME: use transformer instead of self-made stuff

        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);

        final DocumentBuilder builder = factory.newDocumentBuilder();
        final Document document = builder.newDocument();
        final DOMConstructor domBuilderContentHandler = new DOMConstructor(document, eater);

        // simulate property-tag for dombuilder
        reader.setContentHandler(domBuilderContentHandler);
    } catch (final Exception e) {
        throw new TypeRegistryException(e);
    }
}

From source file:org.kalypso.kalypsomodel1d2d.conv.test.MarshallPolyhedralSurfaceTest.java

@Test
public void testWritePolyhedralSurface() throws Exception {
    final GM_PolygonPatch[] polygons = createPolygons();
    final GM_PolyhedralSurface<GM_PolygonPatch> surface = GeometryFactory.createGM_PolyhedralSurface(polygons,
            crs);/*from ww  w  .j  ava 2  s .  co m*/

    File polyFile = null;
    OutputStream os = null;
    try {
        polyFile = File.createTempFile("polyTest", ".gml"); //$NON-NLS-1$ //$NON-NLS-2$
        polyFile.deleteOnExit();

        /* Output: to stream */
        os = new BufferedOutputStream(new FileOutputStream(polyFile));
        assertNotNull(os);

        final ToXMLStream xmlStream = new ToXMLStream();
        xmlStream.setOutputStream(os);
        // Configure content handler. IMPORTANT: call after setOutputStream!
        xmlStream.setLineSepUse(true);
        xmlStream.setIndent(true);
        xmlStream.setIndentAmount(1);
        xmlStream.setEncoding("UTF-8"); //$NON-NLS-1$

        final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setContentHandler(xmlStream);

        xmlStream.startDocument();

        final PolyhedralSurfaceMarshaller marshaller = new PolyhedralSurfaceMarshaller(xmlReader);
        marshaller.marshall(surface);

        xmlStream.endDocument();

        os.close();

        final String xmlString = FileUtils.readFileToString(polyFile, "UTF-8"); //$NON-NLS-1$
        System.out.println(xmlString);
    } finally {
        polyFile.delete();
        IOUtils.closeQuietly(os);
    }
}

From source file:org.kalypso.kalypsomodel1d2d.conv.test.MarshallTriangulatedSurfaceTest.java

private XMLReader initMarshalling(final OutputStream os) throws SAXException {
    m_xmlStream = new ToXMLStream();
    m_xmlStream.setOutputStream(os);/*from  w w  w. ja v  a  2 s. c om*/
    // Configure content handler. IMPORTANT: call after setOutputStream!
    m_xmlStream.setLineSepUse(true);
    m_xmlStream.setIndent(true);
    m_xmlStream.setIndentAmount(1);
    m_xmlStream.setEncoding("UTF-8"); //$NON-NLS-1$

    final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    xmlReader.setContentHandler(m_xmlStream);

    m_xmlStream.startDocument();

    return xmlReader;
}

From source file:org.kalypso.mapserver.utils.MapFileUtilities.java

/**
 * This function loads a map file from XML.
 *
 * @param inputStream//from w  w  w.j  av a2  s . c  om
 *          The input stream.
 * @return The contents of the map file.
 */
public static Map loadFromXML(final InputStream inputStream)
        throws JAXBException, SAXException, ParserConfigurationException, IOException {
    /* Create the unmarshaller. */
    final Unmarshaller unmarshaller = JC.createUnmarshaller();

    /* Get the sax parser factory. */
    final SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);
    spf.setXIncludeAware(true);

    /* Get the xml reader. */
    final XMLReader xr = spf.newSAXParser().getXMLReader();
    xr.setContentHandler(unmarshaller.getUnmarshallerHandler());
    xr.parse(new InputSource(inputStream));

    return (Map) unmarshaller.getUnmarshallerHandler().getResult();
}

From source file:org.kalypso.ogc.gml.GisTemplateHelper.java

public static final Gismapview loadGisMapView(final InputSource is)
        throws JAXBException, SAXException, ParserConfigurationException, IOException {
    final Unmarshaller unmarshaller = TemplateUtilities.createGismapviewUnmarshaller();

    // XInclude awareness
    final SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);//from   www  . j av  a  2 s  .  com
    spf.setXIncludeAware(true);
    final XMLReader xr = spf.newSAXParser().getXMLReader();
    xr.setContentHandler(unmarshaller.getUnmarshallerHandler());
    xr.parse(is);
    return (Gismapview) unmarshaller.getUnmarshallerHandler().getResult();
}

From source file:org.kalypso.ogc.gml.serialize.GmlSerializer.java

public static GMLWorkspace createGMLWorkspace(final InputSource inputSource, final URL schemaLocationHint,
        final URL context, final IFeatureProviderFactory factory)
        throws ParserConfigurationException, SAXException, IOException, GMLException {
    TimeLogger perfLogger = null;/*w w w.jav  a 2s .  c  o m*/
    if (KalypsoCoreDebug.PERF_SERIALIZE_GML.isEnabled())
        perfLogger = new TimeLogger(Messages.getString("org.kalypso.ogc.gml.serialize.GmlSerializer.7")); //$NON-NLS-1$

    final IFeatureProviderFactory providerFactory = factory == null ? DEFAULT_FACTORY : factory;
    final XMLReader xmlReader = createXMLReader();

    // TODO: also set an error handler here

    final GMLorExceptionContentHandler exceptionHandler = new GMLorExceptionContentHandler(xmlReader,
            schemaLocationHint, context, providerFactory);
    xmlReader.setContentHandler(exceptionHandler);

    xmlReader.parse(inputSource);

    final GMLWorkspace workspace = exceptionHandler.getWorkspace();

    if (perfLogger != null) {
        perfLogger.takeInterimTime();
        perfLogger.printCurrentTotal(Messages.getString("org.kalypso.ogc.gml.serialize.GmlSerializer.8")); //$NON-NLS-1$
    }

    return workspace;
}