KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > org > xml > sax > Locator

org.xml.sax
Interface Locator

All Known Subinterfaces:
Locator2
All Known Implementing Classes:
Locator2Impl, LocatorImpl
See Also:
Top Examples, Source Code, startDocument, ContentHandler.setDocumentLocator(org.xml.sax.Locator)

int getColumnNumber()
See Also:
getLineNumber()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


int getLineNumber()
See Also:
getColumnNumber()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1579]Creates a validating SAX parser
By Anonymous on 2005/11/04 20:23:08  Rate
import javax.xml.parsers.*; 
 import org.xml.sax.*; 
 import org.xml.sax.helpers.*; 
  
  
 /** 
 A simple application class that creates a validating SAX parser and 
 validates a document whose name is passed on the command line. 
 */
 
  
  
 public class SAXValidator 
     extends DefaultHandler 
  {  
     /** 
     Report the warning to the console. 
     */
 
     public void warning  ( SAXParseException ex )  
         throws SAXException 
      {  
         System.out.println  
              ( "Warning at Line " + locator.getLineNumber  (  )  + ":" ) ; 
         System.out.println  ( " " + ex.getMessage  (  )  ) ; 
      }  
      
     /** 
     Report the error to the console. 
     */
 
     public void error  ( SAXParseException ex )  
         throws SAXException 
      {  
         System.out.println  
              ( "Error at Line " + locator.getLineNumber  (  )  + ":" ) ; 
         System.out.println  ( " " + ex.getMessage  (  )  ) ; 
      }  
      
     /** 
     Grab a reference to the parser's document locator for later use in 
     error and warning reports. 
     */
 
     public void setDocumentLocator  ( Locator locator )  
      {  
         this.locator = locator; 
      }  
      
     /** 
     Create a SAX parser that can validate against DTDs or Schema. 
     This is accomplished by delegation to  
      { @link #getSchemaValidatingSAXParser getSchemaValidatingSAXParser } . 
     Parse the document whose name is the first command-line argument. 
     */
 
     public static void main  ( String [  ]  args )  
      {  
         try 
          {  
             if  ( args.length == 0 )  
              {  
                 System.out.println  ( "Usage: " + 
                     "java SAXValidator  < XML filename > " ) ; 
                 System.exit  ( -1 ) ; 
              }  
              
             SAXParser parser = getSchemaValidatingSAXParser  (  ) ; 
             parser.parse  ( new java.io.File  ( args [ 0 ]  ) , new SAXValidator  (  )  ) ; 
          }  
         catch  ( Exception ex )  
          {  
             ex.printStackTrace  (  ) ; 
          }  
      }  
      
     /** 
     Gets a SAX parser with JAXP 1.2 features turned on 
     to allow validation of a parsed document using XML Schema. 
     */
 
     public static SAXParser getSchemaValidatingSAXParser  (  )  
      {  
         SAXParser result = null; 
  
  
         try 
          {  
             SAXParserFactory factory = SAXParserFactory.newInstance  (  ) ; 
             factory.setValidating  ( true ) ; 
             factory.setFeature  
                  ( "http://xml.org/sax/features/namespaces", true ) ; 
              
             result = factory.newSAXParser  (  ) ; 
             result.setProperty  
                  ( "http://java.sun.com/xml/jaxp/properties/schemaLanguage",  
                     "http://www.w3.org/2001/XMLSchema" ) ; 
          }  
         catch  ( Exception ex )  
          {  
             System.out.println  ( "This parser apparently can't " + 
                 "validate against XML Schema." ) ; 
             System.out.println  ( "   ( " + ex.getMessage  (  )  + " ) " ) ; 
          }  
          
         return result; 
      }  
      
     /** 
     This references the most recently reported document locator. 
     */
 
     private Locator locator; 
  } 


String getPublicId()
See Also:
getSystemId()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


String getSystemId()
See Also:
getPublicId()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags