Example usage for org.jdom2 Document getRootElement

List of usage examples for org.jdom2 Document getRootElement

Introduction

In this page you can find the example usage for org.jdom2 Document getRootElement.

Prototype

public Element getRootElement() 

Source Link

Document

This will return the root Element for this Document

Usage

From source file:com.archimatetool.model.viewpoints.ViewpointManager.java

License:Open Source License

/**
 * Load viewpoints from XML file/* ww  w  .  j av  a2s .  c om*/
 */
void loadDefaultViewpointsFile() throws IOException, JDOMException {
    URL url = Platform.getBundle(BUNDLE_ID).getEntry(VIEWPOINTS_FILE);

    Document doc = new SAXBuilder().build(url);
    Element rootElement = doc.getRootElement();

    for (Element xmlViewpoint : rootElement.getChildren("viewpoint")) { //$NON-NLS-1$

        String id = xmlViewpoint.getAttributeValue("id"); //$NON-NLS-1$
        if (id == null || "".equals(id)) { //$NON-NLS-1$
            System.err.println("Blank id for viewpoint"); //$NON-NLS-1$
            continue;
        }

        Element xmlName = xmlViewpoint.getChild("name"); //$NON-NLS-1$
        if (xmlName == null) {
            System.err.println("No name element for viewpoint"); //$NON-NLS-1$
            continue;
        }

        String name = xmlName.getText();
        if (name == null || "".equals(name)) { //$NON-NLS-1$
            System.err.println("Blank name for viewpoint"); //$NON-NLS-1$
            continue;
        }

        Viewpoint vp = new Viewpoint(id, name);

        for (Element xmlConcept : xmlViewpoint.getChildren("concept")) { //$NON-NLS-1$
            String conceptName = xmlConcept.getText();
            if (conceptName == null || "".equals(conceptName)) { //$NON-NLS-1$
                System.err.println("Blank concept name for viewpoint"); //$NON-NLS-1$
                continue;
            }

            if (ELEMENTS_MAP.containsKey(conceptName)) {
                addCollection(vp, conceptName);
            } else {
                EClass eClass = (EClass) IArchimatePackage.eINSTANCE.getEClassifier(conceptName);
                if (eClass != null) {
                    addConcept(vp, eClass);
                } else {
                    System.err.println("Couldn't get eClass: " + conceptName); //$NON-NLS-1$
                }

            }
        }

        VIEWPOINTS.put(id, vp);
    }
}

From source file:com.archimatetool.templates.impl.model.ArchimateTemplateManager.java

License:Open Source License

@Override
protected boolean isValidTemplateFile(File file) throws IOException {
    if (file == null || !file.exists()) {
        return false;
    }/*from w w  w  .j  a v  a 2  s .  c om*/

    // Ensure the template is of the right kind
    String xmlString = ZipUtils.extractZipEntry(file, ZIP_ENTRY_MANIFEST);
    if (xmlString == null) {
        return false;
    }

    // If the attribute "type" exists then return true if its value is "model".
    // If the attribute doesn't exist it was from an older version (before 2.1)
    try {
        Document doc = JDOMUtils.readXMLString(xmlString);
        Element root = doc.getRootElement();
        Attribute attType = root.getAttribute(ITemplateXMLTags.XML_TEMPLATE_ATTRIBUTE_TYPE);
        if (attType != null) {
            return ArchimateModelTemplate.XML_TEMPLATE_ATTRIBUTE_TYPE_MODEL.equals(attType.getValue());
        }
    } catch (JDOMException ex) {
        return false;
    }

    return true;
}

From source file:com.archimatetool.templates.model.AbstractTemplate.java

License:Open Source License

private void loadManifest() {
    // Default first
    fManifestLoaded = true;//  w w w  .  j  ava2 s . com
    fName = ""; //$NON-NLS-1$
    fDescription = ""; //$NON-NLS-1$

    if (fFile != null && fFile.exists()) {
        try {
            // Manifest
            String manifest = ZipUtils.extractZipEntry(fFile, TemplateManager.ZIP_ENTRY_MANIFEST);
            if (manifest != null) {
                Document doc = JDOMUtils.readXMLString(manifest);
                Element rootElement = doc.getRootElement();

                // Name
                Element nameElement = rootElement.getChild(XML_TEMPLATE_ELEMENT_NAME);
                if (nameElement != null) {
                    fName = nameElement.getText();
                }

                // Description
                Element descriptionElement = rootElement.getChild(XML_TEMPLATE_ELEMENT_DESCRIPTION);
                if (nameElement != null) {
                    fDescription = descriptionElement.getText();
                }

                // Key thumbnail
                Element keyThumbnailElement = rootElement.getChild(XML_TEMPLATE_ELEMENT_KEY_THUMBNAIL);
                if (keyThumbnailElement != null) {
                    fKeyThumbnailPath = keyThumbnailElement.getText();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.archimatetool.templates.model.TemplateManager.java

License:Open Source License

/**
 * Load all user templates as declared in the manifest
 *///from   w w  w  .j ava2 s. c om
protected void loadUserTemplates() {
    fUserTemplates = new ArrayList<ITemplate>();
    fUserTemplateGroups = new ArrayList<ITemplateGroup>();

    if (!getUserTemplatesManifestFile().exists()) {
        return;
    }

    Document doc = null;
    try {
        doc = JDOMUtils.readXMLFile(getUserTemplatesManifestFile());
    } catch (Exception ex) {
        ex.printStackTrace();
        return;
    }

    HashMap<String, ITemplate> userTemplateMap = new HashMap<String, ITemplate>();

    Element rootElement = doc.getRootElement();

    // Templates
    for (Object child : rootElement.getChildren(XML_TEMPLATE_ELEMENT_TEMPLATE)) {
        Element templateElement = (Element) child;
        String type = templateElement.getAttributeValue(XML_TEMPLATE_ATTRIBUTE_TYPE);
        ITemplate template = createTemplate(type);
        if (template != null) {
            String id = templateElement.getAttributeValue(XML_TEMPLATE_ATTRIBUTE_ID);
            String path = templateElement.getAttributeValue(XML_TEMPLATE_ATTRIBUTE_FILE);
            if (id != null && path != null) {
                File file = new File(path);
                if (file.exists()) {
                    template.setID(id);
                    template.setFile(file);
                    fUserTemplates.add(template);
                    userTemplateMap.put(id, template);
                }
            }
        }
    }

    // Groups
    for (Object child : rootElement.getChildren(XML_TEMPLATE_ELEMENT_GROUP)) {
        Element groupElement = (Element) child;
        ITemplateGroup templateGroup = new TemplateGroup();
        templateGroup.setName(groupElement.getAttributeValue(XML_TEMPLATE_ATTRIBUTE_NAME));
        fUserTemplateGroups.add(templateGroup);

        // Template refs
        for (Object child2 : groupElement.getChildren(XML_TEMPLATE_ELEMENT_TEMPLATE_REF)) {
            Element templateRefElement = (Element) child2;
            String ref = templateRefElement.getAttributeValue(XML_TEMPLATE_ATTRIBUTE_REF);
            if (ref != null) {
                ITemplate template = userTemplateMap.get(ref);
                if (template != null) {
                    templateGroup.addTemplate(template);
                }
            }
        }
    }
}

From source file:com.ardor3d.extension.model.collada.jdom.ColladaImporter.java

License:Open Source License

/**
 * Reads the whole Collada DOM tree from the given resource and returns its root element. Exceptions may be thrown
 * by underlying tools; these will be wrapped in a RuntimeException and rethrown.
 * /*from  w w w.j av  a 2s .  c o  m*/
 * @param resource
 *            the ResourceSource to read the resource from
 * @return the Collada root element
 */
private Element readCollada(final ResourceSource resource, final DataCache dataCache) {
    try {
        final JDOMFactory jdomFac = new ArdorFactory(dataCache);
        final SAXBuilder builder = new SAXBuilder(null, new SAXHandlerFactory() {
            @Override
            public SAXHandler createSAXHandler(final JDOMFactory factory) {
                return new SAXHandler(jdomFac) {
                    @Override
                    public void startPrefixMapping(final String prefix, final String uri) throws SAXException {
                        // Just kill what's usually done here...
                    }
                };
            }
        }, jdomFac);

        final Document doc = builder.build(resource.openStream());
        final Element collada = doc.getRootElement();

        // ColladaDOMUtil.stripNamespace(collada);

        return collada;
    } catch (final Exception e) {
        throw new RuntimeException("Unable to load collada resource from source: " + resource, e);
    }
}

From source file:com.arrggh.eve.api.xml.parsers.ResponseParsers.java

static List<XmlApiCharacter> parseCharacterList(String text) {
    List<XmlApiCharacter> results = new LinkedList<>();
    try {/* w w  w . j  a v a2  s  .co  m*/
        SAXBuilder jdomBuilder = new SAXBuilder();
        Document document = jdomBuilder.build(new StringReader(text));

        List<Element> rows = document.getRootElement().getChild("result").getChild("rowset").getChildren("row");
        for (Element row : rows) {
            XmlApiCharacter.XmlApiCharacterBuilder builder = XmlApiCharacter.builder();

            builder.id(row.getAttributeValue("characterID"));
            builder.name(row.getAttributeValue("name"));
            builder.corporationName(row.getAttributeValue("corporationName"));
            builder.corporationId(row.getAttributeValue("corporationID"));
            builder.allianceId(row.getAttributeValue("allianceID"));
            builder.allianceName(row.getAttributeValue("allianceName"));
            builder.factionId(row.getAttributeValue("factionID"));
            builder.factionName(row.getAttributeValue("factionName"));

            results.add(builder.build());
        }
    } catch (JDOMException | IOException e) {
        throw new ParserException("Cannot parse character list", e);
    }

    return results;
}

From source file:com.arrggh.eve.api.xml.parsers.ResponseParsers.java

static List<XmlApiCharacterIndustryJob> parseIndustryJobs(String text) {
    List<XmlApiCharacterIndustryJob> results = new LinkedList<>();
    try {/*from  w w w .  j  av  a2 s  . co m*/
        SAXBuilder jdomBuilder = new SAXBuilder();
        Document document = jdomBuilder.build(new StringReader(text));

        List<Element> rows = document.getRootElement().getChild("result").getChild("rowset").getChildren("row");
        for (Element row : rows) {
            XmlApiCharacterIndustryJob.XmlApiCharacterIndustryJobBuilder builder = XmlApiCharacterIndustryJob
                    .builder();

            builder.jobID(getLong(row, "jobID"));
            builder.installerID(getLong(row, "installerID"));
            builder.installerName(row.getAttributeValue("installerName"));
            builder.facilityID(getLong(row, "facilityID"));
            builder.solarSystemID(getLong(row, "solarSystemID"));
            builder.solarSystemName(row.getAttributeValue("solarSystemName"));
            builder.stationID(getLong(row, "stationID"));
            builder.activityID(getLong(row, "activityID"));
            builder.blueprintID(getLong(row, "blueprintID"));
            builder.blueprintTypeID(getLong(row, "blueprintTypeID"));
            builder.blueprintTypeName(row.getAttributeValue("blueprintTypeName"));
            builder.blueprintLocationID(getLong(row, "blueprintLocationID"));
            builder.outputLocationID(getLong(row, "outputLocationID"));
            builder.runs(getLong(row, "runs"));
            builder.cost(row.getAttributeValue("cost"));
            builder.teamID(getLong(row, "teamID"));
            builder.licensedRuns(row.getAttributeValue("licensedRuns"));
            builder.probability(row.getAttributeValue("probability"));
            builder.productTypeID(getLong(row, "productTypeID"));
            builder.productTypeName(row.getAttributeValue("productTypeName"));
            builder.status(row.getAttributeValue("status"));
            builder.timeInSeconds(getLong(row, "timeInSeconds"));
            builder.startDate(row.getAttributeValue("startDate"));
            builder.endDate(row.getAttributeValue("endDate"));
            builder.pauseDate(row.getAttributeValue("pauseDate"));
            builder.completedDate(row.getAttributeValue("completedDate"));
            builder.completedCharacterID(getLong(row, "completedCharacterID"));
            builder.successfulRuns(row.getAttributeValue("successfulRuns"));

            results.add(builder.build());
        }
    } catch (JDOMException | IOException e) {
        throw new ParserException("Cannot parse industry job", e);
    }

    return results;
}

From source file:com.arrggh.eve.api.xml.parsers.ResponseParsers.java

static List<XmlApiOwnedBlueprint> parseBlueprints(String text) {
    List<XmlApiOwnedBlueprint> results = new LinkedList<>();
    try {//from  w  w w  .j  a  v  a2  s  .  co m
        SAXBuilder jdomBuilder = new SAXBuilder();
        Document document = jdomBuilder.build(new StringReader(text));

        List<Element> rows = document.getRootElement().getChild("result").getChild("rowset").getChildren("row");
        for (Element row : rows) {
            XmlApiOwnedBlueprint.XmlApiOwnedBlueprintBuilder builder = XmlApiOwnedBlueprint.builder();

            builder.itemId(getLong(row, "itemID"));
            builder.typeName(row.getAttributeValue("typeName"));
            builder.typeId(getLong(row, "typeID"));
            builder.flagId(getLong(row, "flagID"));
            builder.quantity(row.getAttributeValue("quantity"));
            builder.runs(row.getAttributeValue("runs"));
            builder.locationId(getLong(row, "locationID"));
            builder.materialEfficiency(row.getAttributeValue("materialEfficiency"));
            builder.timeEfficiency(row.getAttributeValue("timeEfficiency"));

            results.add(builder.build());
        }
    } catch (JDOMException | IOException e) {
        throw new ParserException("Cannot parse blueprints", e);
    }

    return results;
}

From source file:com.arrggh.eve.api.xml.parsers.ResponseParsers.java

static List<XmlApiOwnedAsset> parseAssets(String text) {
    List<XmlApiOwnedAsset> results = new LinkedList<>();
    try {//from  w  w  w  .  j  a  v  a 2  s . co  m
        SAXBuilder jdomBuilder = new SAXBuilder();
        Document document = jdomBuilder.build(new StringReader(text));

        List<Element> rows = document.getRootElement().getChild("result").getChild("rowset").getChildren("row");
        for (Element row : rows) {
            XmlApiOwnedAsset.XmlApiOwnedAssetBuilder builder = XmlApiOwnedAsset.builder();

            builder.itemID(getLong(row, "itemID"));
            builder.locationID(getLong(row, "locationID"));
            builder.typeID(getLong(row, "typeID"));
            builder.quantity(getLong(row, "quantity"));
            builder.flag(row.getAttributeValue("flag"));
            builder.singleton(row.getAttributeValue("singleton"));
            builder.rawQuantity(row.getAttributeValue("rawQuantity"));

            results.add(builder.build());
        }
    } catch (JDOMException | IOException e) {
        throw new ParserException("Cannot parse assets", e);
    }

    return results;
}

From source file:com.arrggh.eve.api.xml.parsers.ResponseParsers.java

static List<XmlApiEveLocation> parseLocations(String text) {
    List<XmlApiEveLocation> results = new LinkedList<>();
    try {/* w w w.  j  a v a  2s  . co m*/
        SAXBuilder jdomBuilder = new SAXBuilder();
        Document document = jdomBuilder.build(new StringReader(text));

        List<Element> rows = document.getRootElement().getChild("result").getChild("rowset").getChildren("row");
        for (Element row : rows) {
            XmlApiEveLocation.XmlApiEveLocationBuilder builder = XmlApiEveLocation.builder();

            builder.itemID(getLong(row, "itemID"));
            builder.itemName(row.getAttributeValue("itemName"));
            builder.x(getDouble(row, "x"));
            builder.y(getDouble(row, "y"));
            builder.z(getDouble(row, "z"));

            results.add(builder.build());
        }
    } catch (JDOMException | IOException e) {
        throw new ParserException("Cannot parse locations", e);
    }

    return results;
}