Package httl.ast

Examples of httl.ast.Statement


  private BlockDirective reduce(List<Statement> directives) throws ParseException {
    LinkedStack<BlockDirectiveEntry> directiveStack = new LinkedStack<BlockDirectiveEntry>();
    RootDirective rootDirective = new RootDirective();
    directiveStack.push(new BlockDirectiveEntry(rootDirective));
    for (int i = 0, n = directives.size(); i < n; i ++) {
      Statement directive = (Statement)directives.get(i);
      if (directive == null)
        continue;
      Class<?> directiveClass = directive.getClass();
      // 弹栈
      if (directiveClass == EndDirective.class
          || directiveClass == ElseDirective.class) {
        if (directiveStack.isEmpty())
          throw new ParseException("Miss #end directive.", directive.getOffset());
        BlockDirective blockDirective = ((BlockDirectiveEntry) directiveStack.pop()).popDirective();
        if (blockDirective == rootDirective)
          throw new ParseException("Miss #end directive.", directive.getOffset());
        EndDirective endDirective;
        if (directiveClass == ElseDirective.class) {
          endDirective = new EndDirective(directive.getOffset());
        } else {
          endDirective = (EndDirective) directive;
        }
        blockDirective.setEnd(endDirective);
      }
      // 设置树
      if (directiveClass != EndDirective.class) { // 排除EndDirective
        if (directiveStack.isEmpty())
          throw new ParseException("Miss #end directive.", directive.getOffset());
        ((BlockDirectiveEntry) directiveStack.peek()).appendInnerDirective(directive);
      }
      // 压栈
      if (directive instanceof BlockDirective)
        directiveStack.push(new BlockDirectiveEntry((BlockDirective) directive));
View Full Code Here


   
    // 待移除的空的 Text Node (主要是 Text Node 不允许为空的内容,只能在循环外把它delete了)
    List<Integer> empty_text_index_list = new ArrayList<Integer>();

    for (int i = 0; i < nodes.size(); i++) {
      Statement node = nodes.get(i);

      if (isTrimableDirective(node)) {
        if (i > 0) {
          int prev_index = i - 1;
          Statement prev = nodes.get(prev_index);

          if (isNoLiteralText(prev)
              && !empty_text_index_list.contains(prev_index)) {
            // 删除上一个文本节点最后一个\n之后的所有空白符
            String text = ((Text) prev).getContent();
            int pos = text.lastIndexOf('\n');
            if (pos >= 0) {
              String tail = text.substring(pos + 1);
              if (tail.length() > 0 && tail.trim().length() == 0) {
                text = text.substring(0, pos + 1);
                if (text.length() == 0) {
                  empty_text_index_list.add(prev_index); // 将会是空节点,加入到待删除队列
                } else {
                  nodes.set(prev_index, new Text(text, false,
                      prev.getOffset())); // 修改
                }
              }
            }
          }
        } // prev text node

        if (i + 1 < nodes.size()) {
          int next_index = i + 1;
          Statement next = nodes.get(next_index);

          if (isNoLiteralText(next)) {
            // 删除下一个文本节点地一个\n之前的所有空白符
            String text = ((Text) next).getContent();
            int pos = text.indexOf('\n');
            if (pos >= 0) {
              String head = text.substring(0, pos);
              if (head.trim().length() == 0) {
                text = text.substring(pos + 1);
                boolean isEmptyNode = false;
                if (text.length() == 0) {
                  empty_text_index_list.add(next_index); // 将会是空节点,加入到待删除队列
                  isEmptyNode = true;
                } else if (text.indexOf('\n') == -1
                    && text.trim().length() == 0) {
                  // 看看下面是不是还是个指令,是不是可以全部丢掉
                  if (next_index + 1 < nodes.size()) {
                    Statement next_next = nodes
                        .get(next_index + 1);
                    if (isTrimableDirective(next_next)) {
                      empty_text_index_list
                          .add(next_index); // 将会是空节点,加入到待删除队列
                      isEmptyNode = true;
View Full Code Here

TOP

Related Classes of httl.ast.Statement

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.