派生自 projectDept/qhighschool

yn147
2022-11-24 138ccc9fe4798e5335c49a6407e8cdc70e1ff122
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.qxueyou.scc.exercise.service.impl.parser;
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import com.qxueyou.scc.exercise.service.impl.Node;
import com.qxueyou.scc.exercise.service.impl.ParseResult;
import com.qxueyou.scc.exercise.service.impl.Parser;
import com.qxueyou.scc.exercise.service.impl.node.Item;
import com.qxueyou.scc.exercise.service.impl.node.Option;
 
 
 
public class OptionParser extends Parser {
    
    /**
     * Ï°ÌâÑ¡Ïî±àºÅģʽ[´øÀ¨ºÅ]
     */
    //Pattern optNoWithBrackets = Pattern.compile("\\(?[ABCDEFabcdef]\\)?[¡¢.£®]");
    
    /**
     * Ï°ÌâÑ¡Ïî  word×Ô´øµÄ¸ñʽµÄ a.a¡¢a)    ÕâÈýÖÖ½ÏΪÆÕ±é£¬Ä¿Ç°03¿ÉÒÔ½âÎö³É¹¦£¬07½âÎöʧ°Ü*[\t]
     */
    Pattern optNoWithBrackets = Pattern.compile("\\(?[ABCDEFGHIJKLMNOabcdefghijklmno]\\)?[.£®¡¢]");
    
    /**
     * Ï°ÌâÑ¡Ïî±àºÅģʽ
     */
    Pattern optNo = Pattern.compile("[ABCDEFGHIJKLMNOabcdefghijklmno]");
    
 
    @Override
    public ParseResult parse(Node node, String str) {
        return parse((Option)node,str);
    }
    
    /**
     * ½âÎö Item
     * @param item
     * @param str
     * @return
     */
    private ParseResult parse(Option option, String str) {
        
        //ϰÌâÑ¡Ïî
        Matcher optNoWithBracketsMatch = optNoWithBrackets.matcher(str);
        
        //³¢ÊÔµ÷ÓÃÉϼ¶½âÎöÆ÷¼ÌÐø½âÎö
        if(!matchBegin(optNoWithBracketsMatch)){
            return new ParseResult(false,ParseResult.STEP_PRE,null);
        }
        
        Matcher optNoMatch = null;
        
        int start = 0;
        Option preOpt = null;
        
        optNoWithBracketsMatch.reset();
        while(optNoWithBracketsMatch.find()){
            
            String noWithBrackets = optNoWithBracketsMatch.group();
            optNoMatch = optNo.matcher(noWithBrackets);
            optNoMatch.find();
            String no = optNoMatch.group();
 
            Option opt = getOpt(no.toUpperCase(),(Item)option.getParent());
            
            if(start!=optNoWithBracketsMatch.start() && preOpt!=null){
                preOpt.setContent(
                        beautyContent(
                                str.substring(start, optNoWithBracketsMatch.start())
                                )
                        );    
            }
            preOpt = opt;
            start = optNoWithBracketsMatch.end();
            
            // ÔÚ×ʼƥÅä³É¹¦¼´Ìø³öÑ­»·
            //break ;
        }
        
        preOpt.setContent(beautyContent(str.substring(start, str.length())));
        
        return new ParseResult(true,ParseResult.STEP_CUR,null);
        
    }
    
    /**
     * È¥³ýÎı¾ÖжàÓà×Ö·û
     * @param content
     * @return
     */
    private String beautyContent(String str){
        String prefix = ".¡£~¡¢¡¢£®£®)";
        String content = str;
        while(content.length()>0 && prefix.contains(content.substring(0, 1))){
            content = content.substring(1);
        }
        return content;
    }
    
    /**
     * »ñÈ¡ÌâĿѡÏî
     * @param no
     * @param item
     * @return
     */
    private Option getOpt(String no,Item item){
        Option opt = item.getOption(no);
        if(opt==null){
            
            opt = item.getOption("");
            
            if(opt==null){
                opt = new Option();
                item.addNode(opt);
                opt.setNo(no);
            }
        }
        return opt;
    }
}