/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
* $Id: DocumentHandler.java 568786 2007-08-23 00:40:57Z vgritsenko $
*/
package org.apache.xindice.core.indexer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.core.DBException;
import org.apache.xindice.core.data.Key;
import org.apache.xindice.util.ObjectStack;
import org.apache.xindice.util.ReadOnlyException;
import org.apache.xindice.xml.SymbolTable;
import org.apache.xindice.xml.sax.CompressionHandler;
import org.apache.xindice.xml.sax.SAXEventGenerator;
import org.w3c.dom.Document;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
/**
* DocumentHandler actually performs the work of adding and removing Indexer
* entries by calling appropiate methods of IndexerEventHandler.
*
* @version $Revision: 568786 $, $Date: 2007-08-22 20:40:57 -0400 (Wed, 22 Aug 2007) $
*/
class DocumentHandler implements ContentHandler, CompressionHandler {
private static final Log log = LogFactory.getLog(DocumentHandler.class);
static final int ACTION_CREATE = 0;
static final int ACTION_DELETE = 1;
private ObjectStack stack = new ObjectStack();
private Indexer[] indexers;
private IndexerEventHandler[] handlers;
public Key key;
public Document doc;
public int action;
private SymbolTable symbols;
public StackInfo info; // Current State
public DocumentHandler(SymbolTable symbols, Key key, Document doc, int action, Indexer[] list) {
this.symbols = symbols;
this.key = key;
this.doc = doc;
this.action = action;
this.indexers = list;
handlers = new IndexerEventHandler[list.length];
for (int i = 0; i < list.length; i++) {
handlers[i] = list[i].getIndexerEventHandler();
}
try {
SAXEventGenerator events = new SAXEventGenerator(symbols, doc);
events.setContentHandler(this);
events.setProperty(HANDLER, this);
events.start();
} catch (Exception e) {
if (log.isWarnEnabled()) {
log.warn("ignored exception", e);
}
}
}
// These are all NO-OPs
public void setDocumentLocator(Locator locator) {
}
public void startPrefixMapping(String prefix, String uri) {
}
public void endPrefixMapping(String prefix) {
}
public void ignorableWhitespace(char ch[], int start, int length) {
}
public void processingInstruction(String target, String data) {
}
public void skippedEntity(String name) {
}
public void symbols(SymbolTable symbols) {
}
public void dataBytes(byte[] data) {
}
public void startDocument() {
}
public void endDocument() {
for (int i = 0; i < handlers.length; i++ ) {
try {
switch (action) {
case ACTION_CREATE:
handlers[i].onDocumentAdded(key);
break;
case ACTION_DELETE:
handlers[i].onDocumentDeleted(key);
break;
default:
if (log.isWarnEnabled()) {
log.warn("invalid action : " + action);
}
}
} catch (Exception e) {
if (log.isWarnEnabled()) {
log.warn("ignored exception", e);
}
}
}
}
public void processEntry(IndexPattern pattern, String value, int pos, int len) {
for (int i = 0; i < handlers.length; i++ ) {
try {
switch (action) {
case ACTION_CREATE:
add(i, pattern, value, key, pos, len);
break;
case ACTION_DELETE:
delete(i, pattern, value, key, pos, len);
break;
default:
if (log.isWarnEnabled()) {
log.warn("invalid action : " + action);
}
}
} catch (Exception e) {
if (log.isWarnEnabled()) {
log.warn("ignored exception", e);
}
}
}
}
public void add(int n, IndexPattern ptrn, String value, Key key, int pos, int len) throws DBException {
IndexPattern[] patterns = indexers[n].getPatterns();
for (int i = 0; i < patterns.length; i++) {
if (ptrn.getMatchLevel(patterns[i]) > 0) {
handlers[n].onNameAdded(patterns[i], key, ptrn.getElementID(), ptrn.getAttributeID());
handlers[n].onValueAdded(patterns[i], value, key, pos, len, ptrn.getElementID(), ptrn.getAttributeID());
}
}
}
public void delete(int n, IndexPattern ptrn, String value, Key key, int pos, int len) throws DBException {
IndexPattern[] patterns = indexers[n].getPatterns();
for (int i = 0; i < patterns.length; i++) {
if (ptrn.getMatchLevel(patterns[i]) > 0) {
handlers[n].onNameDeleted(patterns[i], key, ptrn.getElementID(), ptrn.getAttributeID());
handlers[n].onValueDeleted(patterns[i], value, key, pos, len, ptrn.getElementID(), ptrn.getAttributeID());
}
}
}
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
try {
// Modify the stack info to normalize the symbolID
if (namespaceURI != null && namespaceURI.length() > 0) {
info.symbolID = symbols.getNormalizedSymbol(localName, namespaceURI, true);
}
int size = atts.getLength();
for (int i = 0; i < size; i++) {
String nsURI = atts.getURI(i);
short id;
if (nsURI != null && nsURI.length() > 0) {
id = symbols.getNormalizedSymbol(atts.getLocalName(i), nsURI, true);
} else {
id = symbols.getSymbol(atts.getQName(i), true);
}
processEntry(new IndexPattern(symbols, info.symbolID, id), atts.getValue(i), info.pos, info.len);
}
} catch (ReadOnlyException e) {
throw new SAXException(e);
}
}
public void endElement(String namespaceURI, String localName, String qName) {
StringBuffer sb = info.sb;
processEntry(new IndexPattern(symbols, info.symbolID), sb.toString(), info.pos, info.len);
info = (StackInfo) stack.pop();
if (info != null) {
info.sb.append(sb);
}
}
public void characters(char ch[], int start, int length) {
info.sb.append(ch, start, length);
}
public void symbolID(short symbolID) {
if (info != null) {
stack.push(info);
}
info = new StackInfo(symbolID);
}
public void dataLocation(int pos, int len) {
info.pos = pos;
info.len = len;
}
/**
* StackInfo
*/
private class StackInfo {
public short symbolID;
public StringBuffer sb = new StringBuffer();
public int pos = -1;
public int len = -1;
public StackInfo(short symbolID) {
this.symbolID = symbolID;
}
}
}