Search in sources :

Example 61 with JDOMException

use of org.jdom.JDOMException in project intellij-community by JetBrains.

the class XmlSerializer method deserialize.

@NotNull
public static <T> T deserialize(@NotNull URL url, Class<T> aClass) throws XmlSerializationException {
    try {
        Document document = JDOMUtil.loadDocument(url);
        document = JDOMXIncluder.resolve(document, url.toExternalForm());
        return deserialize(document.getRootElement(), aClass);
    } catch (IOException e) {
        throw new XmlSerializationException(e);
    } catch (JDOMException e) {
        throw new XmlSerializationException(e);
    }
}
Also used : IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) NotNull(org.jetbrains.annotations.NotNull)

Example 62 with JDOMException

use of org.jdom.JDOMException in project Asqatasun by Asqatasun.

the class KbCsvMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        FileReader fin = new FileReader(inCsv);
        BufferedReader reader = new BufferedReader(fin);
        if (!outCsv.getParentFile().exists()) {
            outCsv.getParentFile().mkdirs();
        }
        PrintWriter writer = new PrintWriter(outCsv);
        writer.println(formatCsvLine("name", "url", "campain", "test", "result"));
        String readed;
        while ((readed = reader.readLine()) != null) {
            // CSV params: 0=campain, 1=test
            String[] params = readed.split(";");
            if (params.length != 2) {
                getLog().warn("Incorrect line: " + readed);
                continue;
            }
            String ruleUrl = baseUrl + params[0] + "/" + params[1] + "/";
            for (int i = 0; i < KB_TEST_RESULTS.length; i++) {
                String url = ruleUrl + KB_TEST_RESULTS[i] + "/?lang=en";
                try {
                    for (String result : getUrls(url)) {
                        // Substring 28 to remove URL beginning
                        String targetName = sanitizeClassName(result.substring(28));
                        writer.println(formatCsvLine(targetName, result, params[0], params[1], TG_TEST_RESULTS[i]));
                    }
                } catch (JaxenException ex) {
                    getLog().warn("Unable to select URLs for address " + url, ex);
                } catch (JDOMException ex) {
                    getLog().warn("Invalid XML at " + url, ex);
                }
            }
        }
        writer.close();
        reader.close();
        fin.close();
    } catch (IOException ex) {
        throw new MojoExecutionException("Unexpected IO Exception", ex);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) JaxenException(org.jaxen.JaxenException) JDOMException(org.jdom.JDOMException)

Example 63 with JDOMException

use of org.jdom.JDOMException in project maven-plugins by apache.

the class AbstractEffectiveMojo method addMavenNamespace.

/**
     * Add a Pom/Settings namespaces to the effective XML content.
     *
     * @param effectiveXml not null the effective POM or Settings
     * @param isPom if <code>true</code> add the Pom xsd url, otherwise add the settings xsd url.
     * @return the content of the root element, i.e. &lt;project/&gt; or &lt;settings/&gt; with the Maven namespace or
     *         the original <code>effective</code> if an error occurred.
     * @see #POM_XSD_URL
     * @see #SETTINGS_XSD_URL
     */
protected static String addMavenNamespace(String effectiveXml, boolean isPom) {
    SAXBuilder builder = new SAXBuilder();
    try {
        Document document = builder.build(new StringReader(effectiveXml));
        Element rootElement = document.getRootElement();
        // added namespaces
        Namespace pomNamespace = Namespace.getNamespace("", "http://maven.apache.org/POM/4.0.0");
        rootElement.setNamespace(pomNamespace);
        Namespace xsiNamespace = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        rootElement.addNamespaceDeclaration(xsiNamespace);
        if (rootElement.getAttribute("schemaLocation", xsiNamespace) == null) {
            rootElement.setAttribute("schemaLocation", "http://maven.apache.org/POM/4.0.0 " + (isPom ? POM_XSD_URL : SETTINGS_XSD_URL), xsiNamespace);
        }
        ElementFilter elementFilter = new ElementFilter(Namespace.getNamespace(""));
        for (Iterator<?> i = rootElement.getDescendants(elementFilter); i.hasNext(); ) {
            Element e = (Element) i.next();
            e.setNamespace(pomNamespace);
        }
        StringWriter w = new StringWriter();
        Format format = Format.getPrettyFormat();
        XMLOutputter out = new XMLOutputter(format);
        out.output(document.getRootElement(), w);
        return w.toString();
    } catch (JDOMException e) {
        return effectiveXml;
    } catch (IOException e) {
        return effectiveXml;
    }
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) SAXBuilder(org.jdom.input.SAXBuilder) Format(org.jdom.output.Format) StringWriter(java.io.StringWriter) Element(org.jdom.Element) ElementFilter(org.jdom.filter.ElementFilter) StringReader(java.io.StringReader) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) Namespace(org.jdom.Namespace)

Example 64 with JDOMException

use of org.jdom.JDOMException in project maven-plugins by apache.

the class EffectivePomMojo method prettyFormat.

/**
     * @param effectivePom not null
     * @return pretty format of the xml  or the original <code>effectivePom</code> if an error occurred.
     */
private static String prettyFormat(String effectivePom) {
    SAXBuilder builder = new SAXBuilder();
    try {
        Document effectiveDocument = builder.build(new StringReader(effectivePom));
        StringWriter w = new StringWriter();
        Format format = Format.getPrettyFormat();
        XMLOutputter out = new XMLOutputter(format);
        out.output(effectiveDocument, w);
        return w.toString();
    } catch (JDOMException e) {
        return effectivePom;
    } catch (IOException e) {
        return effectivePom;
    }
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) SAXBuilder(org.jdom.input.SAXBuilder) Format(org.jdom.output.Format) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException)

Example 65 with JDOMException

use of org.jdom.JDOMException in project intellij-plugins by StepicOrg.

the class ModuleUtils method createStepModule.

static void createStepModule(@NotNull Project project, @NotNull StepNode step, @NotNull ModifiableModuleModel moduleModel) {
    StudyNode lesson = step.getParent();
    if (lesson != null) {
        String moduleDir = String.join("/", project.getBasePath(), lesson.getPath());
        StepModuleBuilder stepModuleBuilder = new StepModuleBuilder(moduleDir, step);
        try {
            stepModuleBuilder.createModule(moduleModel);
        } catch (IOException | ModuleWithNameAlreadyExists | JDOMException | ConfigurationException e) {
            logger.warn("Cannot create step: " + step.getDirectory(), e);
        }
    }
}
Also used : ConfigurationException(com.intellij.openapi.options.ConfigurationException) ModuleWithNameAlreadyExists(com.intellij.openapi.module.ModuleWithNameAlreadyExists) IOException(java.io.IOException) JDOMException(org.jdom.JDOMException) StudyNode(org.stepik.core.courseFormat.StudyNode) StepModuleBuilder(org.stepik.plugin.projectWizard.idea.StepModuleBuilder)

Aggregations

JDOMException (org.jdom.JDOMException)72 IOException (java.io.IOException)59 Element (org.jdom.Element)46 Document (org.jdom.Document)27 SAXBuilder (org.jdom.input.SAXBuilder)20 File (java.io.File)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 Nullable (org.jetbrains.annotations.Nullable)11 NotNull (org.jetbrains.annotations.NotNull)10 StringReader (java.io.StringReader)9 THashMap (gnu.trove.THashMap)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 InvalidDataException (com.intellij.openapi.util.InvalidDataException)5 Path (java.nio.file.Path)4 Logger (com.intellij.openapi.diagnostic.Logger)3 Project (com.intellij.openapi.project.Project)3 StringWriter (java.io.StringWriter)3 Format (org.jdom.output.Format)3 XMLOutputter (org.jdom.output.XMLOutputter)3