Exemplo n.º 1
0
 @Test
 public void test6() {
   ElementSelector p = new ElementSelector("//a//b");
   assertEquals(2, p.size());
   assertEquals("a", p.get(0));
   assertEquals("b", p.get(1));
 }
Exemplo n.º 2
0
 @Test
 public void testSuffix() {
   ElementSelector p = new ElementSelector("a/");
   assertEquals(1, p.size());
   assertEquals("a", p.peekLast());
   assertEquals("a", p.get(0));
 }
Exemplo n.º 3
0
 @Test
 public void test4() {
   ElementSelector p = new ElementSelector("/a123/b1234/cvvsdf");
   assertEquals(3, p.size());
   assertEquals("a123", p.get(0));
   assertEquals("b1234", p.get(1));
   assertEquals("cvvsdf", p.get(2));
 }
Exemplo n.º 4
0
 @Test
 public void test2() {
   ElementSelector p = new ElementSelector("a/b");
   assertEquals(2, p.size());
   assertEquals("b", p.peekLast());
   assertEquals("a", p.get(0));
   assertEquals("b", p.get(1));
 }
  /** 解析<code>TypeFilter</code>s。 */
  private void parseTypeFilters(
      Element element,
      ClassLoader classLoader,
      List<TypeFilter> includes,
      List<TypeFilter> excludes) {
    ElementSelector includeSelector = and(sameNs(element), name("include-filter"));
    ElementSelector excludeSelector = and(sameNs(element), name("exclude-filter"));

    for (Element subElement : subElements(element)) {
      if (includeSelector.accept(subElement)) {
        TypeFilter filter = createTypeFilter(subElement, classLoader);

        if (filter != null) {
          includes.add(filter);
        }
      } else if (excludeSelector.accept(subElement)) {
        TypeFilter filter = createTypeFilter(subElement, classLoader);

        if (filter != null) {
          excludes.add(filter);
        }
      }
    }
  }
  @Override
  protected void doParse(
      Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    Map<String, ParsingModuleInfo> classes =
        parseSpecificBeans(
            element, parserContext, builder.getRawBeanDefinition(), and(beansNs(), name("bean")));

    ElementSelector searchPackages = and(sameNs(element), name("search-packages"));
    ElementSelector searchClasses = and(sameNs(element), name("search-classes"));
    ModuleDefinitionScanner scanner = getScanner(parserContext);

    for (Element subElement : subElements(element)) {
      Pattern classNamePattern = null;
      String typeName = null;
      String moduleName = null;
      String classResourceName = null;

      if (searchPackages.accept(subElement)) {
        String packageName =
            assertNotNull(
                normalizeClassName(subElement.getAttribute("packages")),
                "no package name provided for search-packages");

        classNamePattern = compileClassName(packageName, MATCH_PREFIX);
        typeName =
            assertNotNull(trimToNull(subElement.getAttribute("type")), "no type name provided");
        classResourceName = classNameToPathName(packageName) + "/**/*.class";

        log.trace("Searching in packages: {}, moduleType={}", packageName, typeName);
      } else if (searchClasses.accept(subElement)) {
        String className =
            assertNotNull(
                normalizeClassName(subElement.getAttribute("classes")),
                "no class name provided for search-classes");

        classNamePattern = compileClassName(className, MATCH_PREFIX);
        typeName =
            assertNotNull(trimToNull(subElement.getAttribute("type")), "no type name provided");
        moduleName =
            assertNotNull(trimToNull(subElement.getAttribute("name")), "no module name provided");
        classResourceName = classNameToPathName(className);

        if (classResourceName.endsWith("**")) {
          classResourceName += "/*.class";
        } else {
          classResourceName += ".class";
        }

        log.trace(
            "Searching for classes: {}, moduleType={}, moduleName={}",
            new Object[] {className, typeName, moduleName});
      }

      boolean includeAbstractClasses =
          "true".equalsIgnoreCase(trimToNull(subElement.getAttribute("includeAbstractClasses")));

      if (classResourceName != null) {
        // 处理所有的include/exclude filters
        ClassLoader classLoader = scanner.getResourceLoader().getClassLoader();
        List<TypeFilter> includes = createLinkedList();
        List<TypeFilter> excludes = createLinkedList();

        parseTypeFilters(subElement, classLoader, includes, excludes);

        ModuleTypeFilter filter =
            new ModuleTypeFilter(classes, classNamePattern, typeName, moduleName, includes);

        // 事实上,只有一个顶级的include filter,其它的include filter被这一个moduleTypeFilter所调用。
        scanner.addIncludeFilter(filter);

        // Exclude filter比较简单,直接加到scanner中即可。
        for (TypeFilter exclude : excludes) {
          scanner.addExcludeFilter(exclude);
        }

        scanner.setBeanNameGenerator(filter);
        scanner.setResourcePattern(classResourceName.replace('?', '*'));
        scanner.setIncludeAbstractClasses(includeAbstractClasses);
        scanner.setBeanDefinitionDefaults(getBeanDefinitionDefaults(subElement, parserContext));

        int found = scanner.scan("");

        log.debug("Found {} module classes with pattern: {}", found, classResourceName);
      }
    }

    postProcessItems(element, parserContext, builder, classes, "search-packages or search-classes");
  }
Exemplo n.º 7
0
  // test tail matching
  @Test
  public void testTailMatch() {
    {
      ElementPath p = new ElementPath("/a/b");
      ElementSelector ruleElementSelector = new ElementSelector("*");
      assertEquals(0, ruleElementSelector.getTailMatchLength(p));
    }

    {
      ElementPath p = new ElementPath("/a");
      ElementSelector ruleElementSelector = new ElementSelector("*/a");
      assertEquals(1, ruleElementSelector.getTailMatchLength(p));
    }

    {
      ElementPath p = new ElementPath("/A");
      ElementSelector ruleElementSelector = new ElementSelector("*/a");
      assertEquals(1, ruleElementSelector.getTailMatchLength(p));
    }

    {
      ElementPath p = new ElementPath("/a");
      ElementSelector ruleElementSelector = new ElementSelector("*/A");
      assertEquals(1, ruleElementSelector.getTailMatchLength(p));
    }

    {
      ElementPath p = new ElementPath("/a/b");
      ElementSelector ruleElementSelector = new ElementSelector("*/b");
      assertEquals(1, ruleElementSelector.getTailMatchLength(p));
    }

    {
      ElementPath p = new ElementPath("/a/B");
      ElementSelector ruleElementSelector = new ElementSelector("*/b");
      assertEquals(1, ruleElementSelector.getTailMatchLength(p));
    }

    {
      ElementPath p = new ElementPath("/a/b/c");
      ElementSelector ruleElementSelector = new ElementSelector("*/b/c");
      assertEquals(2, ruleElementSelector.getTailMatchLength(p));
    }
  }
Exemplo n.º 8
0
 @Test
 public void test5() {
   ElementSelector p = new ElementSelector("//a");
   assertEquals(1, p.size());
   assertEquals("a", p.get(0));
 }
Exemplo n.º 9
0
  // test prefix matching
  @Test
  public void testPrefixMatch() {
    {
      ElementPath p = new ElementPath("/a/b");
      ElementSelector ruleElementSelector = new ElementSelector("/x/*");
      assertEquals(0, ruleElementSelector.getPrefixMatchLength(p));
    }

    {
      ElementPath p = new ElementPath("/a");
      ElementSelector ruleElementSelector = new ElementSelector("/x/*");
      assertEquals(0, ruleElementSelector.getPrefixMatchLength(p));
    }

    {
      ElementPath p = new ElementPath("/a/b");
      ElementSelector ruleElementSelector = new ElementSelector("/a/*");
      assertEquals(1, ruleElementSelector.getPrefixMatchLength(p));
    }

    {
      ElementPath p = new ElementPath("/a/b");
      ElementSelector ruleElementSelector = new ElementSelector("/A/*");
      assertEquals(1, ruleElementSelector.getPrefixMatchLength(p));
    }

    {
      ElementPath p = new ElementPath("/A/b");
      ElementSelector ruleElementSelector = new ElementSelector("/a/*");
      assertEquals(1, ruleElementSelector.getPrefixMatchLength(p));
    }

    {
      ElementPath p = new ElementPath("/a/b");
      ElementSelector ruleElementSelector = new ElementSelector("/a/b/*");
      assertEquals(2, ruleElementSelector.getPrefixMatchLength(p));
    }

    {
      ElementPath p = new ElementPath("/a/b");
      ElementSelector ruleElementSelector = new ElementSelector("/*");
      assertEquals(0, ruleElementSelector.getPrefixMatchLength(p));
    }
  }