Search in sources :

Example 1 with InputSource

use of org.apache.maven.model.InputSource in project maven by apache.

the class DefaultModelBuilder method doReadFileModel.

@SuppressWarnings("checkstyle:methodlength")
private Model doReadFileModel(ModelSource modelSource, ModelBuildingRequest request, DefaultModelProblemCollector problems) throws ModelBuildingException {
    Model model;
    problems.setSource(modelSource.getLocation());
    try {
        boolean strict = request.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0;
        Map<String, Object> options = new HashMap<>(3);
        options.put(ModelProcessor.IS_STRICT, strict);
        options.put(ModelProcessor.SOURCE, modelSource);
        InputSource source;
        if (request.isLocationTracking()) {
            source = (InputSource) options.computeIfAbsent(ModelProcessor.INPUT_SOURCE, k -> new InputSource());
        } else {
            source = null;
        }
        try {
            model = modelProcessor.read(modelSource.getInputStream(), options);
        } catch (ModelParseException e) {
            if (!strict) {
                throw e;
            }
            options.put(ModelProcessor.IS_STRICT, Boolean.FALSE);
            try {
                model = modelProcessor.read(modelSource.getInputStream(), options);
            } catch (ModelParseException ne) {
                // still unreadable even in non-strict mode, rethrow original error
                throw e;
            }
            Severity severity = (modelSource instanceof FileModelSource) ? Severity.ERROR : Severity.WARNING;
            problems.add(new ModelProblemCollectorRequest(severity, Version.V20).setMessage("Malformed POM " + modelSource.getLocation() + ": " + e.getMessage()).setException(e));
        }
        if (source != null) {
            source.setModelId(ModelProblemUtils.toId(model));
            source.setLocation(modelSource.getLocation());
        }
    } catch (ModelParseException e) {
        problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.BASE).setMessage("Non-parseable POM " + modelSource.getLocation() + ": " + e.getMessage()).setException(e));
        throw problems.newModelBuildingException();
    } catch (IOException e) {
        String msg = e.getMessage();
        if (msg == null || msg.length() <= 0) {
            // NOTE: There's java.nio.charset.MalformedInputException and sun.io.MalformedInputException
            if (e.getClass().getName().endsWith("MalformedInputException")) {
                msg = "Some input bytes do not match the file encoding.";
            } else {
                msg = e.getClass().getSimpleName();
            }
        }
        problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.BASE).setMessage("Non-readable POM " + modelSource.getLocation() + ": " + msg).setException(e));
        throw problems.newModelBuildingException();
    }
    if (modelSource instanceof FileModelSource) {
        model.setPomFile(((FileModelSource) modelSource).getFile());
    }
    problems.setSource(model);
    modelValidator.validateFileModel(model, request, problems);
    if (hasFatalErrors(problems)) {
        throw problems.newModelBuildingException();
    }
    return model;
}
Also used : InputSource(org.apache.maven.model.InputSource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ModelParseException(org.apache.maven.model.io.ModelParseException) Model(org.apache.maven.model.Model) Severity(org.apache.maven.model.building.ModelProblem.Severity) IOException(java.io.IOException)

Example 2 with InputSource

use of org.apache.maven.model.InputSource in project maven by apache.

the class DefaultModelReader method readModelEx.

private Model readModelEx(XmlPullParser parser, InputSource source, boolean strict) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    if (readMethodEx == null) {
        readMethodEx = MavenXpp3ReaderEx.class.getDeclaredMethod("read", XmlPullParser.class, boolean.class, InputSource.class);
        readMethodEx.setAccessible(true);
    }
    MavenXpp3ReaderEx mr = new MavenXpp3ReaderEx();
    Object model = readMethodEx.invoke(mr, parser, strict, source);
    return (Model) model;
}
Also used : MavenXpp3ReaderEx(org.apache.maven.model.io.xpp3.MavenXpp3ReaderEx) InputSource(org.apache.maven.model.InputSource) XmlPullParser(org.codehaus.plexus.util.xml.pull.XmlPullParser) Model(org.apache.maven.model.Model)

Example 3 with InputSource

use of org.apache.maven.model.InputSource in project maven by apache.

the class StringSearchModelInterpolatorTest method locationTrackerShouldBeExcludedFromInterpolation.

@Test
public void locationTrackerShouldBeExcludedFromInterpolation() {
    Properties props = new Properties();
    props.setProperty("expression", "value");
    DefaultModelBuildingRequest request = new DefaultModelBuildingRequest();
    request.setUserProperties(props);
    InputSource source = new InputSource();
    source.setLocation("${expression}");
    source.setModelId("${expression}");
    Model model = new Model();
    model.setLocation("", new InputLocation(1, 1, source));
    SimpleProblemCollector problems = new SimpleProblemCollector();
    StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator(null, null, new DefaultModelVersionProcessor());
    interpolator.interpolateObject(model, model, null, request, problems);
    assertProblemFree(problems);
    assertEquals("${expression}", source.getLocation());
    assertEquals("${expression}", source.getModelId());
}
Also used : SimpleProblemCollector(org.apache.maven.model.building.SimpleProblemCollector) InputSource(org.apache.maven.model.InputSource) InputLocation(org.apache.maven.model.InputLocation) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) Model(org.apache.maven.model.Model) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 4 with InputSource

use of org.apache.maven.model.InputSource in project m2e-core by eclipse-m2e.

the class PomTextHover method getLabelForRegion.

/**
 * @param region
 */
public static StyledString getLabelForRegion(ManagedArtifactRegion region) {
    MavenProject mavprj = region.project;
    if (mavprj != null) {
        String version = null;
        if (region.isDependency) {
            version = PomTemplateContext.searchDM(mavprj, region.groupId, region.artifactId);
        }
        if (region.isPlugin) {
            version = PomTemplateContext.searchPM(mavprj, region.groupId, region.artifactId);
        }
        StyledString ret = new StyledString();
        if (version != null) {
            ret.append(org.eclipse.m2e.editor.internal.Messages.PomTextHover_managed_version);
            // not happy with decorations but how to just do bold text
            ret.append(version, StyledString.DECORATIONS_STYLER);
        } else {
            ret.append(org.eclipse.m2e.editor.internal.Messages.PomTextHover_managed_version_missing);
        }
        InputLocation openLocation = PomHyperlinkDetector.findLocationForManagedArtifact(region, mavprj);
        if (openLocation != null) {
            // MNGECLIPSE-2539 apparently you can have an InputLocation with null input source.
            // check!
            InputSource source = openLocation.getSource();
            if (source != null) {
                // a space after the version value
                ret.append(" ");
                ret.append(NLS.bind(org.eclipse.m2e.editor.internal.Messages.PomTextHover_managed_location, source.getModelId()));
            }
        } else {
            // a space after the version value
            ret.append(" ");
            ret.append(org.eclipse.m2e.editor.internal.Messages.PomTextHover_managed_location_missing);
        }
        return ret;
    }
    // $NON-NLS-1$
    return new StyledString("");
}
Also used : InputLocation(org.apache.maven.model.InputLocation) InputSource(org.apache.maven.model.InputSource) MavenProject(org.apache.maven.project.MavenProject) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString)

Example 5 with InputSource

use of org.apache.maven.model.InputSource in project m2e-core by eclipse-m2e.

the class XmlUtils method fileForInputLocation.

/**
 * converts an InputLocation to a file path on the local disk, null if not available. still the input source's model
 * value can be used further..
 *
 * @param location
 * @return
 */
public static File fileForInputLocation(InputLocation location, MavenProject origin) {
    InputSource source = location.getSource();
    if (source != null) {
        // MNGECLIPSE-2539 apparently if maven can't resolve the model from local storage,
        // the location will be empty. not only applicable to local repo models but
        // apparently also to models in workspace not reachable by relativePath
        String loc = source.getLocation();
        File file = null;
        if (loc != null) {
            file = new File(loc);
        } else {
            // try to find pom by coordinates..
            String modelId = source.getModelId();
            if (origin.getModel().getId().equals(modelId) && origin.getFile() != null) {
                return origin.getFile();
            }
            String[] splitStrings = modelId.split(":");
            assert splitStrings.length == 3;
            IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getMavenProject(splitStrings[0], splitStrings[1], splitStrings[2]);
            if (facade != null) {
                file = facade.getPomFile();
            } else {
                // if not in the workspace, try looking into the local repository.
                IMaven maven = MavenPlugin.getMaven();
                try {
                    String path = maven.getArtifactPath(maven.getLocalRepository(), splitStrings[0], splitStrings[1], splitStrings[2], "pom", null);
                    if (path != null) {
                        file = new File(maven.getLocalRepositoryPath(), path);
                    }
                } catch (CoreException e) {
                    log.error("Failed to calculate local repository path of artifact", e);
                }
            }
        }
        return file;
    }
    return null;
}
Also used : InputSource(org.apache.maven.model.InputSource) CoreException(org.eclipse.core.runtime.CoreException) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IMaven(org.eclipse.m2e.core.embedder.IMaven)

Aggregations

InputSource (org.apache.maven.model.InputSource)14 InputLocation (org.apache.maven.model.InputLocation)6 IOException (java.io.IOException)4 Model (org.apache.maven.model.Model)4 File (java.io.File)3 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)3 XmlPullParser (org.codehaus.plexus.util.xml.pull.XmlPullParser)3 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 Plugin (org.apache.maven.model.Plugin)2 MavenProject (org.apache.maven.project.MavenProject)2 MXParser (org.codehaus.plexus.util.xml.pull.MXParser)2 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)2 StyledString (org.eclipse.jface.viewers.StyledString)2 FileInputStream (java.io.FileInputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Properties (java.util.Properties)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1