Example usage for org.jdom2.input SAXBuilder SAXBuilder

List of usage examples for org.jdom2.input SAXBuilder SAXBuilder

Introduction

In this page you can find the example usage for org.jdom2.input SAXBuilder SAXBuilder.

Prototype

public SAXBuilder() 

Source Link

Document

Creates a new JAXP-based SAXBuilder.

Usage

From source file:com.aurum.whitehole.ObjectDB.java

License:Open Source License

public static void init() {
    fallback = true;//ww w.  ja  va 2  s.c o  m
    timestamp = 0;

    categories = new LinkedHashMap();
    objects = new LinkedHashMap();

    File odbfile = new File("objectdb.xml");
    if (!(odbfile.exists() && odbfile.isFile()))
        return;

    try {
        Element root = new SAXBuilder().build(odbfile).getRootElement();
        timestamp = root.getAttribute("timestamp").getLongValue();

        List<Element> catelems = root.getChild("categories").getChildren("category");
        for (Element catelem : catelems)
            categories.put(catelem.getAttribute("id").getIntValue(), catelem.getText());

        List<Element> objelems = root.getChildren("object");
        for (Element objelem : objelems) {
            Object entry = new Object();
            entry.ID = objelem.getAttributeValue("id");
            entry.name = objelem.getChildText("name");
            entry.category = objelem.getChild("category").getAttribute("id").getIntValue();
            entry.type = objelem.getChild("preferredfile").getAttributeValue("name");
            entry.notes = objelem.getChildText("notes");

            Element flags = objelem.getChild("flags");
            entry.games = flags.getAttribute("games").getIntValue();
            entry.known = flags.getAttribute("known").getIntValue();
            entry.complete = flags.getAttribute("complete").getIntValue();

            if (entry.notes.isEmpty() || entry.notes.equals(""))
                entry.notes = "(No description found for this objects.)";
            if (entry.type.isEmpty() || entry.notes.equals(""))
                entry.type = "Unknown";

            entry.files = new ArrayList();
            String files = objelem.getChildText("files");
            for (String file : files.split("\n")) {
                entry.files.add(file);
            }

            List<Element> fields = objelem.getChildren("field");
            entry.fields = new HashMap(fields.size());
            if (!fields.isEmpty()) {
                for (Element field : fields) {
                    Object.Field fielddata = new Object.Field();

                    fielddata.ID = field.getAttribute("id").getIntValue();
                    fielddata.type = field.getAttributeValue("type");
                    fielddata.name = field.getAttributeValue("name");
                    fielddata.values = field.getAttributeValue("values");
                    fielddata.notes = field.getAttributeValue("notes");

                    entry.fields.put(fielddata.ID, fielddata);
                }
            }

            objects.put(entry.ID, entry);
        }
    } catch (IOException | JDOMException ex) {
        timestamp = 0;
        return;
    }

    fallback = false;
}

From source file:com.bc.ceres.binio.binx.BinX.java

License:Open Source License

private CompoundType parseDocument(URI uri) throws IOException, BinXException {
    SAXBuilder builder = new SAXBuilder();
    Document document;/* w w w.  j a  v  a  2 s.com*/
    try {
        document = builder.build(uri.toURL());
    } catch (JDOMException e) {
        throw new BinXException(MessageFormat.format("Failed to read ''{0}''", uri), e);
    }
    Element binxElement = document.getRootElement();
    this.namespace = binxElement.getNamespace();
    parseParameters(binxElement);
    parseDefinitions(binxElement);
    return parseDataset(binxElement);
}

From source file:com.bc.ceres.jai.opimage.XmlRIF.java

License:Open Source License

private RenderedImage create(URI location, Map<String, Object> configuration, RenderingHints renderingHints)
        throws JDOMException, IOException, IllegalArgumentException {
    configuration = new HashMap<String, Object>(configuration);

    SAXBuilder builder = new SAXBuilder();
    Document document = builder.build(location.toURL());
    Element rootElement = document.getRootElement();
    Map<String, Element> sourceMap = getElementMap(rootElement, ENAME_SOURCE);
    Map<String, Element> parameterMap = getElementMap(rootElement, ENAME_PARAMETER);
    Element targetElement = rootElement.getChild(ENAME_TARGET);
    return parseImage(targetElement, sourceMap, parameterMap, configuration, renderingHints, "rendered");
}

From source file:com.bennavetta.util.tycho.impl.DefaultWrapperGenerator.java

License:Apache License

private void writeModules(List<String> modules, File pomFile) throws JDOMException, IOException {
    SAXBuilder builder = new SAXBuilder();
    Document pom = builder.build(pomFile);
    Namespace pomNs = pom.getRootElement().getNamespace();
    Element modulesElem = pom.getRootElement().getChild("modules", pomNs);
    if (modulesElem == null) {
        modulesElem = new Element("modules", pomNs);
        pom.getRootElement().addContent(modulesElem);
    }//w  ww.  j  ava2s . c o  m
    for (String module : modules) {
        boolean exists = false;
        for (Element existingModule : modulesElem.getChildren()) {
            if (existingModule.getTextTrim().equals(module)) {

                exists = true;
                break;
            }
        }
        if (!exists) {
            Element moduleElem = new Element("module", pomNs);
            moduleElem.setText(module);
            modulesElem.addContent(moduleElem);
        }
    }
    XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat().setIndent("\t"));
    try (FileOutputStream out = new FileOutputStream(pomFile)) {
        xout.output(pom, out);
    }
}

From source file:com.bennavetta.util.tycho.Main.java

License:Apache License

private int run(String[] args) throws Exception {
    CmdLineParser parser = new CmdLineParser(this);
    try {/*from ww w . ja  v  a2 s . co m*/
        parser.parseArgument(args);

        if (logLevel != null) {
            ThreadContext.put(LOGGING_PROP, logLevel.toUpperCase());
        }

        if (version) {
            String version = Config.get(Config.VERSION);
            String aetherVersion = Config.get(Config.AETHER_VERSION);
            String mavenVersion = Config.get(Config.MAVEN_VERSION);
            String gradleVersion = Config.get(Config.GRADLE_VERSION);
            Date built = new Date(Long.parseLong(Config.get(Config.BUILD_TIMESTAMP)));

            System.out.println("Tycho Generator version: " + version);
            System.out.println("Sonatype Aether version: " + aetherVersion);
            System.out.println("Apache Maven version: " + mavenVersion);
            String builtDate = new SimpleDateFormat("dd MMMM yyyy").format(built);
            String builtTime = new SimpleDateFormat("hh:mm:ss.SSS aa").format(built);
            System.out.println("Built by Gradle " + gradleVersion + " on " + builtDate + " at " + builtTime);
        } else if (configFile == null) {
            throw new CmdLineException(parser, "No config file given");
        } else {
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(configFile);
            WrapRequest request = new XmlWrapDescriptorParser().createRequest(doc);

            WrapperGenerator generator = new DefaultWrapperGenerator();
            generator.generate(request);
        }

        return 0;
    } catch (CmdLineException e) {
        String invocation = "tycho-gen-" + Config.get(Config.VERSION) + "-dist.jar";
        // tell the user what was wrong with the arguments
        System.err.println(e.getMessage());

        // show usage information
        System.err.println("Usage: java -jar " + invocation + " [options] config-file");
        parser.printUsage(System.err);

        // show an example
        System.err.println();
        System.err.println("    Example: java -jar " + invocation + parser.printExample(ExampleMode.REQUIRED)
                + " myconfig.xml");
        return 1;
    }
}

From source file:com.c4om.autoconf.ulysses.extra.svrlinterpreter.SVRLInterpreterMain.java

License:Apache License

/**
 * Constructor/*  ww w.jav  a 2  s  .c  o m*/
 * @param file {@link SVRLInterpreterMain#file}
 * @param metamodelPath {@link SVRLInterpreterMain#metamodelPath}
 */
public SVRLInterpreterMain(String file, String metamodelPath) throws IOException, JDOMException {
    this.metamodelPath = metamodelPath;
    SAXBuilder builder = new SAXBuilder();
    InputStream is;
    if (file.equals("--")) {
        is = System.in;
    } else {
        File fileFile = new File(file);
        if (!fileFile.exists()) {
            throw new FileNotFoundException("File \"" + file + "\" does not exist.");
        }
        is = new FileInputStream(fileFile);
    }
    this.document = builder.build(is);
}

From source file:com.c4om.autoconf.ulysses.extra.svrlinterpreter.SVRLInterpreterProcessor.java

License:Apache License

/**
 * Method that actually performs the process
 * @param intermediateResults a list of intermediate results.
 * @return the resulting report//w  w w .  java  2s .co m
 * @throws SaxonApiException if a XQuery query fails.
 * @throws IOException required by internal methods, but it will never be thrown (normally)
 * @throws JDOMException if there are problems at XML Parsing
 */
public Document process(List<List<String>> intermediateResults)
        throws SaxonApiException, JDOMException, IOException {
    SAXBuilder builder = new SAXBuilder();
    Element rootElement = new Element("report", NAMESPACE_SVRL_INTERPETER_REPORT);
    Document result = new Document(rootElement);
    Map<String, List<Element>> queryCache = performXPathMetamodelSuggestionQueries(
            listOfListsToSingleList(intermediateResults, 0));
    for (int i = 0; i < intermediateResults.size(); i++) {
        List<String> intermediateResult = intermediateResults.get(i);
        Element currentDiscrepancyElement = new Element("discrepancy", NAMESPACE_SVRL_INTERPETER_REPORT);
        rootElement.addContent(currentDiscrepancyElement);

        String partialNodeString = saxonQuery(intermediateResult.get(1));
        Reader partialNodeStringReader = new StringReader(partialNodeString);
        Document partialNodeDoc = builder.build(partialNodeStringReader);
        Element partialNode = partialNodeDoc.getRootElement().detach();

        Element currentInterestingPathsElement = new Element("interestingPaths",
                NAMESPACE_SVRL_INTERPETER_REPORT);
        moveAndRemoveNSOfAttr(partialNode, currentInterestingPathsElement, "mandatory-path",
                NAMESPACE_SVRL_INTERPETER_REPORT);
        moveAndRemoveNSOfAttr(partialNode, currentInterestingPathsElement, "base-path",
                NAMESPACE_SVRL_INTERPETER_REPORT);
        moveAndRemoveNSOfAttr(partialNode, currentInterestingPathsElement, "key-subpath",
                NAMESPACE_SVRL_INTERPETER_REPORT);
        moveAndRemoveNSOfAttr(partialNode, currentInterestingPathsElement, "search-path",
                NAMESPACE_SVRL_INTERPETER_REPORT);
        currentDiscrepancyElement.addContent(currentInterestingPathsElement);

        Element currentSuggestedPartialNodeElement = new Element("suggestedPartialNode",
                NAMESPACE_SVRL_INTERPETER_REPORT);
        currentDiscrepancyElement.addContent(currentSuggestedPartialNodeElement);

        currentSuggestedPartialNodeElement.addContent(partialNode);

        Element currentMetamodelSuggestionsElement = new Element("metamodelSuggestions",
                NAMESPACE_SVRL_INTERPETER_REPORT);
        currentDiscrepancyElement.addContent(currentMetamodelSuggestionsElement);

        List<Element> queryResults = queryCache.get(intermediateResult.get(0));
        for (Element queryResult : queryResults) {
            currentMetamodelSuggestionsElement.addContent(queryResult.clone());
        }

    }
    return result;
}

From source file:com.c4om.utils.xmlutils.JDOMUtils.java

License:Apache License

/**
 * Loads a XML file from a stream as a JDOM Document
 * @param is the input stream/*from  w  ww  . j a va  2s .c  o m*/
 * @return the loaded document
 * @throws IOException Input/Output errors.
 * @throws JDOMException Errors at XML parsing. 
 */
public static Document loadJDOMDocumentFromStream(InputStream is) throws JDOMException, IOException {
    SAXBuilder builder = new SAXBuilder();
    return builder.build(is);
}

From source file:com.c4om.utils.xmlutils.JDOMUtils.java

License:Apache License

/**
 * Loads a XML file from a string as a JDOM Document
 * @param s the input string//from   w  w  w  .  ja v  a2 s. com
 * @return the loaded document
 * @throws IOException Input/Output errors.
 * @throws JDOMException Errors at XML parsing. 
 */
public static Document loadJDOMDocumentFromString(String s) throws JDOMException, IOException {
    SAXBuilder builder = new SAXBuilder();
    return builder.build(new StringReader(s));
}

From source file:com.cats.version.VersionCfgParseAndSave.java

License:Apache License

public List<VersionInfo> getVersionInfo(String fullPath) {
    SAXBuilder builder = new SAXBuilder();
    List<VersionInfo> infos = new ArrayList<VersionInfo>();
    try {//from w w  w  .  j  a  v a  2s .co m
        Document doc = builder.build(new File(fullPath));
        Element root = doc.getRootElement();
        List<Element> softEles = root.getChildren("software");
        for (Element softEle : softEles) {
            String appName = softEle.getAttribute("name").getValue();
            String versionCode = softEle.getChildText("latest-version-code");
            String versionName = softEle.getChildText("latest-version");
            String versionPath = softEle.getChildText("latest-version-abspath");
            String startupName = softEle.getChildText("latest-version-startup");

            Element detailEles = softEle.getChild("latest-version-detail");
            List<Element> detailItemEles = detailEles.getChildren("item");
            List<VersionInfoDetail> details = new ArrayList<VersionInfoDetail>();
            for (Element detailItem : detailItemEles) {
                String title = detailItem.getAttributeValue("name");
                List<Element> detailEleList = detailItem.getChildren("detail");
                List<String> detailList = new ArrayList<String>();
                for (Element detailEle : detailEleList) {
                    String strDetail = detailEle.getText();
                    detailList.add(strDetail);
                }
                details.add(new VersionInfoDetail(title, detailList));
            }

            Element ignoreEles = softEle.getChild("ignore-files");
            List<String> ignoreFiles = new ArrayList<String>();
            if (ignoreEles != null) {
                List<Element> ignoreItems = ignoreEles.getChildren("item");
                for (Element ignoreItem : ignoreItems) {
                    ignoreFiles.add(ignoreItem.getText());
                }
            }

            VersionInfo versionInfo = new VersionInfo();
            versionInfo.setAppName(appName);
            versionInfo.setVersion(versionName);
            versionInfo.setStartupName(startupName);
            versionInfo.setVersionCode(Integer.parseInt(versionCode));
            versionInfo.setPath(versionPath);
            versionInfo.setDetails(details);
            versionInfo.setIgnoreFiles(ignoreFiles);
            infos.add(versionInfo);
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return infos;
}