[Castore-commits] XMLLuceneIndexer.java 1.13 1.14
Neman OULD SID'AHMED
neman at adullact1.hosting.cri74.org
Mer 16 Aou 16:27:02 CEST 2006
Update of /cvsroot/castore/castore-core/src/java/fr/emn/castor/recherche/metier/lucene
In directory adullact1:/tmp/cvs-serv22357/src/java/fr/emn/castor/recherche/metier/lucene
Modified Files:
XMLLuceneIndexer.java
Log Message:
mise en page
Index: XMLLuceneIndexer.java
===================================================================
RCS file: /cvsroot/castore/castore-core/src/java/fr/emn/castor/recherche/metier/lucene/XMLLuceneIndexer.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** XMLLuceneIndexer.java 11 Aug 2006 14:03:22 -0000 1.13
--- XMLLuceneIndexer.java 16 Aug 2006 14:27:00 -0000 1.14
***************
*** 74,78 ****
* Classe d'indexation de fichiers XML pour lucene. Travail effectue a partir
* des SandBox de lucene et plus particulierement Philip Ogren (ogren at mayo.edu)
! *
* @author $Author$
* @version $Revision$
--- 74,78 ----
* Classe d'indexation de fichiers XML pour lucene. Travail effectue a partir
* des SandBox de lucene et plus particulierement Philip Ogren (ogren at mayo.edu)
! *
* @author $Author$
* @version $Revision$
***************
*** 80,307 ****
public class XMLLuceneIndexer extends DefaultHandler implements ErrorHandler {
! /** le message si le fichier a indexer est introuvable. */
! public static final int FILE_NOT_FOUND = 0;
! /** message si la transformation xsl a echouee. */
! public static final int TRANSFORMER_EXCEPTION = 1;
! /** message si une erreur E/S est survenue pendant la transformation. */
! public static final int IO_EXCEPTION = 2;
! /** message si le parsing SAX a echoue. */
! public static final int SAX_EXCEPTION = 3;
! /** Utilisation du common logging. */
! private static Log log = LogFactory.getLog(XMLLuceneIndexer.class);
! /**
! * le reader XML.
! */
! private XMLReader xmlReader = null;
! /**
! * le flux XML transformé pour l'indexation.
! */
! private Transformer transformer = null;
! /**
! * la liste des fields a indexer.
! */
! private ArrayList<FieldHolder> fieldHolders = new ArrayList<FieldHolder>();
! /**
! * le field courant.
! */
! private FieldHolder currentField;
! /**
! * indique si on se trouve dans un champ à indexer.
! */
! private boolean inField = false;
! /**
! * le buffer de caracteres.
! */
! private StringBuffer charBuf = new StringBuffer();
! /**
! * Constructeur de l'indexeur.
! *
! * @param docType le type de document à indexer (document ou notice)
! */
! public XMLLuceneIndexer(final int docType) {
! try {
! //init reader
! this.xmlReader = XMLReaderFactory
! .createXMLReader(Constants.XML_SAX_PARSER);
! this.xmlReader.setContentHandler(this);
! this.xmlReader.setErrorHandler(this);
! // init transformer
! File xslFile = null;
! // on retrouve la feuille de style xsl transformant
! // le document ou la notice en fichier simplement indexable
! if (docType == IndexationRechercheLuceneDocEtNotice.DOCUMENT) {
! // traitement du document
! // URL xslDocUrl = Utils.();
! xslFile = new File(Constants.CASTOR_INDEX_XSL_FILES_PATH
! + Constants.FILE_SEPARATOR + "indexDocuments.xsl");
! } else if (docType == IndexationRechercheLuceneDocEtNotice.NOTICE) {
! // traitement de la notice
! xslFile = new File(Constants.CASTOR_INDEX_XSL_FILES_PATH
! + Constants.FILE_SEPARATOR + "indexNotices.xsl");
! } else {
! if (log.isFatalEnabled()) {
! log
! .fatal("pas de feuille de style trouvée pour indexer le document !");
! }
! return;
! }
! TransformerFactory transformerFactory = TransformerFactory
! .newInstance();
! StreamSource source = new StreamSource(xslFile
! .toURL().toString());
! this.transformer = transformerFactory.newTransformer(source);
! this.fieldHolders = new ArrayList<FieldHolder>();
! } catch (Exception ex) {
! if (log.isFatalEnabled()) {
! log.fatal(ex.getMessage(), ex);
! }
! }
! }
! /**
! * @see org.xml.sax.ContentHandler#startDocument()
! */
! public final void startDocument() throws SAXException {
! this.fieldHolders.clear();
! }
! /**
! * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
! * java.lang.String, java.lang.String, org.xml.sax.Attributes)
! */
! public final void startElement(
! final String uri, final String localName, final String qName,
! final Attributes attributes) throws SAXException {
! if (qName.equals("field") || qName.equals("datefield")) {
! this.currentField = new FieldHolder();
! this.currentField.setName(attributes.getValue("name"));
! this.currentField.appendValue(new StringBuffer());
! if (attributes.getValue("store").equals("true")) {
! this.currentField.setStore(Field.Store.YES);
! } else {
! this.currentField.setStore(Field.Store.NO);
! }
! if (attributes.getValue("index").equals("true")
! && attributes.getValue("token").equals("true")) {
! this.currentField.setIndex(Field.Index.TOKENIZED);
! } else if (attributes.getValue("token").equals("false")) {
! this.currentField.setIndex(Field.Index.UN_TOKENIZED);
! } else {
! this.currentField.setIndex(Field.Index.NO);
! }
! this.inField = true;
! }
! }
! /**
! * @see org.xml.sax.ContentHandler#characters(char[], int, int)
! */
! public final void characters(
! final char[] text, final int start, final int length) {
! // on recupere le texte entre les balises.
! StringBuffer buf = new StringBuffer();
! buf.append(text, start, length);
! // si on a pas vidé le buffer, on rajoute un espace entre
! // les caracteres
! // on vérifie tout de meme si on est pas au début du buffer de lecture
! // de SAX, sinon on insere un espace en trop !
! if ((this.charBuf.length() > 0) && (start != 0)) {
! this.charBuf.append(" ");
! }
! this.charBuf.append(buf.toString().trim());
! }
! /**
! * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
! * java.lang.String, java.lang.String)
! */
! public final void endElement(
! final String uri, final String localName, final String qName)
! throws SAXException {
! // on rajoute le field lu a la liste
! if (this.inField) {
! // on enleve les elements parasites
! this.currentField.appendValue(this.charBuf);
! this.fieldHolders.add(this.currentField);
! }
! this.inField = false;
! this.charBuf.setLength(0);
! }
! /**
! * Methode permettant de retourner un Document Lucene qui sera indexé.
! *
! * @param xmlFile le fichier XML source à indexer
! * @return un Document Lucene
! * @throws IndexationException si des erreurs surviennent durant
! * la transformation.
! */
! public final Document getDoc(final File xmlFile)
! throws IndexationException {
! // on demande a indexer le document xmlFile passé en parametre,
! // en utilisant une feuille de style pour cette transformation.
! ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
! BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
! byteOutputStream);
! StreamResult streamResult = new StreamResult(bufferedOutputStream);
! FileInputStream fileInputStream = null;
! try {
! fileInputStream = new FileInputStream(xmlFile);
! } catch (FileNotFoundException fnfe) {
! log.fatal(fnfe.getMessage(), fnfe);
! throw new IndexationException(FILE_NOT_FOUND);
! }
! StreamSource streamSource = new StreamSource(
! new BufferedInputStream(fileInputStream));
! try {
! this.transformer.transform(streamSource, streamResult);
! } catch (TransformerException te) {
! log.fatal(te.getMessage(), te);
! throw new IndexationException(TRANSFORMER_EXCEPTION);
! }
! BufferedInputStream inputStream = new BufferedInputStream(
! new ByteArrayInputStream(byteOutputStream.toByteArray()));
! try {
! this.xmlReader.parse(new InputSource(inputStream));
! } catch (IOException ioe) {
! log.fatal(ioe.getMessage(), ioe);
! throw new IndexationException(IO_EXCEPTION);
! } catch (SAXException se) {
! log.fatal(se.getMessage(), se);
! throw new IndexationException(SAX_EXCEPTION);
! }
! Document doc = new Document();
! for (int i = 0; i < this.fieldHolders.size(); i++) {
! FieldHolder fieldHolder = this.fieldHolders.get(i);
! String fieldValue =fieldHolder.getValue().toString();
!
!
!
! if (!fieldValue.trim().equals("")) {
! doc.add(new Field(
! fieldHolder.getName(), fieldValue, fieldHolder
! .getStore(), fieldHolder.getIndex().UN_TOKENIZED));
! }
! }
! try {
! fileInputStream.close();
! byteOutputStream.close();
! } catch (IOException ioe) {
! log.fatal(ioe.getMessage(), ioe);
! throw new IndexationException(IO_EXCEPTION);
! }
! return doc;
! }
}
\ No newline at end of file
--- 80,304 ----
public class XMLLuceneIndexer extends DefaultHandler implements ErrorHandler {
! /** le message si le fichier a indexer est introuvable. */
! public static final int FILE_NOT_FOUND = 0;
! /** message si la transformation xsl a echouee. */
! public static final int TRANSFORMER_EXCEPTION = 1;
! /** message si une erreur E/S est survenue pendant la transformation. */
! public static final int IO_EXCEPTION = 2;
! /** message si le parsing SAX a echoue. */
! public static final int SAX_EXCEPTION = 3;
! /** Utilisation du common logging. */
! private static Log log = LogFactory.getLog(XMLLuceneIndexer.class);
! /**
! * le reader XML.
! */
! private XMLReader xmlReader = null;
! /**
! * le flux XML transformé pour l'indexation.
! */
! private Transformer transformer = null;
! /**
! * la liste des fields a indexer.
! */
! private ArrayList<FieldHolder> fieldHolders = new ArrayList<FieldHolder>();
! /**
! * le field courant.
! */
! private FieldHolder currentField;
! /**
! * indique si on se trouve dans un champ à indexer.
! */
! private boolean inField = false;
! /**
! * le buffer de caracteres.
! */
! private StringBuffer charBuf = new StringBuffer();
! /**
! * Constructeur de l'indexeur.
! *
! * @param docType
! * le type de document à indexer (document ou notice)
! */
! public XMLLuceneIndexer(final int docType) {
! try {
! // init reader
! this.xmlReader = XMLReaderFactory
! .createXMLReader(Constants.XML_SAX_PARSER);
! this.xmlReader.setContentHandler(this);
! this.xmlReader.setErrorHandler(this);
! // init transformer
! File xslFile = null;
! // on retrouve la feuille de style xsl transformant
! // le document ou la notice en fichier simplement indexable
! if (docType == IndexationRechercheLuceneDocEtNotice.DOCUMENT) {
! // traitement du document
! // URL xslDocUrl = Utils.();
! xslFile = new File(Constants.CASTOR_INDEX_XSL_FILES_PATH
! + Constants.FILE_SEPARATOR + "indexDocuments.xsl");
! } else if (docType == IndexationRechercheLuceneDocEtNotice.NOTICE) {
! // traitement de la notice
! xslFile = new File(Constants.CASTOR_INDEX_XSL_FILES_PATH
! + Constants.FILE_SEPARATOR + "indexNotices.xsl");
! } else {
! if (log.isFatalEnabled()) {
! log
! .fatal("pas de feuille de style trouvée pour indexer le document !");
! }
! return;
! }
! TransformerFactory transformerFactory = TransformerFactory
! .newInstance();
! StreamSource source = new StreamSource(xslFile.toURL().toString());
! this.transformer = transformerFactory.newTransformer(source);
! this.fieldHolders = new ArrayList<FieldHolder>();
! } catch (Exception ex) {
! if (log.isFatalEnabled()) {
! log.fatal(ex.getMessage(), ex);
! }
! }
! }
! /**
! * @see org.xml.sax.ContentHandler#startDocument()
! */
! public final void startDocument() throws SAXException {
! this.fieldHolders.clear();
! }
! /**
! * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
! * java.lang.String, java.lang.String, org.xml.sax.Attributes)
! */
! public final void startElement(final String uri, final String localName,
! final String qName, final Attributes attributes)
! throws SAXException {
! if (qName.equals("field") || qName.equals("datefield")) {
! this.currentField = new FieldHolder();
! this.currentField.setName(attributes.getValue("name"));
! this.currentField.appendValue(new StringBuffer());
! if (attributes.getValue("store").equals("true")) {
! this.currentField.setStore(Field.Store.YES);
! } else {
! this.currentField.setStore(Field.Store.NO);
! }
! if (attributes.getValue("index").equals("true")
! && attributes.getValue("token").equals("true")) {
! this.currentField.setIndex(Field.Index.TOKENIZED);
! } else if (attributes.getValue("token").equals("false")) {
! this.currentField.setIndex(Field.Index.UN_TOKENIZED);
! } else {
! this.currentField.setIndex(Field.Index.NO);
! }
! this.inField = true;
! }
! }
! /**
! * @see org.xml.sax.ContentHandler#characters(char[], int, int)
! */
! public final void characters(final char[] text, final int start,
! final int length) {
! // on recupere le texte entre les balises.
! StringBuffer buf = new StringBuffer();
! buf.append(text, start, length);
! // si on a pas vidé le buffer, on rajoute un espace entre
! // les caracteres
! // on vérifie tout de meme si on est pas au début du buffer de lecture
! // de SAX, sinon on insere un espace en trop !
! if ((this.charBuf.length() > 0) && (start != 0)) {
! this.charBuf.append(" ");
! }
! this.charBuf.append(buf.toString().trim());
! }
! /**
! * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
! * java.lang.String, java.lang.String)
! */
! public final void endElement(final String uri, final String localName,
! final String qName) throws SAXException {
! // on rajoute le field lu a la liste
! if (this.inField) {
! // on enleve les elements parasites
! this.currentField.appendValue(this.charBuf);
! this.fieldHolders.add(this.currentField);
! }
! this.inField = false;
! this.charBuf.setLength(0);
! }
! /**
! * Methode permettant de retourner un Document Lucene qui sera indexé.
! *
! * @param xmlFile
! * le fichier XML source à indexer
! * @return un Document Lucene
! * @throws IndexationException
! * si des erreurs surviennent durant la transformation.
! */
! public final Document getDoc(final File xmlFile) throws IndexationException {
! // on demande a indexer le document xmlFile passé en parametre,
! // en utilisant une feuille de style pour cette transformation.
! ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
! BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
! byteOutputStream);
! StreamResult streamResult = new StreamResult(bufferedOutputStream);
! FileInputStream fileInputStream = null;
! try {
! fileInputStream = new FileInputStream(xmlFile);
! } catch (FileNotFoundException fnfe) {
! log.fatal(fnfe.getMessage(), fnfe);
! throw new IndexationException(FILE_NOT_FOUND);
! }
! StreamSource streamSource = new StreamSource(new BufferedInputStream(
! fileInputStream));
! try {
! this.transformer.transform(streamSource, streamResult);
! } catch (TransformerException te) {
! log.fatal(te.getMessage(), te);
! throw new IndexationException(TRANSFORMER_EXCEPTION);
! }
! BufferedInputStream inputStream = new BufferedInputStream(
! new ByteArrayInputStream(byteOutputStream.toByteArray()));
! try {
! this.xmlReader.parse(new InputSource(inputStream));
! } catch (IOException ioe) {
! log.fatal(ioe.getMessage(), ioe);
! throw new IndexationException(IO_EXCEPTION);
! } catch (SAXException se) {
! log.fatal(se.getMessage(), se);
! throw new IndexationException(SAX_EXCEPTION);
! }
! Document doc = new Document();
! for (int i = 0; i < this.fieldHolders.size(); i++) {
! FieldHolder fieldHolder = this.fieldHolders.get(i);
! String fieldValue = fieldHolder.getValue().toString();
!
! if (!fieldValue.trim().equals("")) {
! doc.add(new Field(fieldHolder.getName(), fieldValue,
! fieldHolder.getStore(),
! fieldHolder.getIndex().UN_TOKENIZED));
! }
! }
! try {
! fileInputStream.close();
! byteOutputStream.close();
! } catch (IOException ioe) {
! log.fatal(ioe.getMessage(), ioe);
! throw new IndexationException(IO_EXCEPTION);
! }
! return doc;
! }
}
\ No newline at end of file
Plus d'informations sur la liste de diffusion Castore-commits