001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 /* 019 * $Id: SAXSourceLocator.java 468655 2006-10-28 07:12:06Z minchau $ 020 */ 021 package org.apache.xml.utils; 022 023 import java.io.Serializable; 024 025 import javax.xml.transform.SourceLocator; 026 027 import org.xml.sax.Locator; 028 import org.xml.sax.SAXParseException; 029 import org.xml.sax.helpers.LocatorImpl; 030 031 /** 032 * Class SAXSourceLocator extends org.xml.sax.helpers.LocatorImpl 033 * for the purpose of implementing the SourceLocator interface, 034 * and thus can be both a SourceLocator and a SAX Locator. 035 */ 036 public class SAXSourceLocator extends LocatorImpl 037 implements SourceLocator, Serializable 038 { 039 static final long serialVersionUID = 3181680946321164112L; 040 /** The SAX Locator object. 041 * @serial 042 */ 043 Locator m_locator; 044 045 /** 046 * Constructor SAXSourceLocator 047 * 048 */ 049 public SAXSourceLocator(){} 050 051 /** 052 * Constructor SAXSourceLocator 053 * 054 * 055 * @param locator Source locator 056 */ 057 public SAXSourceLocator(Locator locator) 058 { 059 m_locator = locator; 060 this.setColumnNumber(locator.getColumnNumber()); 061 this.setLineNumber(locator.getLineNumber()); 062 this.setPublicId(locator.getPublicId()); 063 this.setSystemId(locator.getSystemId()); 064 } 065 066 /** 067 * Constructor SAXSourceLocator 068 * 069 * 070 * @param locator Source locator 071 */ 072 public SAXSourceLocator(javax.xml.transform.SourceLocator locator) 073 { 074 m_locator = null; 075 this.setColumnNumber(locator.getColumnNumber()); 076 this.setLineNumber(locator.getLineNumber()); 077 this.setPublicId(locator.getPublicId()); 078 this.setSystemId(locator.getSystemId()); 079 } 080 081 082 /** 083 * Constructor SAXSourceLocator 084 * 085 * 086 * @param spe SAXParseException exception. 087 */ 088 public SAXSourceLocator(SAXParseException spe) 089 { 090 this.setLineNumber( spe.getLineNumber() ); 091 this.setColumnNumber( spe.getColumnNumber() ); 092 this.setPublicId( spe.getPublicId() ); 093 this.setSystemId( spe.getSystemId() ); 094 } 095 096 /** 097 * Return the public identifier for the current document event. 098 * 099 * <p>The return value is the public identifier of the document 100 * entity or of the external parsed entity in which the markup 101 * triggering the event appears.</p> 102 * 103 * @return A string containing the public identifier, or 104 * null if none is available. 105 * @see #getSystemId 106 */ 107 public String getPublicId() 108 { 109 return (null == m_locator) ? super.getPublicId() : m_locator.getPublicId(); 110 } 111 112 /** 113 * Return the system identifier for the current document event. 114 * 115 * <p>The return value is the system identifier of the document 116 * entity or of the external parsed entity in which the markup 117 * triggering the event appears.</p> 118 * 119 * <p>If the system identifier is a URL, the parser must resolve it 120 * fully before passing it to the application.</p> 121 * 122 * @return A string containing the system identifier, or null 123 * if none is available. 124 * @see #getPublicId 125 */ 126 public String getSystemId() 127 { 128 return (null == m_locator) ? super.getSystemId() : m_locator.getSystemId(); 129 } 130 131 /** 132 * Return the line number where the current document event ends. 133 * 134 * <p><strong>Warning:</strong> The return value from the method 135 * is intended only as an approximation for the sake of error 136 * reporting; it is not intended to provide sufficient information 137 * to edit the character content of the original XML document.</p> 138 * 139 * <p>The return value is an approximation of the line number 140 * in the document entity or external parsed entity where the 141 * markup triggering the event appears.</p> 142 * 143 * @return The line number, or -1 if none is available. 144 * @see #getColumnNumber 145 */ 146 public int getLineNumber() 147 { 148 return (null == m_locator) ? super.getLineNumber() : m_locator.getLineNumber(); 149 } 150 151 /** 152 * Return the column number where the current document event ends. 153 * 154 * <p><strong>Warning:</strong> The return value from the method 155 * is intended only as an approximation for the sake of error 156 * reporting; it is not intended to provide sufficient information 157 * to edit the character content of the original XML document.</p> 158 * 159 * <p>The return value is an approximation of the column number 160 * in the document entity or external parsed entity where the 161 * markup triggering the event appears.</p> 162 * 163 * @return The column number, or -1 if none is available. 164 * @see #getLineNumber 165 */ 166 public int getColumnNumber() 167 { 168 return (null == m_locator) ? super.getColumnNumber() : m_locator.getColumnNumber(); 169 } 170 }