Search in sources :

Example 61 with SAXReader

use of org.dom4j.io.SAXReader in project OpenOLAT by OpenOLAT.

the class ItemFileResourceValidator method readDocument.

private Document readDocument(InputStream in) {
    try {
        SAXReader reader = new SAXReader();
        reader.setEntityResolver(new IMSEntityResolver());
        reader.setValidation(false);
        return reader.read(in, "");
    } catch (Exception e) {
        return null;
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) IMSEntityResolver(org.olat.ims.resources.IMSEntityResolver) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 62 with SAXReader

use of org.dom4j.io.SAXReader in project OpenOLAT by OpenOLAT.

the class QTIImportProcessor method processSidecarMetadata.

private boolean processSidecarMetadata(QuestionItemImpl item, DocInfos docInfos) {
    InputStream metadataIn = null;
    try {
        Path path = docInfos.root;
        if (path != null && path.getFileName() != null) {
            Path metadata = path.resolve(path.getFileName().toString() + "_metadata.xml");
            metadataIn = Files.newInputStream(metadata);
            SAXReader reader = new SAXReader();
            Document document = reader.read(metadataIn);
            Element rootElement = document.getRootElement();
            QTIMetadataConverter enricher = new QTIMetadataConverter(rootElement, qItemTypeDao, qEduContextDao, qpoolService);
            enricher.toQuestion(item);
        }
        return true;
    } catch (NoSuchFileException e) {
        // nothing to do
        return true;
    } catch (Exception e) {
        log.error("", e);
        return false;
    } finally {
        IOUtils.closeQuietly(metadataIn);
    }
}
Also used : Path(java.nio.file.Path) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) NoSuchFileException(java.nio.file.NoSuchFileException) Document(org.dom4j.Document) QTIDocument(org.olat.ims.qti.editor.beecom.objects.QTIDocument) NoSuchFileException(java.nio.file.NoSuchFileException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException)

Example 63 with SAXReader

use of org.dom4j.io.SAXReader in project plugin-compat-tester by jenkinsci.

the class MavenPom method transformPom.

public void transformPom(MavenCoordinates coreCoordinates) throws PomTransformationException {
    File pom = new File(rootDir.getAbsolutePath() + "/" + pomFileName);
    File backupedPom = new File(rootDir.getAbsolutePath() + "/" + pomFileName + ".backup");
    try {
        FileUtils.rename(pom, backupedPom);
        Document doc;
        try {
            doc = new SAXReader().read(backupedPom);
        } catch (DocumentException x) {
            throw new IOException(x);
        }
        Element parent = doc.getRootElement().element("parent");
        if (parent != null) {
            Element groupIdElem = parent.element(GROUP_ID_ELEMENT);
            if (groupIdElem != null) {
                groupIdElem.setText(coreCoordinates.groupId);
            }
            Element artifactIdElem = parent.element(ARTIFACT_ID_ELEMENT);
            if (artifactIdElem != null) {
                artifactIdElem.setText(coreCoordinates.artifactId);
            }
            Element versionIdElem = parent.element(VERSION_ELEMENT);
            if (versionIdElem != null) {
                versionIdElem.setText(coreCoordinates.version);
            }
        }
        writeDocument(pom, doc);
    } catch (Exception e) {
        throw new PomTransformationException("Error while transforming pom : " + pom.getAbsolutePath(), e);
    }
}
Also used : PomTransformationException(org.jenkins.tools.test.exception.PomTransformationException) SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) Element(org.dom4j.Element) IOException(java.io.IOException) Document(org.dom4j.Document) File(java.io.File) IOException(java.io.IOException) PomTransformationException(org.jenkins.tools.test.exception.PomTransformationException) DocumentException(org.dom4j.DocumentException)

Example 64 with SAXReader

use of org.dom4j.io.SAXReader in project plugin-compat-tester by jenkinsci.

the class MavenPom method addDependencies.

public void addDependencies(Map<String, VersionNumber> toAdd, Map<String, VersionNumber> toReplace, Map<String, VersionNumber> toAddTest, Map<String, VersionNumber> toReplaceTest, VersionNumber coreDep, Map<String, String> pluginGroupIds, List<String> toConvert) throws IOException {
    File pom = new File(rootDir.getAbsolutePath() + "/" + pomFileName);
    Document doc;
    try {
        doc = new SAXReader().read(pom);
    } catch (DocumentException x) {
        throw new IOException(x);
    }
    Element dependencies = doc.getRootElement().element("dependencies");
    if (dependencies == null) {
        dependencies = doc.getRootElement().addElement("dependencies");
    }
    for (Element mavenDependency : (List<Element>) dependencies.elements("dependency")) {
        Element artifactId = mavenDependency.element(ARTIFACT_ID_ELEMENT);
        if (artifactId == null || !"maven-plugin".equals(artifactId.getTextTrim())) {
            continue;
        }
        Element version = mavenDependency.element(VERSION_ELEMENT);
        if (version == null || version.getTextTrim().startsWith("${")) {
            // Prior to 1.532, plugins sometimes assumed they could pick up the Maven plugin version from their parent POM.
            if (version != null) {
                mavenDependency.remove(version);
            }
            version = mavenDependency.addElement(VERSION_ELEMENT);
            version.addText(coreDep.toString());
        }
    }
    Map<String, VersionNumber> toReplaceUsed = new LinkedHashMap<>();
    Map<String, VersionNumber> toReplaceTestUsed = new LinkedHashMap<>();
    for (Element mavenDependency : (List<Element>) dependencies.elements("dependency")) {
        Element artifactId = mavenDependency.element(ARTIFACT_ID_ELEMENT);
        Element groupId = mavenDependency.element(GROUP_ID_ELEMENT);
        if (artifactId == null || groupId == null) {
            continue;
        }
        String expectedGroupId = pluginGroupIds.get(artifactId.getTextTrim());
        if (expectedGroupId == null || !groupId.getTextTrim().equals(expectedGroupId)) {
            continue;
        }
        String trimmedArtifactId = artifactId.getTextTrim();
        excludeSecurity144Compat(mavenDependency);
        VersionNumber replacement = toReplace.get(trimmedArtifactId);
        if (replacement == null) {
            replacement = toReplaceTest.get(trimmedArtifactId);
            if (replacement == null) {
                continue;
            }
            toReplaceTestUsed.put(trimmedArtifactId, replacement);
        }
        Element version = mavenDependency.element(VERSION_ELEMENT);
        if (version != null) {
            mavenDependency.remove(version);
        }
        version = mavenDependency.addElement(VERSION_ELEMENT);
        if (toConvert.contains(artifactId)) {
            // Remove the test scope
            Element scope = mavenDependency.element("scope");
            if (scope != null) {
                mavenDependency.remove(scope);
            }
        }
        version.addText(replacement.toString());
        toReplaceUsed.put(trimmedArtifactId, replacement);
    }
    // If the replacement dependencies weren't explicitly present in the pom, add them directly now
    toReplace.entrySet().removeAll(toReplaceUsed.entrySet());
    toReplaceTest.entrySet().removeAll(toReplaceTestUsed.entrySet());
    toAdd.putAll(toReplace);
    toAddTest.putAll(toReplaceTest);
    dependencies.addComment("SYNTHETIC");
    addPlugins(toAdd, pluginGroupIds, dependencies, null);
    addPlugins(toAddTest, pluginGroupIds, dependencies, "test");
    writeDocument(pom, doc);
}
Also used : SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) Element(org.dom4j.Element) List(java.util.List) IOException(java.io.IOException) Document(org.dom4j.Document) File(java.io.File) VersionNumber(hudson.util.VersionNumber) LinkedHashMap(java.util.LinkedHashMap)

Example 65 with SAXReader

use of org.dom4j.io.SAXReader in project tdq-studio-se by Talend.

the class SQLHistory method loadFromFileV350.

private void loadFromFileV350() {
    try {
        File file = new File(ApplicationFiles.SQLHISTORY_FILE_NAME_V350);
        SAXReader reader = new SAXReader();
        Element root = reader.read(file).getRootElement();
        _history.clear();
        _filteredHistory.clear();
        for (Element elem : root.elements(SQLHistoryElement.ELEMENT)) _history.add(new SQLHistoryElement(elem));
    } catch (DocumentException e) {
        SQLExplorerPlugin.error("Cannot load history (v3.5.0 format)", e);
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) DocumentException(org.dom4j.DocumentException) File(java.io.File)

Aggregations

SAXReader (org.dom4j.io.SAXReader)322 Document (org.dom4j.Document)256 Element (org.dom4j.Element)195 StringReader (java.io.StringReader)120 DocumentException (org.dom4j.DocumentException)74 Test (org.junit.jupiter.api.Test)74 File (java.io.File)54 List (java.util.List)49 IOException (java.io.IOException)48 InputStream (java.io.InputStream)48 ArrayList (java.util.ArrayList)47 Node (org.dom4j.Node)28 FileInputStream (java.io.FileInputStream)25 HashMap (java.util.HashMap)24 XMLWriter (org.dom4j.io.XMLWriter)22 ByteArrayInputStream (java.io.ByteArrayInputStream)20 URL (java.net.URL)18 OutputFormat (org.dom4j.io.OutputFormat)18 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)17 Test (org.junit.Test)14