Home | History | Annotate | Download | only in utils
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one
      3  * or more contributor license agreements. See the NOTICE file
      4  * distributed with this work for additional information
      5  * regarding copyright ownership. The ASF licenses this file
      6  * to you under the Apache License, Version 2.0 (the  "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 /*
     19  * $Id: SAXSourceLocator.java 468655 2006-10-28 07:12:06Z minchau $
     20  */
     21 package org.apache.xml.utils;
     22 
     23 import java.io.Serializable;
     24 
     25 import javax.xml.transform.SourceLocator;
     26 
     27 import org.xml.sax.Locator;
     28 import org.xml.sax.SAXParseException;
     29 import org.xml.sax.helpers.LocatorImpl;
     30 
     31 /**
     32  * Class SAXSourceLocator extends org.xml.sax.helpers.LocatorImpl
     33  * for the purpose of implementing the SourceLocator interface,
     34  * and thus can be both a SourceLocator and a SAX Locator.
     35  */
     36 public class SAXSourceLocator extends LocatorImpl
     37         implements SourceLocator, Serializable
     38 {
     39     static final long serialVersionUID = 3181680946321164112L;
     40   /** The SAX Locator object.
     41    *  @serial
     42    */
     43   Locator m_locator;
     44 
     45   /**
     46    * Constructor SAXSourceLocator
     47    *
     48    */
     49   public SAXSourceLocator(){}
     50 
     51   /**
     52    * Constructor SAXSourceLocator
     53    *
     54    *
     55    * @param locator Source locator
     56    */
     57   public SAXSourceLocator(Locator locator)
     58   {
     59     m_locator = locator;
     60     this.setColumnNumber(locator.getColumnNumber());
     61     this.setLineNumber(locator.getLineNumber());
     62     this.setPublicId(locator.getPublicId());
     63     this.setSystemId(locator.getSystemId());
     64   }
     65 
     66   /**
     67    * Constructor SAXSourceLocator
     68    *
     69    *
     70    * @param locator Source locator
     71    */
     72   public SAXSourceLocator(javax.xml.transform.SourceLocator locator)
     73   {
     74     m_locator = null;
     75     this.setColumnNumber(locator.getColumnNumber());
     76     this.setLineNumber(locator.getLineNumber());
     77     this.setPublicId(locator.getPublicId());
     78     this.setSystemId(locator.getSystemId());
     79   }
     80 
     81 
     82   /**
     83    * Constructor SAXSourceLocator
     84    *
     85    *
     86    * @param spe SAXParseException exception.
     87    */
     88   public SAXSourceLocator(SAXParseException spe)
     89   {
     90     this.setLineNumber( spe.getLineNumber() );
     91     this.setColumnNumber( spe.getColumnNumber() );
     92     this.setPublicId( spe.getPublicId() );
     93     this.setSystemId( spe.getSystemId() );
     94   }
     95 
     96   /**
     97    * Return the public identifier for the current document event.
     98    *
     99    * <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 }
    171