Example usage for org.dom4j ElementPath getCurrent

List of usage examples for org.dom4j ElementPath getCurrent

Introduction

In this page you can find the example usage for org.dom4j ElementPath getCurrent.

Prototype

Element getCurrent();

Source Link

Document

DOCUMENT ME!

Usage

From source file:org.snipsnap.snip.XMLSnipImport.java

License:Open Source License

/**
 * Load snips and users into the SnipSpace from an xml document out of a stream.
 *
 * @param in    the input stream to load from
 * @param flags whether or not to overwrite existing content
 *//*from  w ww . j a  v a 2s.c  o m*/
public static void load(InputStream in, final int flags) throws IOException {
    SAXReader saxReader = new SAXReader();
    try {
        saxReader.addHandler("/snipspace/user", new ElementHandler() {
            public void onStart(ElementPath elementPath) {
                // nothing to do here ...
            }

            public void onEnd(ElementPath elementPath) {
                Element userElement = elementPath.getCurrent();
                if ((flags & IMPORT_USERS) != 0) {
                    try {
                        XMLSnipImport.loadUser(elementPath.getCurrent(), flags);
                    } catch (Exception e) {
                        Logger.fatal("XMLSnipImport: error importing user: " + userElement.elementText("name"));
                    }
                    getStatus().inc();
                }
                // prune the element to save memory
                userElement.detach();
            }
        });

        saxReader.addHandler("/snipspace/snip", new ElementHandler() {
            public void onStart(ElementPath elementPath) {
                // nothing to do here ...
            }

            public void onEnd(ElementPath elementPath) {
                Element snipElement = elementPath.getCurrent();
                if ((flags & IMPORT_SNIPS) != 0) {
                    try {
                        XMLSnipImport.loadSnip(snipElement, flags);
                    } catch (Exception e) {
                        Logger.fatal("XMLSnipImport: error importing snip: " + snipElement.elementText("name"));
                    }
                    getStatus().inc();
                }
                // prune the element to save memory
                snipElement.detach();
            }
        });

        // add a reader wrapper to remove illegal characters from input stream
        // it looks like the database export (XMLWriter) allows these to get through
        InputStreamReader reader = new InputStreamReader(in, "UTF-8") {
            public int read(char[] chars) throws IOException {
                int n = super.read(chars);
                for (int i = 0; i < n; i++) {
                    chars[i] = replaceIfIllegal(chars[i]);
                }
                return n;
            }

            public int read(char[] chars, int start, int length) throws IOException {
                int n = super.read(chars, start, length);
                for (int i = 0; i < n; i++) {
                    chars[i] = replaceIfIllegal(chars[i]);
                }
                return n;
            }

            private char replaceIfIllegal(char c) {
                if (c < 0x20 && !(c == 0x09 || c == 0x0a || c == 0x0d)) {
                    charErrCount++;
                    return (char) 0x20;
                }
                return c;
            }
        };

        saxReader.read(reader);
        Logger.warn("XMLSnipImport: corrected " + charErrCount + " characters in input");
        Logger.log("XMLSnipImport: imported " + getStatus().getValue() + " data records");
    } catch (DocumentException e) {
        Logger.warn("XMLSnipImport: unable to parse document", e);
        throw new IOException("Error parsing document: " + e);
    }
}

From source file:uidserver.Config.java

public Config(String configFilePath) {
    File configFile = new File(configFilePath);
    if (configFile.exists()) {
        try {/*from   w  w  w .  ja v  a 2  s.c  om*/
            SAXReader reader = new SAXReader();
            reader.addHandler("/config", new ElementHandler() {
                @Override
                public void onStart(ElementPath elementPath) {

                }

                @Override
                public void onEnd(ElementPath elementPath) {
                    Element row = elementPath.getCurrent();
                    readElement(row);
                    row.detach();
                }

                private void readElement(Element row) {
                    List<Element> nodes = row.elements();
                    if (!nodes.isEmpty()) {
                        for (Element node : nodes) {
                            String name = node.getName().toLowerCase();
                            String value = node.getText();
                            switch (name) {
                            case "logpath":
                                logPath = new File(value);
                                break;
                            case "port":
                                port = value;
                                break;
                            case "timeout":
                                timeOut = Integer.valueOf(value);
                                break;
                            case "uidfile":
                                uidFile = new File(value);
                                break;
                            }

                        }

                    } else {
                        System.out.println("Error: empty elements in config file, please add correct setup");
                        System.exit(0);
                    }

                }

            });

            reader.setValidation(false);
            Document document = reader.read(configFile);
            if (logPath != null && port != null && uidFile != null) {
                if (!logPath.exists()) {
                    if (!logPath.mkdirs()) {
                        System.out.println("Failed to create log file: " + logPath.getAbsoluteFile());
                        System.out.println("Please setup correct log file path");
                        System.exit(0);
                    }
                }

            } else {
                System.out.println("Please set up correct Port/LogFile/UidFile");
                System.exit(0);
            }
        } catch (DocumentException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Error during reading xml config file, please double check file content");
            System.exit(0);
        }
    } else {
        System.out.println("The specified config file: " + configFile.getAbsolutePath() + " doesn't exist");
        System.out.println("Please key in correct config file path");
        System.exit(0);
    }
}