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;
}
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;
}
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());
}
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("");
}
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;
}
Aggregations