lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From o...@apache.org
Subject cvs commit: jakarta-lucene-sandbox/contributions/XML-Indexing-Demo/src/java/org/apache/lucenesandbox/xmlindexingdemo XMLDocumentHandlerSAX.java
Date Tue, 06 Apr 2004 09:51:41 GMT
otis        2004/04/06 02:51:41

  Modified:    contributions/XML-Indexing-Demo/src/java/org/apache/lucenesandbox/xmlindexingdemo
                        XMLDocumentHandlerSAX.java
  Log:
  - Applied a patch from bug entry: src/java/org/apache/lucenesandbox/xmlindexingdemo/XMLDocumentHandlerSAX.java
  
  Revision  Changes    Path
  1.3       +66 -13    jakarta-lucene-sandbox/contributions/XML-Indexing-Demo/src/java/org/apache/lucenesandbox/xmlindexingdemo/XMLDocumentHandlerSAX.java
  
  Index: XMLDocumentHandlerSAX.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene-sandbox/contributions/XML-Indexing-Demo/src/java/org/apache/lucenesandbox/xmlindexingdemo/XMLDocumentHandlerSAX.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- XMLDocumentHandlerSAX.java	11 Jan 2004 14:12:19 -0000	1.2
  +++ XMLDocumentHandlerSAX.java	6 Apr 2004 09:51:41 -0000	1.3
  @@ -1,7 +1,20 @@
   package org.apache.lucenesandbox.xmlindexingdemo;
   
  -import org.xml.sax.*;
  -import javax.xml.parsers.*;
  +/**
  + * Copyright 2004 The Apache Software Foundation
  + *
  + * Licensed under the Apache License, Version 2.0 (the "License");
  + * you may not use this file except in compliance with the License.
  + * You may obtain a copy of the License at
  + *
  + *     http://www.apache.org/licenses/LICENSE-2.0
  + *
  + * Unless required by applicable law or agreed to in writing, software
  + * distributed under the License is distributed on an "AS IS" BASIS,
  + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  + * See the License for the specific language governing permissions and
  + * limitations under the License.
  + */
   
   import org.apache.lucene.document.Document;
   import org.apache.lucene.document.Field;
  @@ -9,8 +22,14 @@
   import java.io.File;
   import java.io.IOException;
   
  -public class XMLDocumentHandlerSAX
  -  extends HandlerBase {
  +import javax.xml.parsers.ParserConfigurationException;
  +import javax.xml.parsers.SAXParser;
  +import javax.xml.parsers.SAXParserFactory;
  +import org.xml.sax.Attributes;
  +import org.xml.sax.SAXException;
  +import org.xml.sax.helpers.DefaultHandler;
  +
  +public class XMLDocumentHandlerSAX extends DefaultHandler {
     /** A buffer for each XML element */
     private StringBuffer elementBuffer = new StringBuffer();
   
  @@ -21,30 +40,64 @@
       throws ParserConfigurationException, SAXException, IOException {
       SAXParserFactory spf = SAXParserFactory.newInstance();
   
  +    // use validating parser?
  +    //spf.setValidating(false);
  +    // make parser name space aware?
  +    //spf.setNamespaceAware(true);
  +
       SAXParser parser = spf.newSAXParser();
  -    parser.parse(xmlFile, this);
  +    //System.out.println("parser is validating: " + parser.isValidating());
  +    try {
  +      parser.parse(xmlFile, this);
  +    } catch (org.xml.sax.SAXParseException spe) {
  +      System.out.println("SAXParser caught SAXParseException at line: " +
  +        spe.getLineNumber() + " column " +
  +        spe.getColumnNumber());
  +    }
     }
   
     // call at document start
  -  public void startDocument() {
  +  public void startDocument() throws SAXException {
       mDocument = new Document();
     }
   
     // call at element start
  -  public void startElement(String localName, AttributeList atts)
  -    throws SAXException {
  -    elementBuffer.setLength(0);
  +  public void startElement(String namespaceURI, String localName,
  +    String qualifiedName, Attributes attrs) throws SAXException {
  +
  +    String eName = localName;
  +     if ("".equals(eName)) {
  +       eName = qualifiedName; // namespaceAware = false
  +     }
  +     // list the attribute(s)
  +     if (attrs != null) {
  +       for (int i = 0; i < attrs.getLength(); i++) {
  +         String aName = attrs.getLocalName(i); // Attr name
  +         if ("".equals(aName)) { aName = attrs.getQName(i); }
  +         // perform application specific action on attribute(s)
  +         // for now just dump out attribute name and value
  +         System.out.println("attr " + aName+"="+attrs.getValue(i));
  +       }
  +     }
  +     elementBuffer.setLength(0);
     }
   
     // call when cdata found
  -  public void characters(char[] text, int start, int length) {
  +  public void characters(char[] text, int start, int length)
  +    throws SAXException {
       elementBuffer.append(text, start, length);
     }
   
     // call at element end
  -  public void endElement(String localName)
  -    throws SAXException {
  -    mDocument.add(Field.Text(localName, elementBuffer.toString()));
  +  public void endElement(String namespaceURI, String simpleName,
  +    String qualifiedName)  throws SAXException {
  +
  +    String eName = simpleName;
  +    if ("".equals(eName)) {
  +      eName = qualifiedName; // namespaceAware = false
  +    }
  +
  +    mDocument.add(Field.Text(eName, elementBuffer.toString()));
     }
   
     public Document getDocument() {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org


Mime
View raw message