parser.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 521 行 · 第 1/2 页

JAVA
521
字号
            if (posPairs == null) {                posPairs = new ArrayList();            }            PosPair posPair = new PosPair();            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,ErrMsgException {                StringBuffer staticLines = new StringBuffer();        Stack stack = new Stack();         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++;                                    int commentBegin = line.indexOf("<!--");                        if (commentBegin >= 0) {                boolean isComment = false;                if (commentBegin > 0) {                    String lineBefore = null;                    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) + "\n");                    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");                }                                staticLines.append(commentBuf);                if (isComment) {                    if (commentEnd < commentLine.length()-1) {                        line = commentLine.substring(commentEnd + "-->".length()) + "\n";                    }                    else                        continue;                }            }                        Token token = parseLine(line);                                                if (token.getType() == Token.NONE) {                staticLines.append(line + "\n");                continue;            }                        if (staticLines.length() > 0) {                staticPart = new StaticPart(staticLines.toString());                top.addStep(staticPart);                staticLines.setLength(0);             }            switch (token.getType()) {            case Token.BEGIN:                ListPart listPart = (ListPart) getListPartByName(line);                                 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:                 List posPairs = token.posPairs;                                                                if (posPairs != null) {                                        PaginatorPart pp = null;                    if (line.indexOf("paginator") != -1) {                        pp = new PaginatorPart();                        pp.setParentName(top.getName());                        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);                                                                                    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();        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);                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) {                        for (int i = 1; i <= m.groupCount(); i++) {                System.out.println("第" + i + "组的子串内容为: " + m.group(i));            }            result = m.find();        }            }        static class PosPair {        int begin = 0;        int end = 0;    };}

⌨️ 快捷键说明

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