package org.ariadne_eu.utils.lucene.document;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.eun.lucene.core.indexer.document.DocumentHandler;
import org.eun.lucene.core.indexer.document.DocumentHandlerException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
public class CAMHandler extends DocumentHandler {
private static final String[] MIN_MAX = { "min", "max" };
/** A buffer for each XML element */
private StringBuffer elementBuffer = new StringBuffer();
private String purpose = "", taxonPathSource, purposeFieldName, tpSourceFieldName, taxonPathId, tpIdFieldName, source, catalog, identifier;
private HashMap<String, String> attributeMap = new HashMap<String, String>();
private String branche = "";
private Document doc;
private String contents;
private final String BRANCH_SEPARATOR = ".";
private final String ATT_SEPARATOR = ".";
private final String EQUAL_SEPARATOR = "=";
public Document getDocument(InputStream is) throws DocumentHandlerException {
SAXParserFactory spf = SAXParserFactory.newInstance();
try {
SAXParser parser = spf.newSAXParser();
parser.parse(is, this);
} catch (IOException e) {
throw new DocumentHandlerException("Cannot parse XML document", e);
} catch (ParserConfigurationException e) {
throw new DocumentHandlerException("Cannot parse XML document", e);
} catch (SAXException e) {
throw new DocumentHandlerException("Cannot parse XML document", e);
}
return doc;
}
public void startDocument() {
doc = new Document();
contents = new String();
}
public void endDocument() {
doc.add(new Field("contents", contents, Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("group.solr", "all", Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
}
/*
* Save the attribute in a map to reuse it when the element ends (only used
* for the last element of a branch) Add an attribute field Incremental
* string creation to represent the current branch parsed
*
* @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
* java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
branche += qName.toLowerCase();
elementBuffer.setLength(0);
attributeMap.clear();// No need for a map :D
if (atts.getLength() > 0) {
attributeMap = new HashMap<String, String>();
for (int i = 0; i < atts.getLength(); i++) {
attributeMap.put(atts.getQName(i), atts.getValue(i));
if (!atts.getQName(i).equals("uniqueElementName")) {
if (atts.getQName(i).equalsIgnoreCase("xmlns") || atts.getQName(i).equalsIgnoreCase("xsi:schemaLocation")) {
String fieldName = "untokenized." + atts.getQName(i);
doc.add(new Field(fieldName.toLowerCase(), atts.getValue(i).toLowerCase(), Field.Store.YES, Field.Index.NOT_ANALYZED));// XXX
fieldName = atts.getQName(i);
doc.add(new Field(fieldName.toLowerCase(), atts.getValue(i).toLowerCase(), Field.Store.YES, Field.Index.NOT_ANALYZED));// XXX
} else {
String tmpBranche = branche.substring(0, branche.length());
//remove the NS+colons on any element
if (tmpBranche.contains(":")) {
tmpBranche = tmpBranche.replaceAll("(\\w+):", "");
}
String fieldName = tmpBranche + "" + ATT_SEPARATOR + "" + atts.getQName(i);
doc.add(new Field(fieldName.toLowerCase(), atts.getValue(i).toLowerCase(), Field.Store.YES,Field.Index.NOT_ANALYZED));// XXX
}
}
}
}
branche += BRANCH_SEPARATOR;
}
public void characters(char[] text, int start, int length) {
elementBuffer.append(text, start, length);
}
public void endElement(String uri, String localName, String qName) throws SAXException {
String tmpBranche = branche.substring(0, branche.length() - 1);
String tmp2Branche = "";
if (branche.endsWith(qName.toLowerCase() + "" + BRANCH_SEPARATOR)) {
branche = branche.substring(0, branche.length() - qName.length() - 1);
if (!branche.equals(""))
tmp2Branche = branche.substring(0, branche.length() - 1);
}
if (elementBuffer.toString().trim().equals("")) {
return;
}
// In all the other cases add a field !
doc.add(new Field(tmpBranche.toLowerCase(), elementBuffer.toString().toLowerCase(), Field.Store.YES, Field.Index.NOT_ANALYZED));// XXX
// to store the contents without metatags
contents = contents.concat(" " + elementBuffer.toString().toLowerCase());
elementBuffer.setLength(0);
}
public static void main(String args[]) throws Exception {
CAMHandler handler = new CAMHandler();
Document doc = handler.getDocument(new FileInputStream(new File("/Work/CAM/Examples/ZoepCAMsample.xml")));
List fields = doc.getFields();
for (Iterator iterator = fields.iterator(); iterator.hasNext();) {
Field field = (Field) iterator.next();
System.out.println(field.name() + " :: " + field.stringValue());
}
}
}