Example usage for org.dom4j.io SAXReader SAXReader

List of usage examples for org.dom4j.io SAXReader SAXReader

Introduction

In this page you can find the example usage for org.dom4j.io SAXReader SAXReader.

Prototype

public SAXReader() 

Source Link

Usage

From source file:com.easyjf.container.impl.SingletonContainerLoader.java

License:Apache License

/**
 * Obtain an instance of {@link Container} through the special configFile .
 * //www  . j av  a2 s . com
 * <p>
 * If the container is not initialized , then initial it .
 * 
 * @param configFile
 *            bean's configuration file
 * 
 * @return an instance of {@link Container}
 */
public static Container getInstance(String configFile) {

    Container container = null;

    synchronized (SingletonContainerLoader.class) {

        container = containers.get(configFile);
        if (container == null) {
            // TODO ???

            // ?EasyJWeb?Loader?Loader?

            // DefaultContainer container =
            // ContainerLoader.getInstance(configFile);
            Document doc = null;
            try {
                doc = new SAXReader().read(SingletonContainerLoader.class.getResourceAsStream(configFile));
            } catch (DocumentException e) {
                throw new IllegalArgumentException(configFile + "is not a valid config file !");
            }

            // ?DefaultContainer??Container
            DefaultContainer defaultContainer = new DefaultContainer();
            defaultContainer.registerBeanDefinitions(BeanConfigReader.parseBeansFromDocument(doc));
            defaultContainer.refresh();

            container = defaultContainer;

            containers.put(configFile, container);
        }
    }

    return container;

}

From source file:com.easyjf.generator.AllGenerator.java

License:Apache License

private Document getDocument(String xmlFileName) throws DocumentException {
    SAXReader reader = new SAXReader();
    File file = new File(xmlFileName);
    if (file.exists()) {
        return reader.read(xmlFileName);
    } else {//w w  w.j a va2  s.  co  m
        return DocumentHelper.createDocument();
    }
}

From source file:com.easyjf.web.config.XMLConfigFactory.java

License:Apache License

/**
 * @deprecated/*from   ww  w  . j a  v  a2 s. com*/
 * 
 */
public XMLConfigFactory() {
    try {
        File file = new File(Globals.CONFIG_FILE_FULL_PATH);
        if (file.exists()) {
            FileInputStream in = new FileInputStream(file);
            SAXReader reader = new SAXReader();
            doc = reader.read(in);
        } else {
            logger.warn(I18n.getLocaleMessage("core.web.unable.to.find.configuration.file")
                    + Globals.CONFIG_FILE_FULL_PATH + I18n.getLocaleMessage(
                            "core.web.EasyJWEB.will.automatically.use.the.default.path.processing.Action"));
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.warn(I18n.getLocaleMessage("core.web.configuration.file.errors") + e);
    }
}

From source file:com.easyjf.web.config.XMLConfigFactory.java

License:Apache License

public XMLConfigFactory(InputStream in) {
    try {/*from w ww  .j  a  v a 2 s . co m*/

        SAXReader reader = new SAXReader();
        doc = reader.read(in);
    } catch (Exception e) {
        e.printStackTrace();
        //         logger.warn("??" + e);
        logger.warn(I18n.getLocaleMessage("core.web.unable.to.find.configuration.file")
                + Globals.CONFIG_FILE_FULL_PATH + ","
                + I18n.getLocaleMessage("core.web.configuration.file.errors") + I18n.getLocaleMessage(
                        "core.web.EasyJWEB.will.automatically.use.the.default.path.processing.Action"));
    }
}

From source file:com.easyjf.web.config.XMLConfigFactory.java

License:Apache License

public Document parse(String fileName) throws DocumentException {
    SAXReader reader = new SAXReader();
    File file = new File(fileName);
    Document document = file.exists() ? reader.read(fileName) : null;
    return document;
}

From source file:com.erpak.jtrackplanner3d.xml.LibraryReader.java

License:Open Source License

/**
 * create a new LibraryReader object with the library file name
 * @param fileName Name of the library file
 *//*from   w w  w  .j a  va  2s  .  c  o  m*/
public LibraryReader(File libraryFile) {

    TrackSystem trackSystem = null;
    TracksTreeCellRenderer renderer = new TracksTreeCellRenderer();
    try {
        renderer.setStraigthTrackIcon(new ImageIcon(ResourcesManager.getResource("StraigthTrackIcon")));
        renderer.setCurvedTrackIcon(new ImageIcon(ResourcesManager.getResource("CurvedTrackIcon")));
        renderer.setStraightTurnoutTrackIcon(
                new ImageIcon(ResourcesManager.getResource("StraigthTurnoutTrackIcon")));
        renderer.setCrossingTrackIcon(new ImageIcon(ResourcesManager.getResource("CrossingTrackIcon")));
    } catch (Exception ex) {
        logger.error("Unable to load tree node icons");
        logger.error(ex.toString());
    }

    try {
        Element trackSystemInfoNode;
        Element element;
        Element subElement;
        Attribute attribute;
        Iterator iter;
        DefaultMutableTreeNode treeTop;
        DefaultMutableTreeNode treeCategory;
        DefaultMutableTreeNode treeElement;

        // Load the xml library file
        InputStream in = new FileInputStream(libraryFile);
        EntityResolver resolver = new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId) {
                logger.debug("Public id : " + publicId);
                logger.debug("System Id : " + systemId);
                if (publicId.equals("-//TrackPlanner//DTD track_system V 1.0//EN")) {
                    InputStream in = getClass().getResourceAsStream("ressources/track_system.dtd");
                    return new InputSource(in);
                }
                return null;
            }
        };
        SAXReader reader = new SAXReader();
        reader.setEntityResolver(resolver);
        // Read the xml library file
        Document document = reader.read(in);
        Element root = document.getRootElement();
        logger.debug("Root name : " + root.getName());

        // Get "track_system_info" node
        trackSystemInfoNode = root.element("track_system_infos");
        // Get track_system_info_node attributes 
        trackSystem = new TrackSystem(trackSystemInfoNode.attribute("name").getValue(),
                trackSystemInfoNode.attribute("version").getValue());
        trackSystem.setScale(trackSystemInfoNode.attribute("scale").getValue());
        trackSystem.setManufacturer(trackSystemInfoNode.attribute("manufacturer").getValue());
        // Get the "track_system_caracteristics" node
        element = trackSystemInfoNode.element("track_system_caracteristics");
        trackSystem.setBallastWidth(Float.parseFloat(element.attribute("ballast_width").getValue()));
        trackSystem.setTrackWidth(Float.parseFloat(element.attribute("railway_width").getValue()));
        // Get the "track_system_colors" node
        element = trackSystemInfoNode.element("track_system_colors");
        for (iter = element.elementIterator(); iter.hasNext();) {
            subElement = (Element) iter.next();
            trackSystem.addColor(subElement.attribute("name").getValue(), subElement.attribute("r").getValue(),
                    subElement.attribute("g").getValue(), subElement.attribute("b").getValue());
        }

        // Set the top node of tree with tracksystem
        treeTop = new DefaultMutableTreeNode(trackSystem.getName());

        // iterate "straight_tracks" node
        element = root.element("straight_tracks");
        treeCategory = new DefaultMutableTreeNode(element.getName());
        treeTop.add(treeCategory);
        logger.debug("Node : " + element.getName());
        for (iter = element.elementIterator("straight_track"); iter.hasNext();) {
            subElement = (Element) iter.next();
            logger.debug("Adding reference : " + subElement.attribute("reference").getValue());
            treeElement = new DefaultMutableTreeNode(new StraightTrackSymbol(trackSystem,
                    subElement.attribute("reference").getValue(), subElement.attribute("reference").getValue(),
                    subElement.attribute("reference").getValue(),
                    Float.parseFloat(subElement.attribute("length").getValue()),
                    subElement.attribute("color").getValue()));
            treeCategory.add(treeElement);
        }

        // iterate "curved_tracks" node
        element = root.element("curved_tracks");
        treeCategory = new DefaultMutableTreeNode(element.getName());
        treeTop.add(treeCategory);
        logger.debug("Node : " + element.getName());
        for (iter = element.elementIterator("curved_track"); iter.hasNext();) {
            subElement = (Element) iter.next();
            logger.debug("Adding reference : " + subElement.attribute("reference").getValue());
            treeElement = new DefaultMutableTreeNode(new CurvedTrackSymbol(trackSystem,
                    subElement.attribute("reference").getValue(), subElement.attribute("reference").getValue(),
                    subElement.attribute("reference").getValue(),
                    Float.parseFloat(subElement.attribute("radius").getValue()),
                    Float.parseFloat(subElement.attribute("angle").getValue()),
                    subElement.attribute("color").getValue()));
            treeCategory.add(treeElement);
        }

        // iterate "straight_turnouts" node
        element = root.element("straight_turnouts");
        treeCategory = new DefaultMutableTreeNode(element.getName());
        treeTop.add(treeCategory);
        logger.debug("Node : " + element.getName());
        for (iter = element.elementIterator("straight_turnout"); iter.hasNext();) {
            subElement = (Element) iter.next();
            logger.debug("Adding reference : " + subElement.attribute("reference").getValue());
            treeElement = new DefaultMutableTreeNode(new StraightTurnoutSymbol(trackSystem,
                    subElement.attribute("reference").getValue(), subElement.attribute("reference").getValue(),
                    subElement.attribute("reference").getValue(),
                    Float.parseFloat(subElement.attribute("length").getValue()),
                    Float.parseFloat(subElement.attribute("radius").getValue()),
                    Float.parseFloat(subElement.attribute("angle").getValue()),
                    subElement.attribute("direction").getValue(), subElement.attribute("color").getValue()));
            treeCategory.add(treeElement);
        }

        // iterate through "curved_turnouts" node
        element = root.element("curved_turnouts");
        treeCategory = new DefaultMutableTreeNode(element.getName());
        treeTop.add(treeCategory);
        logger.debug("Node : " + element.getName());
        for (Iterator i = root.elementIterator("curved_turnouts"); i.hasNext();) {
            element = (Element) i.next();
            logger.debug("Node : " + element.getName());
        }

        // iterate through "three_way_turnouts" node
        element = root.element("three_way_turnouts");
        treeCategory = new DefaultMutableTreeNode(element.getName());
        treeTop.add(treeCategory);
        logger.debug("Node : " + element.getName());
        for (iter = element.elementIterator("symetric_three_way_turnout"); iter.hasNext();) {
            subElement = (Element) iter.next();
            logger.debug("Adding reference : " + subElement.attribute("reference").getValue());
            treeElement = new DefaultMutableTreeNode(new SymetricThreeWayTurnoutSymbol(trackSystem,
                    subElement.attribute("reference").getValue(), subElement.attribute("reference").getValue(),
                    subElement.attribute("reference").getValue(),
                    Float.parseFloat(subElement.attribute("length").getValue()),
                    Float.parseFloat(subElement.attribute("radius").getValue()),
                    Float.parseFloat(subElement.attribute("angle").getValue()),
                    subElement.attribute("color").getValue()));
            treeCategory.add(treeElement);
        }

        // iterate through "crossings" node
        element = root.element("crossings");
        treeCategory = new DefaultMutableTreeNode(element.getName());
        treeTop.add(treeCategory);
        logger.debug("Node : " + element.getName());
        for (Iterator i = root.elementIterator("crossings"); i.hasNext();) {
            element = (Element) i.next();
            logger.debug("Node : " + element.getName());
        }

        // iterate through "double_slip_switchs" node
        element = root.element("double_slip_switchs");
        treeCategory = new DefaultMutableTreeNode(element.getName());
        treeTop.add(treeCategory);
        logger.debug("Node : " + element.getName());
        for (Iterator i = root.elementIterator("double_slip_switchs"); i.hasNext();) {
            element = (Element) i.next();
            logger.debug("Node : " + element.getName());
        }

        // iterate through "special_tracks" node
        element = root.element("special_tracks");
        treeCategory = new DefaultMutableTreeNode(element.getName());
        treeTop.add(treeCategory);
        logger.debug("Node : " + element.getName());
        for (Iterator i = root.elementIterator("special_tracks"); i.hasNext();) {
            element = (Element) i.next();
            logger.debug("Node : " + element.getName());
        }

        // fill the tree
        tree = new JTree(treeTop);
    } catch (Exception ex) {
        logger.error("Unable to load track trackSystem library", ex);
        // Todo : Throws an exception and display an error box instaed of displaying atree with scrap data
        tree = new JTree();
    }

    logger.debug("Parsing done for trackSystem : " + trackSystem);
    tree.setName("Track systems");
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setCellRenderer(renderer);
}

From source file:com.eurelis.opencms.ant.task.ManifestBuilderTask.java

License:Open Source License

private void insertExplorerTypes(Element module) {
    if (null != explorertypes) {
        File xml = new File(explorertypes);
        SAXReader reader = new SAXReader();
        Document doc;/*from   w ww .java2  s . c o  m*/
        try {
            doc = reader.read(xml);
            Element root = doc.getRootElement();
            module.add(root);
        } catch (DocumentException e) {
            module.addElement("explorertypes");
        }

    }
}

From source file:com.eurelis.opencms.ant.task.ManifestBuilderTask.java

License:Open Source License

private void insertResourceTypes(Element module) {
    if (null != resourcetypes) {
        File xml = new File(resourcetypes);
        SAXReader reader = new SAXReader();
        Document doc;//from  w w  w  . ja va 2 s .c om
        try {
            doc = reader.read(xml);
            Element root = doc.getRootElement();
            module.add(root);
        } catch (DocumentException e) {
            module.addElement("resourcetypes");
        }
    }

}

From source file:com.eurelis.opencms.ant.task.ManifestBuilderTask.java

License:Open Source License

private void addAccessesToTree(Element root, String propFilePath) {
    if (null != propFilePath) {
        File xml = new File(propFilePath);
        SAXReader reader = new SAXReader();
        Document doc;//from w w  w.  ja v  a  2  s .  com
        try {
            doc = reader.read(xml);
            Element elem = doc.getRootElement();
            if (null != elem)
                root.add(elem);
            else
                root.addElement("accesscontrol");
        } catch (DocumentException e) {
            root.addElement("accesscontrol");
        }
    }
}

From source file:com.eurelis.opencms.workflows.workflows.A_OSWorkflowManager.java

License:Open Source License

/**
 * Get the OpenCMS RFS OSWorkflow configuration file and update it with the given filepath.
 * /*from  w w  w . j a va2 s . c  o m*/
 * @param listOfWorkflowsFilepath
 *            the path of the file containing the list of available workflow descriptions
 * @return the path in RFS of the updated file
 * @throws DocumentException
 *             this exception is thrown if an error occurs during the parsing of the document
 * @throws IOException
 *             this exception is thrown if a problem occurs during overwriting of the config file
 */
private String updateOSWorkflowConfigFile(String listOfWorkflowsFilepath)
        throws CmsException, DocumentException, IOException {

    // get file path
    String configFilePath = this.getWebINFPath() + ModuleSharedVariables.SYSTEM_FILE_SEPARATOR
            + OSWORKFLOWCONFIGFILE_RFSFILEPATH;

    File listOfWorkflowsFile = new File(listOfWorkflowsFilepath);
    // Load Jdom parser
    SAXReader reader = new SAXReader();
    Document document = reader.read(configFilePath);
    Node propertyNode = document.selectSingleNode(OSWORKFLOWCONFIGFILE_PROPERTYTOUPDATE_XPATH);
    if (propertyNode != null) {
        if (propertyNode.getNodeType() == Node.ELEMENT_NODE) {
            // convert Node into element
            Element propertyElement = (Element) propertyNode;

            // update the Attribute
            Attribute valueAttribute = propertyElement
                    .attribute(OSWORKFLOWCONFIGFILE_PROPERTYTOUPDATE_ATTRIBUTNAME);

            valueAttribute.setValue(listOfWorkflowsFile.toURI().toString());

        } else {
            LOGGER.debug("the node with Xpath " + OSWORKFLOWCONFIGFILE_PROPERTYTOUPDATE_XPATH + " in the file "
                    + configFilePath + " doesn't correspond to an element");
        }

    } else {
        Node parentNode = document.selectSingleNode(OSWORKFLOWCONFIGFILE_PROPERTYTOUPDATE_PARENT_XPATH);
        if (parentNode != null) {

            if (parentNode.getNodeType() == Node.ELEMENT_NODE) {
                // convert Node into element
                Element parentElement = (Element) parentNode;

                // add new property
                Element propertyElement = parentElement.addElement("property");

                // add attributs
                propertyElement.addAttribute("key", "resource");
                propertyElement.addAttribute("value", listOfWorkflowsFile.toURI().toString());

            } else {
                LOGGER.debug("the node with Xpath " + OSWORKFLOWCONFIGFILE_PROPERTYTOUPDATE_XPATH
                        + " in the file " + configFilePath + " doesn't correspond to an element");
            }

        } else {
            LOGGER.debug("the node with Xpath " + OSWORKFLOWCONFIGFILE_PROPERTYTOUPDATE_PARENT_XPATH
                    + " in the file " + configFilePath + " has not been found.");
        }
    }

    /*
     * Get a string of the resulting file
     */

    // creating of a buffer that will collect result
    ByteArrayOutputStream xmlContent = new ByteArrayOutputStream();

    // Pretty print the document to xmlContent
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(xmlContent, format);
    writer.write(document);
    writer.flush();
    writer.close();

    // get the config file content as a String
    String documentContent = new String(xmlContent.toByteArray());

    /*
     * Overwrite the config file
     */
    FileWriter.writeFile(configFilePath, documentContent);

    return configFilePath;
}