Search in sources :

Example 1 with JDOMException

use of org.jdom2.JDOMException in project dq-easy-cloud by dq-open-cloud.

the class DqXMLUtils method getMapFromInputStream.

/**
 * <p>
 * 将xml流中的信息放入map中
 * </p>
 *
 * @param inputStream
 *            : InputStream : xml输入流
 * @param paramsMap
 *            : Map<String, Object> : xml结果容器
 * @return Map<String, Object>
 * @throws IOException
 * @author daiqi 创建时间 2018年2月23日 下午12:49:06
 */
public static Map<String, Object> getMapFromInputStream(InputStream inputStream, Map<String, Object> paramsMap) throws IOException {
    if (null == paramsMap) {
        paramsMap = new HashMap<>();
    }
    SAXBuilder builder = new SAXBuilder();
    try {
        Document doc = builder.build(inputStream);
        Element root = doc.getRootElement();
        List<Element> list = root.getChildren();
        Iterator<Element> it = list.iterator();
        while (it.hasNext()) {
            Element e = (Element) it.next();
            String k = e.getName();
            Object v = DqStringUtils.EMPTY;
            List<Element> children = e.getChildren();
            if (children.isEmpty()) {
                v = e.getTextNormalize();
            } else {
                v = getChildren(children);
            }
            paramsMap.put(k, v);
        }
    } catch (JDOMException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    return paramsMap;
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException)

Example 2 with JDOMException

use of org.jdom2.JDOMException in project archi by archimatetool.

the class JDOMUtils method readXMLFile.

/**
 * Reads and returns a JDOM Document from file with Schema validation
 * @param xmlFile The XML File
 * @param schemaFile One or more Schema files
 * @return The JDOM Document or null if not found
 * @throws JDOMException
 * @throws IOException
 */
public static Document readXMLFile(File xmlFile, File... schemaFiles) throws IOException, JDOMException {
    XMLReaderJDOMFactory factory = new XMLReaderXSDFactory(schemaFiles);
    SAXBuilder builder = new SAXBuilder(factory);
    // This allows UNC mapped locations to load
    return builder.build(new FileInputStream(xmlFile));
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) XMLReaderXSDFactory(org.jdom2.input.sax.XMLReaderXSDFactory) XMLReaderJDOMFactory(org.jdom2.input.sax.XMLReaderJDOMFactory) FileInputStream(java.io.FileInputStream)

Example 3 with JDOMException

use of org.jdom2.JDOMException 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 4 with JDOMException

use of org.jdom2.JDOMException in project archi by archimatetool.

the class ViewpointManager method loadDefaultViewpointsFile.

/**
 * Load viewpoints from XML file
 */
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$
        // $NON-NLS-1$
        String id = xmlViewpoint.getAttributeValue("id");
        if (id == null || "".equals(id)) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            System.err.println("Blank id for viewpoint");
            continue;
        }
        // $NON-NLS-1$
        Element xmlName = xmlViewpoint.getChild("name");
        if (xmlName == null) {
            // $NON-NLS-1$
            System.err.println("No name element for viewpoint");
            continue;
        }
        String name = xmlName.getText();
        if (name == null || "".equals(name)) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            System.err.println("Blank name for viewpoint");
            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$
                // $NON-NLS-1$
                System.err.println("Blank concept name for viewpoint");
                continue;
            }
            if (ELEMENTS_MAP.containsKey(conceptName)) {
                addCollection(vp, conceptName);
            } else {
                EClass eClass = (EClass) IArchimatePackage.eINSTANCE.getEClassifier(conceptName);
                if (eClass != null) {
                    addConcept(vp, eClass);
                } else {
                    // $NON-NLS-1$
                    System.err.println("Couldn't get eClass: " + conceptName);
                }
            }
        }
        VIEWPOINTS.put(id, vp);
    }
}
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)

Example 5 with JDOMException

use of org.jdom2.JDOMException in project JMRI by JMRI.

the class JmriJTablePersistenceManager method savePreferences.

@Override
public synchronized void savePreferences(Profile profile) {
    log.debug("Saving preferences (dirty={})...", this.dirty);
    Element element = new Element(TABLES_ELEMENT, TABLES_NAMESPACE);
    if (!this.columns.isEmpty()) {
        this.columns.entrySet().stream().map((entry) -> {
            Element table = new Element("table").setAttribute("name", entry.getKey());
            Element columnsElement = new Element("columns");
            entry.getValue().entrySet().stream().map((column) -> {
                Element columnElement = new Element("column").setAttribute("name", column.getKey());
                if (column.getValue().getOrder() != -1) {
                    columnElement.setAttribute("order", Integer.toString(column.getValue().getOrder()));
                }
                if (column.getValue().getWidth() != -1) {
                    columnElement.setAttribute("width", Integer.toString(column.getValue().getWidth()));
                }
                columnElement.setAttribute("hidden", Boolean.toString(column.getValue().getHidden()));
                return columnElement;
            }).forEach((columnElement) -> {
                columnsElement.addContent(columnElement);
            });
            table.addContent(columnsElement);
            List<SortKey> keys = this.sortKeys.get(entry.getKey());
            if (keys != null) {
                Element sorter = new Element(SORT_ORDER);
                keys.stream().forEach((key) -> {
                    sorter.addContent(new Element("sortKey").setAttribute("column", Integer.toString(key.getColumn())).setAttribute(SORT_ORDER, key.getSortOrder().name()));
                });
                table.addContent(sorter);
            }
            return table;
        }).forEach((table) -> {
            element.addContent(table);
        });
    }
    try {
        ProfileUtils.getUserInterfaceConfiguration(ProfileManager.getDefault().getActiveProfile()).putConfigurationFragment(JDOMUtil.toW3CElement(element), false);
    } catch (JDOMException ex) {
        log.error("Unable to save user preferences", ex);
    }
    this.dirty = false;
}
Also used : Arrays(java.util.Arrays) TableColumnModel(javax.swing.table.TableColumnModel) Enumeration(java.util.Enumeration) SortKey(javax.swing.RowSorter.SortKey) XTableColumnModel(jmri.util.swing.XTableColumnModel) AbstractPreferencesManager(jmri.util.prefs.AbstractPreferencesManager) LoggerFactory(org.slf4j.LoggerFactory) ProfileManager(jmri.profile.ProfileManager) HashMap(java.util.HashMap) Timer(java.util.Timer) InitializationException(jmri.util.prefs.InitializationException) ArrayList(java.util.ArrayList) JDOMException(org.jdom2.JDOMException) JDOMUtil(jmri.util.jdom.JDOMUtil) ProfileUtils(jmri.profile.ProfileUtils) Map(java.util.Map) Profile(jmri.profile.Profile) TableColumnModelEvent(javax.swing.event.TableColumnModelEvent) RowSorter(javax.swing.RowSorter) TimerTask(java.util.TimerTask) ListSelectionEvent(javax.swing.event.ListSelectionEvent) Nonnull(javax.annotation.Nonnull) PropertyChangeEvent(java.beans.PropertyChangeEvent) ChangeEvent(javax.swing.event.ChangeEvent) Logger(org.slf4j.Logger) TableColumn(javax.swing.table.TableColumn) RowSorterListener(javax.swing.event.RowSorterListener) Set(java.util.Set) Objects(java.util.Objects) SortOrder(javax.swing.SortOrder) List(java.util.List) TableColumnModelListener(javax.swing.event.TableColumnModelListener) PropertyChangeListener(java.beans.PropertyChangeListener) JTable(javax.swing.JTable) DataConversionException(org.jdom2.DataConversionException) CheckForNull(javax.annotation.CheckForNull) RowSorterEvent(javax.swing.event.RowSorterEvent) Element(org.jdom2.Element) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) List(java.util.List) JDOMException(org.jdom2.JDOMException)

Aggregations

Element (org.jdom2.Element)154 Document (org.jdom2.Document)113 JDOMException (org.jdom2.JDOMException)89 IOException (java.io.IOException)75 SAXBuilder (org.jdom2.input.SAXBuilder)67 Test (org.junit.Test)36 File (java.io.File)32 ArrayList (java.util.ArrayList)22 InputStream (java.io.InputStream)17 Attribute (org.jdom2.Attribute)16 StringReader (java.io.StringReader)15 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)14 HashMap (java.util.HashMap)13 XMLOutputter (org.jdom2.output.XMLOutputter)13 SAXException (org.xml.sax.SAXException)13 URL (java.net.URL)12 XmlFile (jmri.jmrit.XmlFile)12 List (java.util.List)11 MCRObject (org.mycore.datamodel.metadata.MCRObject)11 MCRException (org.mycore.common.MCRException)10