001    /*
002     * To change this template, choose Tools | Templates
003     * and open the template in the editor.
004     */
005    
006    package org.util.xml.parse;
007    
008    import java.io.IOException;
009    import org.util.xml.element.TextElement;
010    
011    /**
012     *   
013     * <div>
014     * &lt;? ... ?&gt;<br/>
015     * e.x.<br/>
016     * &lt;?xml version="1.0" encoding="UTF-8"?&gt;<br/>
017     * </div>
018     * @author masaru
019     */
020    public class CommentInnerTagElementParser extends ParseElement {
021        
022        private int cc,ccc;
023        private String return_value_;
024        private TextElement text_element_;
025        
026        public CommentInnerTagElementParser() {
027            
028        }
029    
030        @Override
031        public boolean match(char c) {
032            return c=='!';
033        }
034    
035        @Override
036            public int parse(int c, ElementParser parser) throws XMLParseException, IOException {
037            int type = -1;
038            StringBuffer sb = new StringBuffer();
039            if(c!='!') throw new XMLParseException("parse error: cannot read comment tag");
040            c=parser.get();
041            cc=-1;
042            ccc=-1;
043            for(int state = 0;;) {
044                
045                if(state == 0) {
046                    sb.append((char)c);
047                    if(c == '-') state = 3;
048                    else if(c == '[') state = 2;
049                    else state = 1;
050                } else if(state == 1) {
051                    if(c=='[') {
052                        state = 6;
053                        sb.append((char)c);
054                    } else if(c=='>') {
055                        type = 0;
056                        break;
057                    } else {
058                        sb.append((char)c);
059                    }
060                } else if(state == 2) {
061                    sb.append((char)c);
062                    String cdata = "CDATA[";
063                    boolean match = true;
064                    for(int i=0; i<cdata.length()&&match;i++) {
065                        if(i!=0)
066                            c = parser.get();
067                        sb.append((char)c);
068                        match = (cdata.charAt(i)==c);
069                    }
070                    if(match) {
071                        sb = new StringBuffer();
072                        state = 5;
073                    } else state = 1;
074                } else if(state == 3) {
075                    if(c=='-') {
076                        sb = new StringBuffer();
077                        state = 4;
078                    } else {
079                        sb.append((char)c);
080                        state = 1;
081                    }
082                } else if(state == 4) {
083                    if(ccc=='-' && cc=='-' && c=='>') {
084                        type = 1;
085                        break;
086                    } else if(ccc != -1) sb.append((char)ccc);
087                    ccc = cc;
088                    cc = c;
089                } else if(state == 5) {
090                    if(ccc==']' && cc==']' && c=='>') {
091                        type = 2;
092                        break;
093                    } else if(ccc != -1) sb.append((char)ccc);
094                    ccc = cc;
095                    cc = c;
096                } else if(state == 6) {
097                    if(c==']') {
098                        sb.append((char)c);
099                        state = 1;
100                    } else
101                        sb.append((char)c);
102                }
103                c=parser.getChar();
104            }
105            return_value_ = sb.toString();
106            text_element_ = new TextElement(return_value_);
107            if(type==0) text_element_.setTag(true);
108            else if(type==1) text_element_.setCommenTag(true);
109            else if(type==2) text_element_.setCDATATag(true);
110            
111            return parser.get();
112        }
113    
114        public TextElement getResult() {
115            return text_element_;
116        }
117        
118        @Override
119        public String getReturnValue() {
120            return return_value_;
121        }
122    }