/** Main method to check file syntax. */
  public static void main(String[] args) {
    // String filename =
    // "E:/programs/jakarta-tomcat/webapps/wtiles-struts/WEB-INF/tiles-examples-defs.xml";
    String filename =
        "E:/programs/jakarta-tomcat-4.0.3/webapps/wtiles-struts/WEB-INF/tiles-examples-defs.xml";
    // String filename =
    // "E:/programs/jakarta-tomcat/webapps/wtiles-struts/WEB-INF/tilesDefinitions.xml";
    // String filename =
    // "E:/programs/jakarta-tomcat/webapps/wtiles-channel/WEB-INF/componentDefinitions.xml";
    // String filename2 =
    // "E:/programs/jakarta-tomcat/webapps/wtiles-tutorial/WEB-INF/componentDefinitions.xml";

    if (args.length > 1) {
      filename = args[1];
    } // end if

    System.out.println("Read file '" + filename + "'");

    InputStream input = null;
    // InputStream input2 = null;
    // Open file
    try {
      input = new BufferedInputStream(new FileInputStream(filename));
      //    input2 = new BufferedInputStream(
      //                   new FileInputStream( filename2) );
    } catch (IOException ex) {
      System.out.println("can't open file '" + filename + "' : " + ex.getMessage());
    }
    // Check file syntax
    try {
      XmlParser parser = new XmlParser();
      parser.setValidating(true);
      XmlDefinitionsSet definitions = new XmlDefinitionsSet();
      System.out.println("  Parse file");
      parser.parse(input, definitions);
      //  System.out.println( "  Check file 2" );
      // parser.parse( input2, definitions);
      System.out.println("  done.");
      System.out.println("  Result : " + definitions.toString());
    } catch (Exception ex) {
      System.out.println("Error during parsing '" + filename + "' : " + ex.getMessage());
      ex.printStackTrace();
    }
  }
Exemplo n.º 2
0
  /**
   * Resolve inheritance. First, resolve parent's inheritance, then set path to the parent's path.
   * Also copy attributes setted in parent, and not set in child If instance doesn't extend
   * anything, do nothing.
   *
   * @throws NoSuchDefinitionException If an inheritance can not be solved.
   */
  public void resolveInheritance(XmlDefinitionsSet definitionsSet)
      throws NoSuchDefinitionException {
    // Already done, or not needed ?
    if (isVisited || !isExtending()) return;

    if (log.isDebugEnabled())
      log.debug(
          "Resolve definition for child name='" + getName() + "' extends='" + getExtends() + "'.");

    // Set as visited to avoid endless recurisvity.
    setIsVisited(true);

    // Resolve parent before itself.
    XmlDefinition parent = definitionsSet.getDefinition(getExtends());
    if (parent == null) { // error
      String msg =
          "Error while resolving definition inheritance: child '"
              + getName()
              + "' can't find its ancestor '"
              + getExtends()
              + "'. Please check your description file.";
      log.error(msg);
      // to do : find better exception
      throw new NoSuchDefinitionException(msg);
    }

    parent.resolveInheritance(definitionsSet);

    // Iterate on each parent's attribute and add it if not defined in child.
    Iterator parentAttributes = parent.getAttributes().keySet().iterator();
    while (parentAttributes.hasNext()) {
      String name = (String) parentAttributes.next();
      if (!getAttributes().containsKey(name)) putAttribute(name, parent.getAttribute(name));
    }
    // Set path and role if not setted
    if (path == null) setPath(parent.getPath());
    if (role == null) setRole(parent.getRole());
    if (controller == null) {
      setController(parent.getController());
      setControllerType(parent.getControllerType());
    }
  }