001    /*
002     * Copyright (c) 2007 Mozilla Foundation
003     *
004     * Permission is hereby granted, free of charge, to any person obtaining a 
005     * copy of this software and associated documentation files (the "Software"), 
006     * to deal in the Software without restriction, including without limitation 
007     * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
008     * and/or sell copies of the Software, and to permit persons to whom the 
009     * Software is furnished to do so, subject to the following conditions:
010     *
011     * The above copyright notice and this permission notice shall be included in 
012     * all copies or substantial portions of the Software.
013     *
014     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
015     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
016     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
017     * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
018     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
019     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
020     * DEALINGS IN THE SOFTWARE.
021     */
022    
023    package nu.validator.messages;
024    
025    import java.util.SortedSet;
026    
027    import org.xml.sax.SAXException;
028    
029    import nu.validator.json.JsonHandler;
030    import nu.validator.source.SourceHandler;
031    
032    public class JsonExtractHandler implements SourceHandler {
033    
034        private static final char[] NEWLINE = {'\n'};
035        
036        private enum HiliteState {
037            BEFORE,
038            INSIDE,
039            AFTER
040        }
041        
042        private final JsonHandler handler;
043        
044        private int hiliteStart;
045        
046        private int hiliteLength;
047        
048        private HiliteState hiliteState;
049        
050        public JsonExtractHandler(JsonHandler handler) {
051            this.handler = handler;
052        }
053    
054        public void characters(char[] ch, int start, int length)
055                throws SAXException {
056            handler.characters(ch, start, length);
057            switch (hiliteState) {
058                case BEFORE:
059                    hiliteStart += length;
060                    break;
061                case INSIDE:
062                    hiliteLength += length;
063                case AFTER:
064                    break;
065            }
066        }
067    
068        public void endCharHilite() throws SAXException {
069            assert hiliteState == HiliteState.INSIDE;
070            hiliteState = HiliteState.AFTER;
071        }
072    
073        public void endRange() throws SAXException {
074            assert hiliteState == HiliteState.INSIDE;
075            hiliteState = HiliteState.AFTER;
076        }
077    
078        public void endSource() throws SAXException {
079            handler.endString();
080        }
081    
082        public void newLine() throws SAXException {
083            handler.characters(NEWLINE, 0, 1);
084            switch (hiliteState) {
085                case BEFORE:
086                    hiliteStart++;
087                    break;
088                case INSIDE:
089                    hiliteLength++;
090                case AFTER:
091                    break;
092            }
093        }
094    
095        public void startCharHilite(int oneBasedLine, int oneBasedColumn)
096                throws SAXException {
097            assert hiliteState == HiliteState.BEFORE;
098            hiliteState = HiliteState.INSIDE;
099        }
100    
101        public void startRange(int oneBasedLine, int oneBasedColumn)
102                throws SAXException {
103            assert hiliteState == HiliteState.BEFORE;
104            hiliteState = HiliteState.INSIDE;
105        }
106    
107        public void startSource(String type, String encoding) throws SAXException {
108            hiliteStart = 0;
109            hiliteLength = 0;
110            hiliteState = HiliteState.BEFORE;
111            handler.startString();
112        }
113    
114        /**
115         * Returns the hiliteLength.
116         * 
117         * @return the hiliteLength
118         */
119        public int getHiliteLength() {
120            return hiliteLength;
121        }
122    
123        /**
124         * Returns the hiliteStart.
125         * 
126         * @return the hiliteStart
127         */
128        public int getHiliteStart() {
129            return hiliteStart;
130        }
131    
132        public void setLineErrors(SortedSet<Integer> oneBasedLineErrors) throws SAXException {
133            
134        }
135    
136    }