Exemplo n.º 1
0
 @Override
 public void visitToken(final DetailAST ast) {
   if (ast.getType() == TokenTypes.PACKAGE_DEF) {
     final DetailAST nameAST = ast.getLastChild().getPreviousSibling();
     final FullIdent full = FullIdent.createFullIdent(nameAST);
     if (root == null) {
       log(nameAST, MSG_MISSING_FILE);
     } else {
       inPkg = full.getText();
       currentLeaf = root.locateFinest(inPkg);
       if (currentLeaf == null) {
         log(nameAST, MSG_UNKNOWN_PKG);
       }
     }
   } else if (currentLeaf != null) {
     final FullIdent imp;
     if (ast.getType() == TokenTypes.IMPORT) {
       imp = FullIdent.createFullIdentBelow(ast);
     } else {
       // know it is a static import
       imp = FullIdent.createFullIdent(ast.getFirstChild().getNextSibling());
     }
     final AccessResult access = currentLeaf.checkAccess(imp.getText(), inPkg);
     if (access != AccessResult.ALLOWED) {
       log(ast, MSG_DISALLOWED, imp.getText());
     }
   }
 }
  @Override
  public void startElement(
      final String namespaceUri,
      final String localName,
      final String qName,
      final Attributes attributes)
      throws SAXException {
    if ("import-control".equals(qName)) {
      final String pkg = safeGet(attributes, PKG_ATTRIBUTE_NAME);
      stack.push(new PkgControl(pkg));
    } else if (SUBPACKAGE_ELEMENT_NAME.equals(qName)) {
      final String name = safeGet(attributes, "name");
      stack.push(new PkgControl(stack.peek(), name));
    } else if (ALLOW_ELEMENT_NAME.equals(qName) || "disallow".equals(qName)) {
      // Need to handle either "pkg" or "class" attribute.
      // May have "exact-match" for "pkg"
      // May have "local-only"
      final boolean isAllow = ALLOW_ELEMENT_NAME.equals(qName);
      final boolean isLocalOnly = attributes.getValue("local-only") != null;
      final String pkg = attributes.getValue(PKG_ATTRIBUTE_NAME);
      final boolean regex = attributes.getValue("regex") != null;
      final Guard guard;
      if (pkg == null) {
        // handle class names which can be normal class names or regular
        // expressions
        final String clazz = safeGet(attributes, "class");
        guard = new Guard(isAllow, isLocalOnly, clazz, regex);
      } else {
        final boolean exactMatch = attributes.getValue("exact-match") != null;
        guard = new Guard(isAllow, isLocalOnly, pkg, exactMatch, regex);
      }

      final PkgControl pkgControl = stack.peek();
      pkgControl.addGuard(guard);
    }
  }