⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.java

📁 云网论坛CWBBS 源码,内容丰富,学习,参考,教学的好资料,具体见内说明,
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            posPair.begin = lBracketPos;
            posPair.end = rBracketPos;

            posPairs.add(posPair);

            begin = rBracketPos + 1;
        }

        if (posPairs != null) {
            token.type = Token.hasVar;
            token.posPairs = posPairs;
        }
        return token;
    }

    public ITemplate parse(BufferedReader reader) throws IOException {
        LogUtil.getLog(getClass()).info("parse:" + reader);

        StringBuffer staticLines = new StringBuffer();
        Stack stack = new Stack(); // 用于ListPart的嵌套解析
        ListPart top = new ListPart(ListPart.TOP, ListPart.ROOT); // 根节点

        StaticPart staticPart = null;

        int lineNo = 0; // 记录行号
        String commentPart = null;

        String line = null;
        while ((line = reader.readLine()) != null) {
            lineNo++;

            // LogUtil.getLog(getClass()).info("parse:" + line);

            // 注释部分
            int commentBegin = line.indexOf("<!--");
            // 找到注释的起始部分
            if (commentBegin >= 0) {
                boolean isComment = false;
                String lineBefore = null;
                if (commentBegin > 0) {
                    lineBefore = line.substring(0, commentBegin);
                }

                staticLines.append(lineBefore + "\n");

                StringBuffer commentBuf = new StringBuffer();
                String commentLine = line;
                int commentEnd = line.indexOf("-->");
                // 该行不包含注释的结尾
                if (commentEnd < 0) {
                    commentBuf.append(line.substring(commentBegin));
                    commentBegin = 0;
                }

                // 找到注释的结尾
                while (commentEnd < 0) {
                    commentLine = reader.readLine();
                    lineNo++;
                    if (commentLine == null)
                        break; // 没找到结尾
                    commentEnd = commentLine.indexOf("-->");
                    // 找到,退出循环
                    if (commentEnd >= 0) {
                        isComment = true;
                        commentBuf.append(commentLine.substring(0, commentEnd + "-->".length()));
                        break;
                    }
                    commentBuf.append(commentLine + "\n");
                }

                LogUtil.getLog(getClass()).info("commentPart:" + commentBuf);

                staticLines.append(commentBuf);

                if (isComment) {
                    if (commentEnd < commentLine.length()-1) {
                        line = commentLine.substring(commentEnd + "-->".length()) + "\n";
                    }
                    else
                        continue;
                }
            }

            // parse a line
            Token token = parseLine(line);

            // LogUtil.getLog(getClass()).info("name=" + token.getName() +
            //                                " type=" + token.getType());

            // normal line, put it to static lines buffer
            if (token.getType() == Token.NONE) {
                staticLines.append(line + "\n");
                continue;
            }

            // line中含有变量

            if (staticLines.length() > 0) {
                staticPart = new StaticPart(staticLines.toString());
                top.addStep(staticPart);
                staticLines.setLength(0); // clean the static lines buf.
            }

            switch (token.getType()) {
            case Token.BEGIN:
                ListPart listPart = (ListPart) getListPartByName(line); // token.getName());
                LogUtil.getLog(getClass()).info("listPart:" + listPart);
                listPart.setName(token.getName());
                listPart.setParentName(top.getName());
                top.addStep(listPart);
                stack.push(top);
                top = listPart;
                break;
            case Token.END:
                if (!top.getName().equals(token.getName())) {
                    throw new IOException("line " + lineNo + ": End : " +
                                          top.getName() + " instead of " +
                                          token.getName() + " is expected.");
                }
                top = (ListPart) stack.pop();
                if (top == null) {
                    throw new IOException("line " + lineNo +
                                          ": End Dynamic: top = null, why?");
                }
                break;
            case Token.hasVar: // this line contains {..}
                List posPairs = token.posPairs;

                // ........{....{.........}....}.....{..........}....... etc
                //              ^         ^         ^          ^
                // StaticPart   VariablePart  Static   Variable   Static etc
                if (posPairs != null) {
                    // 判断是否为分页符所在的行
                    PaginatorPart pp = null;
                    if (line.indexOf("paginator") != -1) {
                        pp = new PaginatorPart();
                        pp.setParentName(top.getName());
                        pp.setRequest(request);
                        top.addStep(pp);
                    }

                    int nPairs = posPairs.size();

                    int begin = 0;
                    int end = line.length() - 1;

                    for (int k = 0; k < nPairs; k++) {
                        PosPair posPair = (PosPair) posPairs.get(k);

                        if (begin < posPair.begin) {
                            staticPart =
                                    new StaticPart(line.substring(begin,
                                    posPair.begin));
                            if (pp == null)
                                top.addStep(staticPart);
                            else
                                pp.addStep(staticPart);
                        }

                        String varStr = line.substring(posPair.begin + 1,
                                posPair.end);

                        if (varPat.matcher(varStr).find()) {
                            token.setType(Token.VAR);
                            VarPart varPart = getVarPartByNameString(varStr);
                            if (pp == null)
                                top.addStep(varPart);
                            else
                                pp.addStep(varPart);
                        } else if (fieldVarPat.matcher(line).find()) {
                            token.setType(Token.FIELD);

                            FieldPart fieldPart =
                                    new FieldPart(varStr);
                            // LogUtil.getLog(getClass()).info("fieldPart name=" +
                            //        fieldPart.getName());
                            if (pp == null)
                                top.addStep(fieldPart);
                            else
                                pp.addStep(fieldPart);
                        } else {
                            if (pp == null) {
                                top.addStep(new StaticPart(line.substring(
                                        posPair.
                                        begin,
                                        posPair.end + 1)));
                            } else {
                                pp.addStep(new StaticPart(line.substring(
                                        posPair.
                                        begin,
                                        posPair.end + 1)));
                            }
                        }
                        begin = posPair.end + 1;
                    }

                    String tail = "\n";
                    if (begin <= end) {
                        tail = line.substring(begin, end + 1) + "\n";
                    }
                    staticPart = new StaticPart(tail);
                    if (pp == null)
                        top.addStep(staticPart);
                    else
                        pp.addStep(staticPart);
                }
                break;
            }
        }

        if (stack.size() > 0) {
            ListPart left = (ListPart) stack.pop();
            throw new IOException("line " + lineNo + ": END:" +
                                  left.getName() +
                                  " is expected but not found.");
        }

        if (staticLines.length() > 0) {
            staticPart = new StaticPart(staticLines.toString());
            top.addStep(staticPart);
        }

        return top;
    }

    public static void main(String[] args) {
        Parser p = new Parser(null);
        String html =
                "bbbb<!-- begin:list.doc dirCode=first start=0 end=3-->a@abc aa<!--begin:doccc_list id=9-->";
        Matcher m = beginPat.matcher(html);

        /*
               html = "dd$doc.id(request.id).content xx";
               html = "$doc.id(1).content";

               if (varPat.matcher(html).find()) {
                   System.out.println("matched");
               }
               Matcher m = varPat.matcher(html);


                 Pattern bgeinPat = Pattern.compile(
                "<!--\\s*begin:(\\S+)\\s*(.*?)-->", Pattern.DOTALL |
                Pattern.CASE_INSENSITIVE);

                 Pattern fieldPat = Pattern.compile("@(\\S+)", Pattern.DOTALL |
                                           Pattern.CASE_INSENSITIVE);
                 Matcher m = fieldPat.matcher(html);
         */
        Pattern varNamePat2 = Pattern.compile(
                "\\@([^\\(\\.]+)(\\.([^\\(]+))?(\\((.*?)\\))?",
                Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
        String fieldString = "@paginator.total(len=2)";
        fieldString = "@total";
        m = varNamePat2.matcher(fieldString);

        boolean result = m.find();
        while (result) {
            // 有四个子串name title size value
            for (int i = 1; i <= m.groupCount(); i++) {
                System.out.println("第" + i + "组的子串内容为: " + m.group(i));
            }
            result = m.find();
        }

        // getVarPartByNameString("");
    }

    /**
     * 找出变量的位置
     * .............{.........}.........{..........}....... etc
     *              ^         ^         ^          ^
     *              begin     end       begin      end
     */
    static class PosPair {
        int begin = 0;
        int end = 0;
    };
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -