Search in sources :

Example 36 with Element

use of org.omegat.filters3.Element in project archi by archimatetool.

the class ArchimateTemplateManager method isValidTemplateFile.

@Override
protected boolean isValidTemplateFile(File file) throws IOException {
    if (file == null || !file.exists()) {
        return false;
    }
    // Ensure the template is of the right kind
    String xmlString = ZipUtils.extractZipEntry(file, ZIP_ENTRY_MANIFEST);
    if (xmlString == null) {
        return false;
    }
    // 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;
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException)

Example 37 with Element

use of org.omegat.filters3.Element in project archi by archimatetool.

the class SaveArchimateModelAsTemplateWizard method createManifest.

private String createManifest() throws IOException {
    Document doc = new Document();
    Element root = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_MANIFEST);
    doc.setRootElement(root);
    // Type
    root.setAttribute(ITemplateXMLTags.XML_TEMPLATE_ATTRIBUTE_TYPE, ArchimateModelTemplate.XML_TEMPLATE_ATTRIBUTE_TYPE_MODEL);
    // Timestamp
    root.setAttribute(ITemplateXMLTags.XML_TEMPLATE_ATTRIBUTE_TIMESTAMP, Long.toString(System.currentTimeMillis()));
    // Name
    Element elementName = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_NAME);
    elementName.setText(fTemplateName);
    root.addContent(elementName);
    // Description
    Element elementDescription = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_DESCRIPTION);
    elementDescription.setText(fTemplateDescription);
    root.addContent(elementDescription);
    // Thumbnails
    if (fIncludeThumbnails) {
        if (fSelectedDiagramModel != null) {
            int i = 1;
            for (IDiagramModel dm : fModel.getDiagramModels()) {
                if (dm == fSelectedDiagramModel) {
                    // $NON-NLS-1$
                    String keyThumb = TemplateManager.ZIP_ENTRY_THUMBNAILS + i + ".png";
                    Element elementKeyThumb = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_KEY_THUMBNAIL);
                    elementKeyThumb.setText(keyThumb);
                    root.addContent(elementKeyThumb);
                    break;
                }
                i++;
            }
        }
    }
    return JDOMUtils.write2XMLString(doc);
}
Also used : IDiagramModel(com.archimatetool.model.IDiagramModel) Element(org.jdom2.Element) Document(org.jdom2.Document)

Example 38 with Element

use of org.omegat.filters3.Element in project archi by archimatetool.

the class AbstractTemplate method save.

@Override
public void save() throws IOException {
    if (fFile == null || !fFile.exists()) {
        return;
    }
    // Manifest
    Document doc = new Document();
    Element root = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_MANIFEST);
    doc.setRootElement(root);
    Element elementName = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_NAME);
    elementName.setText(getName());
    root.addContent(elementName);
    Element elementDescription = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_DESCRIPTION);
    elementDescription.setText(getDescription());
    root.addContent(elementDescription);
    if (fKeyThumbnailPath != null) {
        Element elementKeyThumb = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_KEY_THUMBNAIL);
        elementKeyThumb.setText(fKeyThumbnailPath);
        root.addContent(elementKeyThumb);
    }
    String manifest = JDOMUtils.write2XMLString(doc);
    // Model
    String model = ZipUtils.extractZipEntry(fFile, TemplateManager.ZIP_ENTRY_MODEL);
    // Start a zip stream
    // $NON-NLS-1$
    File tmpFile = File.createTempFile("architemplate", null);
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tmpFile));
    ZipOutputStream zOut = new ZipOutputStream(out);
    ZipUtils.addStringToZip(manifest, TemplateManager.ZIP_ENTRY_MANIFEST, zOut);
    ZipUtils.addStringToZip(model, TemplateManager.ZIP_ENTRY_MODEL, zOut);
    // Thumbnails
    Image[] images = getThumbnails();
    int i = 1;
    for (Image image : images) {
        // $NON-NLS-1$
        ZipUtils.addImageToZip(image, TemplateManager.ZIP_ENTRY_THUMBNAILS + i++ + ".png", zOut, SWT.IMAGE_PNG, null);
    }
    zOut.flush();
    zOut.close();
    // Delete and copy
    fFile.delete();
    FileUtils.copyFile(tmpFile, fFile, false);
    tmpFile.delete();
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) Element(org.jdom2.Element) FileOutputStream(java.io.FileOutputStream) Document(org.jdom2.Document) Image(org.eclipse.swt.graphics.Image) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 39 with Element

use of org.omegat.filters3.Element in project archi by archimatetool.

the class RelationshipsMatrix method loadRelationships.

private void loadRelationships() {
    // URL url = Platform.getBundle(BUNDLE_ID).getResource(RELATIONSHIPS_FILE);
    URL url = Platform.getBundle(BUNDLE_ID).getEntry(RELATIONSHIPS_FILE);
    // Load the JDOM Document from XML
    Document doc = null;
    try {
        doc = new SAXBuilder().build(url);
    } catch (Exception ex) {
        ex.printStackTrace();
        return;
    }
    // Iterate through all "source" concepts
    for (Object object : doc.getRootElement().getChildren(XML_ELEMENT_SOURCE)) {
        Element elementSource = (Element) object;
        // Source concept name
        String sourceName = elementSource.getAttributeValue(XML_ATTRIBUTE_CONCEPT);
        if (sourceName == null) {
            continue;
        }
        // Get EClass source from mapping
        EClass source = null;
        // Use "Relationship" as a generic super type
        if (sourceName.equals("Relationship")) {
            // $NON-NLS-1$
            source = IArchimatePackage.eINSTANCE.getArchimateRelationship();
        } else // Else use given class
        {
            source = (EClass) IArchimatePackage.eINSTANCE.getEClassifier(sourceName);
        }
        if (source == null) {
            // $NON-NLS-1$
            System.err.println(getClass() + ": Couldn't find source " + sourceName);
            continue;
        }
        // Create a new list of type TargetMatrix
        List<TargetMatrix> matrixList = new ArrayList<TargetMatrix>();
        // Put it in the main matrix map
        matrixMap.put(source, matrixList);
        // Iterate through all child "target" concepts
        for (Object objectChild : elementSource.getChildren(XML_ELEMENT_TARGET)) {
            Element elementTarget = (Element) objectChild;
            // Target concept name
            String targetName = elementTarget.getAttributeValue(XML_ATTRIBUTE_CONCEPT);
            if (targetName == null) {
                continue;
            }
            EClass target = null;
            // Use "Relationship" as a generic super type
            if (targetName.equals("Relationship")) {
                // $NON-NLS-1$
                target = IArchimatePackage.eINSTANCE.getArchimateRelationship();
            } else // Else use given class
            {
                target = (EClass) IArchimatePackage.eINSTANCE.getEClassifier(targetName);
            }
            if (target == null) {
                // $NON-NLS-1$
                System.err.println(getClass() + ": Couldn't find target " + targetName);
                continue;
            }
            // Create a new TargetMatrix and add it to the list
            TargetMatrix matrix = new TargetMatrix();
            matrixList.add(matrix);
            // Set target class
            matrix.targetClass = target;
            // Get relations string
            String relations = elementTarget.getAttributeValue(XML_ATTRIBUTE_RELATIONS);
            if (relations == null) {
                continue;
            }
            // Take each character and add the relationship from the mapping
            for (int i = 0; i < relations.length(); i++) {
                char key = relations.charAt(i);
                EClass relationship = relationsKeyMap.get(key);
                if (relationship != null) {
                    matrix.getRelationships().add(relationship);
                } else {
                    // $NON-NLS-1$
                    System.err.println(getClass() + ": Found unmapped key char: " + key);
                }
            }
        }
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) Document(org.jdom2.Document) URL(java.net.URL) EClass(org.eclipse.emf.ecore.EClass)

Example 40 with Element

use of org.omegat.filters3.Element in project archi by archimatetool.

the class RelationshipsMatrix method loadKeyLetters.

private void loadKeyLetters() {
    // URL url = Platform.getBundle(BUNDLE_ID).getResource(RELATIONSHIPS_KEYS_FILE);
    URL url = Platform.getBundle(BUNDLE_ID).getEntry(RELATIONSHIPS_KEYS_FILE);
    // Load the JDOM Document from XML
    Document doc = null;
    try {
        doc = new SAXBuilder().build(url);
    } catch (Exception ex) {
        ex.printStackTrace();
        return;
    }
    for (Object object : doc.getRootElement().getChildren(XML_ELEMENT_KEY)) {
        Element elementKey = (Element) object;
        String keyLetter = elementKey.getAttributeValue(XML_ATTRIBUTE_CHAR);
        if (keyLetter == null || keyLetter.length() != 1) {
            // $NON-NLS-1$
            System.err.println(getClass() + ": Key letter incorrect: " + keyLetter);
            continue;
        }
        String relationName = elementKey.getAttributeValue(XML_ATTRIBUTE_RELATIONSHIP);
        if (relationName == null) {
            // $NON-NLS-1$
            System.err.println(getClass() + ": Relationship name incorrect: " + relationName);
            continue;
        }
        EClass relationship = (EClass) IArchimatePackage.eINSTANCE.getEClassifier(relationName);
        if (relationship == null) {
            // $NON-NLS-1$
            System.err.println(getClass() + ": Couldn't find relationship " + relationName);
            continue;
        }
        relationsKeyMap.put(keyLetter.charAt(0), relationship);
        relationsValueMap.put(relationship, keyLetter.charAt(0));
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) EClass(org.eclipse.emf.ecore.EClass) Element(org.jdom2.Element) Document(org.jdom2.Document) URL(java.net.URL)

Aggregations

Element (org.jdom2.Element)1444 Document (org.jdom2.Document)242 Test (org.junit.Test)157 ArrayList (java.util.ArrayList)111 Attribute (org.jdom2.Attribute)106 IOException (java.io.IOException)76 File (java.io.File)71 JDOMException (org.jdom2.JDOMException)65 XMLOutputter (org.jdom2.output.XMLOutputter)54 List (java.util.List)51 SAXBuilder (org.jdom2.input.SAXBuilder)51 HashMap (java.util.HashMap)49 Test (org.junit.jupiter.api.Test)34 Map (java.util.Map)33 Node (gov.cms.qpp.conversion.model.Node)29 MCRException (org.mycore.common.MCRException)28 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)28 NamedIcon (jmri.jmrit.catalog.NamedIcon)27 Namespace (org.jdom2.Namespace)25 XmlFile (jmri.jmrit.XmlFile)24