Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id D89C1200C0A for ; Sat, 28 Jan 2017 15:51:31 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id D71D1160B51; Sat, 28 Jan 2017 14:51:31 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id D4384160B35 for ; Sat, 28 Jan 2017 15:51:30 +0100 (CET) Received: (qmail 48840 invoked by uid 500); 28 Jan 2017 14:51:24 -0000 Mailing-List: contact dev-help@maven.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Maven Developers List" Reply-To: "Maven Developers List" Delivered-To: mailing list dev@maven.apache.org Received: (qmail 48828 invoked by uid 99); 28 Jan 2017 14:51:24 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 28 Jan 2017 14:51:24 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id F01B518BB81 for ; Sat, 28 Jan 2017 14:51:23 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 2.38 X-Spam-Level: ** X-Spam-Status: No, score=2.38 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HTML_MESSAGE=2, MANY_SPAN_IN_TEXT=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_SORBS_SPAM=0.5, SPF_PASS=-0.001] autolearn=disabled Authentication-Results: spamd3-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id Wm33wVIuHSCt for ; Sat, 28 Jan 2017 14:51:20 +0000 (UTC) Received: from mail-wm0-f49.google.com (mail-wm0-f49.google.com [74.125.82.49]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTPS id B97385F2F0 for ; Sat, 28 Jan 2017 14:51:19 +0000 (UTC) Received: by mail-wm0-f49.google.com with SMTP id c206so183745545wme.0 for ; Sat, 28 Jan 2017 06:51:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:mime-version:date:subject:message-id:to; bh=G8zvEPBRxjyG3OnJe/DmaiThbFUD43wxVryLP9EQJl8=; b=jQilXG7BDGbL6hE7zd91GBxqJxHdJRfPjsCpoTWKBxaDcHjSjdzh0KQin3syUOgAmp I/GiiMPvVyvWEBa2MzoK5qOxOe9nPTUlZIKpUyh8syHdYsXyR6TSlC9kZfrOFg4WlzZs kIvfFd4axecSylBfKDo0ma057cNPxvVCv4qIqsd5vzY64FpquhX7va4E9p9Hc8WOtz7S gfeZIbuHqv9//T+fX6uLNYdAqJEoOWU0AtkZD4RaSRhT8oX0zfI/VcngCepF0s3I9tc8 Ta0v1etVu2XrJ0AhQyJSBLPWkoUctdHNkLRpKU5KD8KQcx3vrG5pIb/K/Hfbs+/FqgZe 9tgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:mime-version:date:subject:message-id:to; bh=G8zvEPBRxjyG3OnJe/DmaiThbFUD43wxVryLP9EQJl8=; b=mrUmX3lT7OY9CkZ+33SP3N9iVIvISydwTzmAz9/r21TKt7rSe6RPWp2F9ZlvWCTTnr jdXvhTQS1Ekbdm/248OMO3wUyfS+tuEauiskn/bpAevOM5MiQG5R2PrAXRrm2lAFn3wZ K6ORYN3MZKkY3jx+CnNGdwgR9Ms+KgAoLxv5n+Cv3LPzZ6e1oM1zh3tjxG4jkyBvRPCN Yg5kRKBx4S0lU/SBZ7JDKJQ2aXaONjpPZnUIldZC101JFSkM4elkfNrpdG/pQxl0mI0Q F7NKPnsyE4OqyAVH+ua4vlMHij/rE0Mfa7Vn1XstoBMwbgSio4222vNU5fCwAv2SFfC+ t9lQ== X-Gm-Message-State: AIkVDXIurAGoVOSnp+t0V074sXl0VY//S5bgmt+3ig3uq8SY4MRBgHtOfioVMawSncGuTw== X-Received: by 10.28.158.82 with SMTP id h79mr7884413wme.30.1485615072404; Sat, 28 Jan 2017 06:51:12 -0800 (PST) Received: from [172.22.22.95] ([37.0.49.54]) by smtp.gmail.com with ESMTPSA id y1sm9203064wme.15.2017.01.28.06.51.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 28 Jan 2017 06:51:11 -0800 (PST) From: Michel Pawlak Content-Type: multipart/alternative; boundary="Apple-Mail=_B2C8D872-E102-4573-85E1-79CF1E5C089F" Mime-Version: 1.0 (Mac OS X Mail 10.2 \(3259\)) Date: Sat, 28 Jan 2017 15:51:10 +0100 Subject: What's the correct way to use InputLocationTracker? Message-Id: <1E1413C8-08C7-4B95-9601-EF4306845756@gmail.com> To: dev@maven.apache.org X-Mailer: Apple Mail (2.3259) archived-at: Sat, 28 Jan 2017 14:51:32 -0000 --Apple-Mail=_B2C8D872-E102-4573-85E1-79CF1E5C089F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hi all, I need some help with issues I=E2=80=99m facing when using = org.apache.maven.model.InputLocationTracker. Let=E2=80=99s first have a look at this sample class file (I tried to = only keep what is relevant to the issue.) -[snip]=E2=80=94 import java.util.ArrayList; import java.util.List; import org.apache.maven.model.Build; import org.apache.maven.model.InputLocation; import org.apache.maven.model.InputLocationTracker; import org.apache.maven.model.Model; import org.apache.maven.model.Plugin; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Sample check that verifies if Java source version is the ones that is = expected. */ public class JavaSourceVersionCheck { private static final Logger LOGGER =3D = LoggerFactory.getLogger(JavaSourceVersionCheck.class); private static final String MAVEN_COMPILER_PLUGIN_GROUPID =3D = "org.apache.maven.plugins"; private static final String MAVEN_COMPILER_PLUGIN_ARTIFACTID =3D = "maven-compiler-plugin"; /** * Default Java source version. */ public static final String EXPECTED_JAVA_SOURCE_VERSION =3D "1.7"; /** * Default Java target version. */ public static final String EXPECTED_JAVA_TARGET_VERSION =3D "1.7"; /** * Executes the check on a Maven Model instance. * * @param model Maven raw Model (i.e. with unresolved pom = inheritance) */ public void execute(final Model model) { LOGGER.debug("checking Model"); final Build build =3D model.getBuild(); if (build !=3D null) { for (final Plugin plugin : build.getPlugins()) { if (isMavenCompilerPlugin(plugin)) { LOGGER.debug("Maven compiler plugin found!"); checkJavaSourceVersion(plugin); } } } LOGGER.debug("done"); } private boolean isMavenCompilerPlugin(final Plugin plugin) { return MAVEN_COMPILER_PLUGIN_GROUPID.equals(plugin.getGroupId()) = && MAVEN_COMPILER_PLUGIN_ARTIFACTID.equals(plugin.getArtifactId()); } private void checkJavaSourceVersion(final Plugin plugin) { final Xpp3Dom configuration =3D (Xpp3Dom) = plugin.getConfiguration(); final Xpp3Dom sourceDom =3D configuration.getChild("source"); LOGGER.debug("Configuration DOM: {}", configuration.toString()); if (sourceDom !=3D null) { LOGGER.debug("Found tag, checking its value"); final String sourceValue =3D sourceDom.getValue(); if (!sourceValue.equals(EXPECTED_JAVA_SOURCE_VERSION)) { final InputLocation location =3D locate(plugin, = =E2=80=9Cconfiguration", "source"); // here I should do something else with the location = instance, but as I want to keep the sample as simple as possible, = let=E2=80=99s just log a message LOGGER.warn(=E2=80=9Cnon compliant value: is {} but = should be {} (issue was found at {} column {})", sourceValue, = EXPECTED_JAVA_SOURCE_VERSION, location.getLineNumber(), location.getColumnNumber()); } } } /** * Locates the line in the {@link Model} POM where XML tags below an = {@link InputLocationTracker} is located. * * @param locationTracker Maven {@link Model} {@link = InputLocationTracker} for which the line has to be retrieved. * @param tags list of tags to drill down, the last one representing = the tag for which the line has to be retrieved. * @return an IssueLocation representing the line and column in the = {@link Model} where the last tag of the {@code tags} list below {@link = InputLocationTracker} is located. */ public final InputLocation locate(final InputLocationTracker = locationTracker, final String... tags) { final List tagsList =3D new ArrayList<>(); if (tags.length =3D=3D 0) { tagsList.add(""); } else { for (final String tag : tags) { tagsList.add(tag); } } final InputLocation inputLocation =3D locate(locationTracker, = tagsList); LOGGER.debug("\t<{}> tag located at line {}, column {})", = tagsList.get(tagsList.size() - 1), inputLocation.getLineNumber(), = inputLocation.getColumnNumber()); return inputLocation; } private final InputLocation locate(final InputLocationTracker = locationTracker, final List tags) { InputLocation inputLocation =3D null; for (final String tag : tags) { if (inputLocation =3D=3D null) { inputLocation =3D locationTracker.getLocation(tag); } else { // InputLocation is an InputLocationTracker final InputLocation tempInputLocation =3D = inputLocation.getLocation(tag); // (1)=20 if (tempInputLocation =3D=3D null) { LOGGER.error("InputLocation for tag <{}> is null, = returning position of last tag that has been found", tag); break; } else { inputLocation =3D tempInputLocation; } } } return inputLocation; } public static void main(final String[] args) throws Exception { final Model model =3D RawMavenModelFactory.instance() = .buildWith(FileUtils.toFile(JavaSourceVersionCheck.class.getClassLoader() .getResource("pom.xml"))); final JavaSourceVersionCheck check =3D new = JavaSourceVersionCheck(); check.execute(model); } } -[snip]=E2=80=94 Here is the code of the buildWith() method: -[snip]=E2=80=94 public Model buildWith(final File pomFile) throws = MavenModelBuildingException { final MavenXpp3ReaderEx reader =3D new MavenXpp3ReaderEx(); reader.setAddDefaultEntities(false); try { return reader.read(new FileInputStream(pomFile), true, new = InputSource()); } catch (IOException | XmlPullParserException e) { throw new MavenModelBuildingException("Could not build Maven = model.", e); } } -[snip]=E2=80=94 The dependencies I=E2=80=99m using here are the following: -[snip]=E2=80=94 3.3.3 1.0.2.v20150114 -[snip]=E2=80=94 And here is the sample pom.xml file that is used by the main method: -[snip]=E2=80=94 4.0.0 test test 1.0.0-SNAPSHOT = 3.3 org.apache.maven.plugins maven-compiler-plugin ${plugin.maven-compiler-plugin.version} 1.8 1.8 -[snip]=E2=80=94 When run the code, I get the following output: -[snip]=E2=80=94 15:21:35.822 [main] DEBUG JavaSourceVersionCheck - checking Model 15:21:35.826 [main] DEBUG JavaSourceVersionCheck - Maven compiler plugin = found! 15:21:35.830 [main] DEBUG JavaSourceVersionCheck - Configuration DOM: = 1.8 1.8 15:21:35.831 [main] DEBUG JavaSourceVersionCheck - Found tag, = checking its value 15:21:35.831 [main] ERROR JavaSourceVersionCheck - InputLocation for tag = is null, returning position of last tag that has been found 15:21:35.831 [main] DEBUG JavaSourceVersionCheck - tag = located at line 15, column 24) 15:21:35.831 [main] WARN JavaSourceVersionCheck - non compliant value: = is 1.8 but should be 1.7 (issue was found at 15 column 24) 15:21:35.832 [main] DEBUG JavaSourceVersionCheck - done -[snip]=E2=80=94 So I have two problems here: I cannot retrieve the line and column number for the tag, as = calling inputLocation.getLocation(tag); is null (see (1) in above source = code) getColumnNumber() returns =E2=80=9Cline 15 column 24=E2=80=9D which is = the last column of the line of the pom.xml where is = located. My questions are the following: Is it a bug or am I doing something wrong? (source is reachable from the = DOM, but when calling getLocation()) How can I retrieve the line and column of the opening tag? How can I retrieve the line and column of the closing tag? Why does getColumnNumber() always return the last column of the line? Thanks a lot in advance for your help and best regards! Michel --Apple-Mail=_B2C8D872-E102-4573-85E1-79CF1E5C089F--