JspAnalyzerPlugin implements AnalyzerPlugin { protected void addApiUses(Analyzer analyzer, String originalContent) { String content = _removeComments(originalContent); int contentX = -1; int contentY = content.length(); while (true) { contentX = content.lastIndexOf("<%@", contentY); if (contentX == -1) { break; } contentY = contentX; int importX = content.indexOf("import=\"", contentY); int importY = -1; if (importX != -1) { importX = importX + "import=\"".length(); importY = content.indexOf("\"", importX); } if ((importX != -1) && (importY != -1)) { String contentFragment = content.substring(importX, importY); String[] packageFragments = contentFragment.split("\\s*,\\s*"); for (String packageFragment : packageFragments) { int index = packageFragment.lastIndexOf('.'); Matcher matcher = _staticImportPattern.matcher( packageFragment); if (matcher.matches()) { packageFragment = matcher.group("package"); packageFragment = packageFragment.substring( 0, packageFragment.length() - 1); index = packageFragment.length(); } if (index != -1) { Packages packages = analyzer.getReferred(); String packageName = packageFragment.substring( 0, index); Descriptors.PackageRef packageRef = analyzer.getPackageRef(packageName); packages.put(packageRef, new Attrs()); addApiUses(analyzer, packageFragment, packageRef); } } } contentY -= 3; } }  @Override boolean analyzeJar(Analyzer analyzer);  }
@Test public void testImportsWithMultiplesAndStatics() throws Exception { JspAnalyzerPlugin jspAnalyzerPlugin = new JspAnalyzerPlugin(); URL url = getResource( "dependencies/imports_without_multipackages_and_statics.jsp"); InputStream inputStream = url.openStream(); String content = IO.collect(inputStream); Builder builder = new Builder(); builder.build(); jspAnalyzerPlugin.addApiUses(builder, content); Packages referredPackages = builder.getReferred(); Assert.assertTrue(referredPackages.containsFQN("java.io")); Assert.assertTrue(referredPackages.containsFQN("java.util")); Assert.assertTrue(referredPackages.containsFQN("java.util.logging")); Assert.assertTrue(referredPackages.containsFQN("javax.portlet")); Assert.assertTrue(referredPackages.containsFQN("javax.portlet.filter")); Assert.assertTrue( referredPackages.containsFQN("javax.portlet.tck.beans")); Assert.assertTrue( referredPackages.containsFQN("javax.portlet.tck.constants")); Assert.assertTrue(referredPackages.containsFQN("javax.servlet")); Assert.assertTrue(referredPackages.containsFQN("javax.servlet.http")); }
@Test public void testPageImportsWithComments() throws Exception { JspAnalyzerPlugin jspAnalyzerPlugin = new JspAnalyzerPlugin(); URL url = getResource("dependencies/page_imports_with_comments.jsp"); InputStream inputStream = url.openStream(); String content = IO.collect(inputStream); Builder builder = new Builder(); builder.build(); jspAnalyzerPlugin.addApiUses(builder, content); Packages referredPackages = builder.getReferred(); Assert.assertTrue(referredPackages.containsFQN("java.io")); Assert.assertFalse(referredPackages.containsFQN("javax.portlet")); Assert.assertFalse( referredPackages.containsFQN("javax.portlet.filter")); Assert.assertFalse( referredPackages.containsFQN("javax.portlet.tck.beans")); Assert.assertTrue( referredPackages.containsFQN("javax.portlet.tck.constants")); Assert.assertFalse(referredPackages.containsFQN("javax.servlet")); Assert.assertFalse(referredPackages.containsFQN("javax.servlet.http")); }