Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2007-2008 Mozilla Foundation
   *
   * Permission is hereby granted, free of charge, to any person obtaining a 
   * copy of this software and associated documentation files (the "Software"), 
   * to deal in the Software without restriction, including without limitation 
   * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
   * and/or sell copies of the Software, and to permit persons to whom the 
   * Software is furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in 
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
  * DEALINGS IN THE SOFTWARE.
  */
 
 package nu.validator.source;
 
 import java.util.List;
 
 
 
 public final class SourceCode implements CharacterHandler {
     private static final Logger log4j = Logger.getLogger(SourceCode.class);
 
     private static Location[] SOURCE_LOCATION_ARRAY_TYPE = new Location[0];
 
     private String uri;
     
     private String type;
     
     private String encoding;
 
     private int expectedLength;
 
     private final SortedSet<LocationreverseSortedLocations = new HeadBiasedSortedSet<Location>(Collections.reverseOrder());
 
     private final SortedSet<LocationexactErrors = new TailBiasedSortedSet<Location>();
 
     private final SortedSet<LocationrangeLasts = new TailBiasedSortedSet<Location>();
     
     private final SortedSet<IntegeroneBasedLineErrors = new TailBiasedSortedSet<Integer>();
 
 //    private final SortedSet<Location> reverseSortedLocations = new TreeSet<Location>(Collections.reverseOrder());
 //
 //    private final SortedSet<Location> exactErrors = new TreeSet<Location>();
 //
 //    private final SortedSet<Location> rangeLasts = new TreeSet<Location>();
 //    
 //    private final SortedSet<Integer> oneBasedLineErrors = new TreeSet<Integer>();
 
     
     private final List<Linelines = new ArrayList<Line>();
 
     private Line currentLine = null;
 
     private boolean prevWasCr = false;
 
     private final LocationRecorder locationRecorder;
 
     public SourceCode() {
         this. = new LocationRecorder(this);
     }
 
     public void initialize(InputSource inputSource) {
         this. = inputSource.getSystemId();
         this. = inputSource.getEncoding();
         if (inputSource instanceof TypedInputSource) {
             TypedInputSource typedInputSource = (TypedInputSourceinputSource;
             int length = typedInputSource.getLength();
             if (length == -1) {
                  = 2048;
             } else {
                  = length;
             }
             this. = typedInputSource.getType();
         } else {
              = 2048;
             this. = null;
        }
    }

    
    public void characters(char[] chint startint length)
            throws SAXException {
        int s = start;
        int end = start + length;
        for (int i = starti < endi++) {
            char c = ch[i];
            switch (c) {
                case '\r':
                    if (s < i) {
                        .characters(chsi - s);
                    }
                    newLine();
                    s = i + 1;
                     = true;
                    break;
                case '\n':
                    if (!) {
                        if (s < i) {
                            .characters(chsi - s);
                        }
                        newLine();
                    }
                    s = i + 1;
                     = false;
                    break;
                default:
                     = false;
                    break;
            }
        }
        if (s < end) {
            .characters(chsend - s);
        }
    }
    private void newLine() {
        int offset;
        char[] buffer;
        if ( == null) {
            offset = 0;
            buffer = new char[];
        } else {
            offset = .getOffset() + .getBufferLength();
            buffer = .getBuffer();
        }
         = new Line(bufferoffset);
        .add();
    }
    public void end() throws SAXException {
        if ( != null && .getBufferLength() == 0) {
            // Theoretical impurity with line separators vs. terminators
            .remove(.size() - 1);
             = null;
        }
    }
    public void start() throws SAXException {
        .clear();
        .clear();
         = null;
        newLine();
         = false;
    }
    void addLocatorLocation(int oneBasedLineint oneBasedColumn) {
        .debug(oneBasedLine + ", " + oneBasedColumn);
        .add(new Location(thisoneBasedLine - 1,
                oneBasedColumn - 1));
    }
    public void exactError(Location locationSourceHandler extractHandler)
            throws SAXException {
        .add(location);
        Location start = location.step(-15);
        Location end = location.step(15);
        extractHandler.startSource();
        emitContent(startlocationextractHandler);
        extractHandler.startCharHilite(location.getLine() + 1,
                location.getColumn() + 1);
        emitCharacter(locationextractHandler);
        extractHandler.endCharHilite();
        location = location.next();
        emitContent(locationendextractHandler);
        extractHandler.endSource();
    }
    
    public void rememberExactError(Location location) {
        if (location.getColumn() < 0 || location.getLine() < 0) {
            return;
        }
        .add(location);
    }
    public void registerRandeEnd(Locator locator) {
        String systemId = locator.getSystemId();
        if ( == systemId || ( != null && .equals(systemId))) {
            .add(newLocatorLocation(locator.getLineNumber(), locator.getColumnNumber()));
        }
    }
    
    public void rangeEndError(Location rangeStartLocation rangeLast,
            SourceHandler extractHandlerthrows SAXException {
        .add(rangeLast);
        .add(rangeLast);
        Location endRange = rangeLast.next();
        Location start = rangeStart.step(-10);
        Location end = endRange.step(6);
        extractHandler.startSource();
        emitContent(startrangeStartextractHandler);
        extractHandler.startRange(rangeLast.getLine() + 1,
                rangeLast.getColumn() + 1);
        emitContent(rangeStartendRangeextractHandler);
        extractHandler.endRange();
        emitContent(endRangeendextractHandler);
        extractHandler.endSource();
    }

    

Parameters:
rangeLast
Returns:
    public Location rangeStartForRangeLast(Location rangeLast) {
        for (Location loc : ) {
            if (loc.compareTo(rangeLast) < 0) {
                return loc.next();
            }
        }
        return new Location(this, 0, 0);
    }
    @SuppressWarnings("boxing")
    public void lineError(int oneBasedLineSourceHandler extractHandler)
            throws SAXException {
        .add(oneBasedLine);
        Line line = .get(oneBasedLine - 1);
        extractHandler.startSource();
        extractHandler.characters(line.getBuffer(), line.getOffset(),
                line.getBufferLength());
        extractHandler.endSource();
    }
    public boolean isWithinKnownSource(Location location) {
        if (location.getLine() >= .size()) {
            return false;
        }
        Line line = .get(location.getLine());
        return line.getBufferLength() >= location.getColumn();
    }
    public boolean isWithinKnownSource(int oneBasedLine) {
        return !(oneBasedLine > .size());
    }
    Line getLine(int line) {
        return .get(line);
    }
    int getNumberOfLines() {
        return .size();
    }
    void emitCharacter(Location locationSourceHandler handler)
            throws SAXException {
        Line line = getLine(location.getLine());
        int col = location.getColumn();
        if (col == line.getBufferLength()) {
            handler.newLine();
        } else {
            handler.characters(line.getBuffer(), line.getOffset() + col, 1);
        }
    }

    
Emits content between from a location (inclusive) until a location (exclusive).

Parameters:
from
until
handler
Throws:
org.xml.sax.SAXException
    void emitContent(Location fromLocation untilSourceHandler handler)
            throws SAXException {
        if (from.compareTo(until) >= 0) {
            return;
        }
        int fromLine = from.getLine();
        int untilLine = until.getLine();
        Line line = getLine(fromLine);
        if (fromLine == untilLine) {
            handler.characters(line.getBuffer(), line.getOffset()
                    + from.getColumn(), until.getColumn() - from.getColumn());
        } else {
            // first line
            int length = line.getBufferLength() - from.getColumn();
            if (length > 0) {
                handler.characters(line.getBuffer(), line.getOffset()
                        + from.getColumn(), length);
            }
            if (fromLine + 1 != .size()) {
                handler.newLine();
            }
            // lines in between
            int wholeLine = fromLine + 1;
            while (wholeLine < untilLine) {
                line = getLine(wholeLine);
                handler.characters(line.getBuffer(), line.getOffset(),
                        line.getBufferLength());
                wholeLine++;
                if (wholeLine != .size()) {
                    handler.newLine();
                }
            }
            // last line
            int untilCol = until.getColumn();
            if (untilCol > 0) {
                line = getLine(untilLine);
                handler.characters(line.getBuffer(), line.getOffset(), untilCol);
            }
        }
    }
    public void emitSource(SourceHandler handlerthrows SAXException {
        List<Rangeranges = new LinkedList<Range>();
        int i = locations.length - 1;
        for (Location loc : ) {
            while (i >= 0 && locations[i].compareTo(loc) < 0) {
                i--;
            }
            Location start;
            if (i == locations.length - 1) {
                start = new Location(this, 0, 0);
            } else {
                start = locations[i + 1].next();                
            }
            Location end = loc.next();
            ranges.add(new Range(startendloc));
        }
        try {
            handler.startSource();
            handler.setLineErrors();
            Iterator<RangerangeIter = ranges.iterator();
            Iterator<LocationexactIter = .iterator();
            Location previousLocation = new Location(this, 0, 0);
            Location exact = null;
            Location rangeStart = null;
            Location rangeEnd = null;
            Location rangeLoc = null;
            if (exactIter.hasNext()) {
                exact = exactIter.next();
            }
            if (rangeIter.hasNext()) {
                Range r = rangeIter.next();
                rangeStart = r.getStart();
                rangeEnd = r.getEnd();
                rangeLoc = r.getLoc();
            }
            while (exact != null || rangeEnd != null) {
                if (exact != null
                        && (rangeStart == null || exact.compareTo(rangeStart) < 0)
                        && (rangeEnd == null || exact.compareTo(rangeEnd) < 0)) { // exact
                                                                                    // first?
                    emitContent(previousLocationexacthandler);
                    handler.startCharHilite(exact.getLine() + 1,
                            exact.getColumn() + 1);
                    emitCharacter(exacthandler);
                    handler.endCharHilite();
                    previousLocation = exact.next();
                    if (exactIter.hasNext()) {
                        exact = exactIter.next();
                    } else {
                        exact = null;
                    }
                } else if (rangeStart != null) { // range start first?
                    emitContent(previousLocationrangeStarthandler);
                    handler.startRange(rangeLoc.getLine() + 1,
                            rangeLoc.getColumn() + 1);
                    previousLocation = rangeStart;
                    rangeStart = null;
                } else { // range end first?
                    emitContent(previousLocationrangeEndhandler);
                    handler.endRange();
                    previousLocation = rangeEnd;
                    rangeEnd = null;
                    if (rangeIter.hasNext()) {
                        Range r = rangeIter.next();
                        rangeStart = r.getStart();
                        rangeEnd = r.getEnd();
                        rangeLoc = r.getLoc();
                    } else {
                        rangeEnd = null;
                    }
                }
            }
            emitContent(previousLocationnew Location(this.size(), 0),
                    handler);
        } finally {
            handler.endSource();
        }
    }

    
Returns the uri.

Returns:
the uri
    public String getUri() {
        return ;
    }

    
Returns the locationRecorder. The returned object is guaranteed to also implement LexicalHandler.

Returns:
the locationRecorder
        return ;
    }
    public Location newLocatorLocation(int oneBasedLineint oneBasedColumn) {
        return new Location(thisoneBasedLine - 1, oneBasedColumn - 1);
    }
New to GrepCode? Check out our FAQ X