Example usage for org.w3c.dom.ls LSParser parseURI

List of usage examples for org.w3c.dom.ls LSParser parseURI

Introduction

In this page you can find the example usage for org.w3c.dom.ls LSParser parseURI.

Prototype

public Document parseURI(String uri) throws DOMException, LSException;

Source Link

Document

Parse an XML document from a location identified by a URI reference [<a href='http://www.ietf.org/rfc/rfc2396.txt'>IETF RFC 2396</a>].

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0 LS 3.0");
    if (impl == null) {
        System.out.println("No DOMImplementation found !");
        System.exit(0);/*from w  w  w .ja  v a 2s  .  c o m*/
    }

    System.out.printf("DOMImplementationLS: %s\n", impl.getClass().getName());

    LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/TR/REC-xml");
    // http://www.w3.org/2001/XMLSchema
    System.out.printf("LSParser: %s\n", parser.getClass().getName());

    Document doc = parser.parseURI("");

    LSSerializer serializer = impl.createLSSerializer();
    LSOutput output = impl.createLSOutput();
    output.setEncoding("UTF-8");
    output.setByteStream(System.out);
    serializer.write(doc, output);
    System.out.println();
}

From source file:javatojs.DomUtil.java

public static Document readDocument(String uri) {
    Document document = null;/*from w  ww.j  a va  2  s.c om*/
    try {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");

        LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

        System.out.println("In readDocument uri: " + uri);
        document = builder.parseURI(uri);
        if (document == null) {
            System.out.println("Null doc returned!!!!!!!!");
        }
        System.out.println("Read toc: \n " + document);
    } catch (Exception e) {
        System.out.println("ERROR reading toc: \n ");
        e.printStackTrace();
    }
    return document;
}

From source file:com.consol.citrus.admin.service.spring.SpringBeanService.java

/**
 * Reads file import locations from Spring bean application context.
 * @param project//www.  j  a v a  2 s.c  om
 * @return
 */
public List<File> getConfigImports(File configFile, Project project) {
    LSParser parser = XMLUtils.createLSParser();

    GetSpringImportsFilter filter = new GetSpringImportsFilter(configFile);
    parser.setFilter(filter);
    parser.parseURI(configFile.toURI().toString());

    return filter.getImportedFiles();
}

From source file:com.consol.citrus.admin.service.spring.SpringBeanService.java

/**
 * Finds all bean definition elements by type and attribute values in Spring application context and
 * performs unmarshalling in order to return a list of JaxB object.
 * @param project/* w  w w  . j a va2s  .c  o m*/
 * @param type
 * @param attributes
 * @return
 */
public <T> List<T> getBeanDefinitions(File configFile, Project project, Class<T> type,
        Map<String, String> attributes) {
    List<T> beanDefinitions = new ArrayList<T>();

    List<File> importedFiles = getConfigImports(configFile, project);
    for (File importLocation : importedFiles) {
        beanDefinitions.addAll(getBeanDefinitions(importLocation, project, type, attributes));
    }

    LSParser parser = XMLUtils.createLSParser();

    GetSpringBeansFilter filter = new GetSpringBeansFilter(type, attributes);
    parser.setFilter(filter);
    parser.parseURI(configFile.toURI().toString());

    for (Element element : filter.getBeanDefinitions()) {
        beanDefinitions.add(createJaxbObjectFromElement(element));
    }

    return beanDefinitions;
}

From source file:com.consol.citrus.admin.service.spring.SpringBeanService.java

/**
 * Method updates existing Spring bean definitions in a XML application context file. Bean definition is
 * identified by its type defining class.
 *
 * @param project/*  w  w  w .  j a  v a  2 s  .  com*/
 * @param type
 * @param jaxbElement
 */
public void updateBeanDefinitions(File configFile, Project project, Class<?> type, Object jaxbElement) {
    Source xsltSource;
    Source xmlSource;
    try {
        xsltSource = new StreamSource(new ClassPathResource("transform/update-bean-type.xsl").getInputStream());
        xsltSource.setSystemId("update-bean");

        List<File> configFiles = new ArrayList<>();
        configFiles.add(configFile);
        configFiles.addAll(getConfigImports(configFile, project));

        LSParser parser = XMLUtils.createLSParser();
        GetSpringBeansFilter getBeanFilter = new GetSpringBeansFilter(type, null);
        parser.setFilter(getBeanFilter);

        for (File file : configFiles) {
            parser.parseURI(file.toURI().toString());
            if (!CollectionUtils.isEmpty(getBeanFilter.getBeanDefinitions())) {
                xmlSource = new StringSource(FileUtils.readToString(new FileInputStream(file)));

                String beanElement = type.getAnnotation(XmlRootElement.class).name();
                String beanNamespace = type.getPackage().getAnnotation(XmlSchema.class).namespace();

                //create transformer
                Transformer transformer = transformerFactory.newTransformer(xsltSource);
                transformer.setParameter("bean_element", beanElement);
                transformer.setParameter("bean_namespace", beanNamespace);
                transformer.setParameter("bean_content", getXmlContent(jaxbElement)
                        .replaceAll("(?m)^(\\s<)", getTabs(1, project.getSettings().getTabSize()) + "$1")
                        .replaceAll("(?m)^(</)", getTabs(1, project.getSettings().getTabSize()) + "$1"));

                //transform
                StringResult result = new StringResult();
                transformer.transform(xmlSource, result);
                FileUtils.writeToFile(format(result.toString(), project.getSettings().getTabSize()), file);
                return;
            }
        }
    } catch (IOException e) {
        throw new ApplicationRuntimeException(UNABLE_TO_READ_TRANSFORMATION_SOURCE, e);
    } catch (TransformerException e) {
        throw new ApplicationRuntimeException(FAILED_TO_UPDATE_BEAN_DEFINITION, e);
    }
}

From source file:com.consol.citrus.admin.service.spring.SpringBeanService.java

/**
 * Method updates an existing Spring bean definition in a XML application context file. Bean definition is
 * identified by its id or bean name./*from   ww  w  .  ja va2 s .c  o m*/
 * @param project
 * @param id
 * @param jaxbElement
 */
public void updateBeanDefinition(File configFile, Project project, String id, Object jaxbElement) {
    Source xsltSource;
    Source xmlSource;
    try {
        xsltSource = new StreamSource(new ClassPathResource("transform/update-bean.xsl").getInputStream());
        xsltSource.setSystemId("update-bean");

        List<File> configFiles = new ArrayList<>();
        configFiles.add(configFile);
        configFiles.addAll(getConfigImports(configFile, project));

        LSParser parser = XMLUtils.createLSParser();
        GetSpringBeanFilter getBeanFilter = new GetSpringBeanFilter(id, jaxbElement.getClass());
        parser.setFilter(getBeanFilter);

        for (File file : configFiles) {
            parser.parseURI(file.toURI().toString());
            if (getBeanFilter.getBeanDefinition() != null) {
                xmlSource = new StringSource(FileUtils.readToString(new FileInputStream(file)));

                //create transformer
                Transformer transformer = transformerFactory.newTransformer(xsltSource);
                transformer.setParameter("bean_id", id);
                transformer.setParameter("bean_content", getXmlContent(jaxbElement)
                        .replaceAll("(?m)^(\\s<)", getTabs(1, project.getSettings().getTabSize()) + "$1")
                        .replaceAll("(?m)^(</)", getTabs(1, project.getSettings().getTabSize()) + "$1"));

                //transform
                StringResult result = new StringResult();
                transformer.transform(xmlSource, result);
                FileUtils.writeToFile(format(result.toString(), project.getSettings().getTabSize()), file);
                return;
            }
        }
    } catch (IOException e) {
        throw new ApplicationRuntimeException(UNABLE_TO_READ_TRANSFORMATION_SOURCE, e);
    } catch (TransformerException e) {
        throw new ApplicationRuntimeException(FAILED_TO_UPDATE_BEAN_DEFINITION, e);
    }
}

From source file:com.consol.citrus.admin.service.spring.SpringBeanService.java

/**
 * Finds bean definition element by id and type in Spring application context and
 * performs unmarshalling in order to return JaxB object.
 * @param project/*from   w w w .  j  a v a2s  . c o  m*/
 * @param id
 * @param type
 * @return
 */
public <T> T getBeanDefinition(File configFile, Project project, String id, Class<T> type) {
    LSParser parser = XMLUtils.createLSParser();

    GetSpringBeanFilter filter = new GetSpringBeanFilter(id, type);
    parser.setFilter(filter);

    List<File> configFiles = new ArrayList<>();
    configFiles.add(configFile);
    configFiles.addAll(getConfigImports(configFile, project));

    for (File file : configFiles) {
        parser.parseURI(file.toURI().toString());

        if (filter.getBeanDefinition() != null) {
            return createJaxbObjectFromElement(filter.getBeanDefinition());
        }
    }

    return null;
}