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.source; 024 025 import org.apache.log4j.Logger; 026 import org.xml.sax.Attributes; 027 import org.xml.sax.ContentHandler; 028 import org.xml.sax.Locator; 029 import org.xml.sax.SAXException; 030 import org.xml.sax.ext.LexicalHandler; 031 032 final class LocationRecorder implements ContentHandler, LexicalHandler { 033 private static final Logger log4j = Logger.getLogger(LocationRecorder.class); 034 035 private final SourceCode owner; 036 037 private Locator locator; 038 039 private String uri; 040 041 /** 042 * @param owner 043 */ 044 LocationRecorder(final SourceCode owner) { 045 this.owner = owner; 046 } 047 048 /** 049 * 050 */ 051 private void addLocatorLocation() { 052 if (locator != null) { 053 String systemId = locator.getSystemId(); 054 log4j.debug(systemId); 055 if (uri == systemId || (uri != null && uri.equals(systemId))) { 056 owner.addLocatorLocation(locator.getLineNumber(), locator.getColumnNumber()); 057 } 058 } 059 } 060 061 public void characters(char[] arg0, int arg1, int arg2) throws SAXException { 062 addLocatorLocation(); 063 } 064 065 066 public void endDocument() throws SAXException { 067 locator = null; 068 } 069 070 public void endElement(String arg0, String arg1, String arg2) 071 throws SAXException { 072 addLocatorLocation(); 073 } 074 075 public void endPrefixMapping(String arg0) throws SAXException { 076 } 077 078 public void ignorableWhitespace(char[] arg0, int arg1, int arg2) 079 throws SAXException { 080 addLocatorLocation(); 081 } 082 083 public void processingInstruction(String arg0, String arg1) 084 throws SAXException { 085 addLocatorLocation(); 086 } 087 088 public void setDocumentLocator(Locator locator) { 089 this.locator = locator; 090 log4j.debug(locator); 091 } 092 093 public void skippedEntity(String arg0) throws SAXException { 094 addLocatorLocation(); 095 } 096 097 public void startDocument() throws SAXException { 098 uri = owner.getUri(); 099 } 100 101 public void startElement(String arg0, String arg1, String arg2, 102 Attributes arg3) throws SAXException { 103 addLocatorLocation(); 104 } 105 106 public void startPrefixMapping(String arg0, String arg1) 107 throws SAXException { 108 } 109 110 public void comment(char[] arg0, int arg1, int arg2) throws SAXException { 111 addLocatorLocation(); 112 } 113 114 public void endCDATA() throws SAXException { 115 addLocatorLocation(); 116 } 117 118 public void endDTD() throws SAXException { 119 addLocatorLocation(); 120 } 121 122 public void endEntity(String arg0) throws SAXException { 123 addLocatorLocation(); 124 } 125 126 public void startCDATA() throws SAXException { 127 addLocatorLocation(); 128 } 129 130 public void startDTD(String arg0, String arg1, String arg2) 131 throws SAXException { 132 addLocatorLocation(); 133 } 134 135 public void startEntity(String arg0) throws SAXException { 136 addLocatorLocation(); 137 } 138 139 }