/[sdx]/sdx_v2/src/java/fr/gouv/culture/sdx/document/AbstractIndexableDocument.java
ViewVC logotype

Annotation of /sdx_v2/src/java/fr/gouv/culture/sdx/document/AbstractIndexableDocument.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.73 - (hide annotations) (download)
Mon May 17 13:48:40 2004 UTC (20 years ago) by rpandey
Branch: MAIN
CVS Tags: sdx_june_2004
Changes since 1.72: +2 -2 lines
some string refactoring and misc. other refactoring

1 msevigny 1.1 /*
2     SDX: Documentary System in XML.
3     Copyright (C) 2000, 2001, 2002 Ministere de la culture et de la communication (France), AJLSM
4    
5     Ministere de la culture et de la communication,
6     Mission de la recherche et de la technologie
7     3 rue de Valois, 75042 Paris Cedex 01 (France)
8     mrt@culture.fr, michel.bottin@culture.fr
9    
10     AJLSM, 17, rue Vital Carles, 33000 Bordeaux (France)
11     sevigny@ajlsm.com
12    
13     This program is free software; you can redistribute it and/or
14     modify it under the terms of the GNU General Public License
15     as published by the Free Software Foundation; either version 2
16     of the License, or (at your option) any later version.
17    
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21     See the GNU General Public License for more details.
22    
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the
25     Free Software Foundation, Inc.
26     59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
27     or connect to:
28     http://www.fsf.org/copyleft/gpl.html
29     */
30     package fr.gouv.culture.sdx.document;
31    
32 rpandey 1.34 import fr.gouv.culture.sdx.documentbase.DefaultIDGenerator;
33     import fr.gouv.culture.sdx.documentbase.IDGenerator;
34 rpandey 1.8 import fr.gouv.culture.sdx.exception.SDXException;
35 fglorieux 1.18 import fr.gouv.culture.sdx.exception.SDXExceptionCode;
36 msevigny 1.1 import fr.gouv.culture.sdx.framework.Framework;
37 rpandey 1.15 import fr.gouv.culture.sdx.utils.Utilities;
38 rpandey 1.39 import fr.gouv.culture.sdx.utils.constants.Node;
39 rpandey 1.71 import fr.gouv.culture.sdx.utils.logging.LoggingUtils;
40 rpandey 1.54 import org.apache.avalon.framework.parameters.ParameterException;
41     import org.apache.avalon.framework.parameters.Parameters;
42 rpandey 1.45 import org.apache.cocoon.serialization.XMLSerializer;
43 msevigny 1.1 import org.apache.cocoon.xml.XMLConsumer;
44 rpandey 1.3 import org.xml.sax.Attributes;
45     import org.xml.sax.ContentHandler;
46     import org.xml.sax.Locator;
47     import org.xml.sax.SAXException;
48 msevigny 1.1 import org.xml.sax.ext.LexicalHandler;
49 rpandey 1.68 import org.xml.sax.helpers.AttributesImpl;
50 msevigny 1.1
51 rpandey 1.45 import java.io.ByteArrayOutputStream;
52     import java.io.IOException;
53 rpandey 1.19 import java.net.MalformedURLException;
54     import java.net.URL;
55 rpandey 1.64 import java.util.ArrayList;
56 rpandey 1.3 import java.util.Enumeration;
57 msevigny 1.1 import java.util.Vector;
58    
59     /**
60     * An abstract class for indexable documents.
61 rpandey 1.30 *TODO: THIS SHOULD BE BETTER, IN MORE DETAILS OF THE BASIC INDEXATION ELEMENT CREATION,
62 rpandey 1.20 * should refer to some schema and it works from the permuatations of the schema.
63 msevigny 1.1 */
64 rpandey 1.30 //TODO?:should this class implement the ParsableDocument interface as both subclasses do?-rbp
65    
66 msevigny 1.1 public abstract class AbstractIndexableDocument extends AbstractDocument implements IndexableDocument {
67    
68     /* These methods handle receiving of indexing data. */
69    
70 rpandey 1.49 protected String DOC_NAMESPACE = Framework.SDXNamespaceURI;
71    
72     protected String DOC_ROOT_ELEMENT_NAME = Node.Name.DOCUMENT;
73    
74     protected String DOC_FIELD_ELEMENT_NAME = Node.Name.FIELD;
75    
76     protected String DOC_ATTACHEDOC_ELEMENT_NAME = Node.Name.ATTACHED_DOCUMENT;
77    
78 rpandey 1.67 protected String DOC_MSG_ELEMENT_NAME = Node.Name.MESSAGE;
79    
80 rpandey 1.52 protected Parameters nsTable = null;
81    
82 msevigny 1.1 /** List of fields for indexing. */
83 rpandey 1.6 protected Vector properties;
84 msevigny 1.1
85     /** A character buffer for element content. */
86 rpandey 1.53 protected StringBuffer characterBuffer;
87 msevigny 1.1
88 brihaye 1.2 /** The current field name */
89 rpandey 1.53 protected String currentFieldName;
90 msevigny 1.1
91 rpandey 1.49 protected int openSdxDocElems = 0;
92 rpandey 1.43
93 rpandey 1.4 /**A list of attached documents*/
94 rpandey 1.70 protected Vector attachedDocuments = null;
95 msevigny 1.1
96 rpandey 1.47 /**A list of sub(Indexable) documents*/
97     protected Vector subDocuments;
98    
99 rpandey 1.20 /** Indicates wheter the documents must be refreshed. */
100 rpandey 1.8 private boolean updateAttachedDocuments = true;
101 rpandey 1.4
102 rpandey 1.30 /**A document resulting from a transformation*/
103 rpandey 1.31 protected IndexableDocument transformedDoc = null;
104 rpandey 1.30
105 rpandey 1.54 //protected Stack subDocs = new Stack();
106 rpandey 1.45 protected IndexableDocument subDoc = null;
107    
108     protected ByteArrayOutputStream subDocBytes = null;
109    
110 rpandey 1.49 protected boolean withinSdxElement = false;
111 rpandey 1.45
112 rpandey 1.68 protected ContentHandler _msgHandler = null;
113    
114     /**
115     * @see org.apache.lucene.document.Document#boost
116     */
117     protected float _boost = 1.0f;//defaulted to value in lucene Document.class
118    
119     protected float _currentFieldBoost = 1.0f;
120    
121 rpandey 1.4 public void startElement(String nsURI, String name, String qName, Attributes atts) throws SAXException {
122 rpandey 1.62 if (DOC_NAMESPACE.equals(nsURI)) {
123 rpandey 1.45 this.withinSdxElement = true;
124    
125 rpandey 1.62 if (DOC_FIELD_ELEMENT_NAME.equals(name)) {
126 rpandey 1.68 this._currentFieldBoost = 1.0f;//resetting for each new field
127 rpandey 1.45 if (this.openSdxDocElems > 1 && this.subDoc != null)
128     this.subDoc.startElement(nsURI, name, qName, atts);
129    
130     else if (atts != null) {
131 rpandey 1.68 String boost = atts.getValue(Node.Name.BOOST);
132     if (Utilities.checkString(boost))
133     this._currentFieldBoost = Float.parseFloat(boost);
134 fglorieux 1.40 currentFieldName = atts.getValue(Node.Name.NAME);
135 brihaye 1.61 //backward compatibility : try to use old attribute name
136 fglorieux 1.40 if (currentFieldName == null || "".equals(currentFieldName)) currentFieldName = atts.getValue(Node.Name.CODE);
137     }
138 rpandey 1.45
139 msevigny 1.1 characterBuffer = new StringBuffer();
140 rpandey 1.62 } else if (DOC_ATTACHEDOC_ELEMENT_NAME.equals(name)) {
141 rpandey 1.45
142 rpandey 1.47 if (this.openSdxDocElems > 1 && this.subDoc != null)
143 rpandey 1.45 this.subDoc.startElement(nsURI, name, qName, atts);
144    
145     else if (this.updateAttachedDocuments()) {
146 rpandey 1.8 if (atts != null) {
147 rpandey 1.39 String id = atts.getValue(Node.Name.ID);
148 fglorieux 1.60 String attid = atts.getValue(Node.Name.ATTID);
149 uid67066 1.66 if (Utilities.checkString(attid)) {
150     if (!Utilities.checkString(id))
151     id = super.getId();//no base 'id' was passed, but an 'attId' was passed so we will use the id of the parent document
152 fglorieux 1.60 id = Utilities.attId(id, attid);
153 uid67066 1.66 }
154 rpandey 1.25 if (Utilities.checkString(id)) {
155 rpandey 1.4 try {
156 rpandey 1.43 this.addAttachedDocument(id, getURL(), atts.getValue(Node.Name.URL), atts.getValue(Node.Name.MIMETYPE), atts.getValue(Node.Name.REPO));
157 rpandey 1.4 } catch (SDXException e) {
158 rpandey 1.11 throw new SAXException(e.getMessage(), e);
159 rpandey 1.4 }
160 fglorieux 1.18 }
161 rpandey 1.4 }
162     }
163 msevigny 1.1 }
164     /*
165     else if ( name.equals("table") )
166     {
167     if ( atts != null )
168     {
169     tableName = atts.getValue("name");
170     if ( tableName != null ) tableValues = new Hashtable();
171     }
172     }
173     else if ( name.equals("column") )
174     {
175     if ( atts != null )
176     {
177     columnName = atts.getValue("name");
178     content = new StringBuffer();
179     }
180     }
181     */
182 rpandey 1.62 else if (DOC_ROOT_ELEMENT_NAME.equals(name)) {
183 rpandey 1.15 try {
184 rpandey 1.45 this.openSdxDocElems++;
185    
186 rpandey 1.47 if (this.openSdxDocElems == 1) {
187 rpandey 1.45 this.handleDocumentId(atts);
188 rpandey 1.48 String repo = atts.getValue(Node.Name.REPO);
189     if (Utilities.checkString(repo))
190     setRepositoryForStorage(repo);
191 rpandey 1.68
192     String boost = atts.getValue(Node.Name.BOOST);
193     if (Utilities.checkString(boost))
194     setBoost(Float.parseFloat(boost));
195    
196 rpandey 1.45 }
197     if (this.openSdxDocElems > 1) {
198     if (this.openSdxDocElems == 2) {
199     XMLSerializer sBytes = new XMLSerializer();
200     this.subDocBytes = new ByteArrayOutputStream();
201     sBytes.setOutputStream(subDocBytes);
202     //if we have a sub doc we build the appropriate consumer
203 rpandey 1.49 try {
204     this.subDoc = (IndexableDocument) this.getClass().newInstance();
205     } catch (InstantiationException e) {
206     throw new SAXException(e.getMessage(), e);
207     } catch (IllegalAccessException e) {
208     throw new SAXException(e.getMessage(), e);
209     }
210     //TODO: the below line should be handled in the constructors
211 rpandey 1.47 ((AbstractIndexableDocument) this.subDoc).resetFields();
212 rpandey 1.45 this.setConsumer(sBytes);
213     if (this.xmlConsumer != null) this.xmlConsumer.startDocument();
214 rpandey 1.52 //adding our namespace prefix mappings from the parent document
215     startNamespaceMappings();
216 rpandey 1.45 }
217     if (this.subDoc != null)
218     this.subDoc.startElement(nsURI, name, qName, atts);
219     }
220    
221    
222 rpandey 1.15 } catch (SDXException e) {
223     throw new SAXException(e.getMessage(), e);
224 rpandey 1.8 }
225 rpandey 1.68 } else if (DOC_MSG_ELEMENT_NAME.equals(name))
226 rpandey 1.67 characterBuffer = new StringBuffer();
227 rpandey 1.47 } else {
228 rpandey 1.45 this.withinSdxElement = false;
229 rpandey 1.47 if (this.openSdxDocElems == 2 && this.xmlConsumer != null)
230     this.xmlConsumer.startElement(nsURI, name, qName, atts);
231 rpandey 1.45 else if (this.subDoc != null)
232     this.subDoc.startElement(nsURI, name, qName, atts);
233     }
234    
235 msevigny 1.1 }
236    
237 fglorieux 1.22 /*
238 rpandey 1.21 private void handleDocumentId(org.xml.sax.Attributes atts) throws SDXException {
239 rpandey 1.15 if (atts != null) {
240 fglorieux 1.18 // default id from <sdx:document id="my_id"/>
241     String docId = atts.getValue("id");
242     // overriding <sdx:document id="my_id" overrideId="true"/> (example: set of document with random possible not unique id)
243 rpandey 1.21 if ("false".equalsIgnoreCase(atts.getValue("overrideId"))) docId = null;
244 fglorieux 1.18 // generate-id <sdx:document id="my_id" generateId="true"/>
245 rpandey 1.19 if ("true".equalsIgnoreCase(atts.getValue("generateId"))) {
246     docId = generateId(atts.getValue("generator"));
247     }
248     if (docId != null) {
249     setId(docId);
250 fglorieux 1.18 }
251 rpandey 1.15
252 rpandey 1.21 //we verify the document to see if we have at least a valid unique id for our
253     //internal purposes, if so we all is well, otherwise we throw an SDXException
254 rpandey 1.72 Utilities.checkDocument(super.getLog(), this);
255 rpandey 1.21 }
256    
257     }
258 fglorieux 1.22 */
259 rpandey 1.21
260 rpandey 1.49 protected void handleDocumentId(org.xml.sax.Attributes atts) throws SDXException {
261 fglorieux 1.42 // already setted id
262 rpandey 1.44 String id = getId();
263 fglorieux 1.42 // override allowed by default
264 rpandey 1.44 boolean overrideId = true;
265 fglorieux 1.42 // don't force generateId by default
266 rpandey 1.44 boolean generateId = false;
267     if (atts != null) {
268 fglorieux 1.42 // get boolean attributes
269     if (atts.getValue(Node.Name.OVERRIDE_ID) != null)
270     if (atts.getValue(Node.Name.OVERRIDE_ID).equalsIgnoreCase("false"))
271 rpandey 1.44 overrideId = false;
272 fglorieux 1.42 if (atts.getValue(Node.Name.GENERATE_ID) != null)
273     if (atts.getValue(Node.Name.GENERATE_ID).equalsIgnoreCase("true"))
274 rpandey 1.44 generateId = true;
275 fglorieux 1.42 // indexation id
276     if (Utilities.checkString(atts.getValue(Node.Name.ID)) && overrideId)
277     id = atts.getValue(Node.Name.ID);
278     }
279     // if no id already available, or generateId wanted ; and overrideId allowed
280 rpandey 1.44 if ((!Utilities.checkString(id) || generateId) && overrideId)
281 rpandey 1.49 //TODO: pass a class name if there is one provided for id generation, in this case we can't verify against a documentbase??
282 rpandey 1.44 id = generateId();
283 fglorieux 1.42 setId(id);
284    
285     /*
286 rpandey 1.27 //at this point we have the atts object of the <sdx:document> element
287 rpandey 1.30 //TODOException?:what if this att's object is null, i assume we need an exception here?-rbp
288 rpandey 1.21 if (atts != null) {
289 fglorieux 1.41 String docId;
290 rpandey 1.21 //indicates whether the existing document id should be overridden, default true
291 fglorieux 1.38 boolean overrideId = false;
292 rpandey 1.21 //if the att does not exist we keep the true setting, if it does and is equal to false we change it
293 fglorieux 1.38 if (atts.getValue("overrideId") != null && !atts.getValue("overrideId").equalsIgnoreCase("false"))
294     overrideId = true;
295 rpandey 1.21
296 rpandey 1.23 // If we don't override and don't already have an id
297 fglorieux 1.22 // we still want an id...
298 rpandey 1.23 // if ( getId() == null ) overrideId = true;
299 rpandey 1.27 if (!Utilities.checkString(getId())) overrideId = true;
300 fglorieux 1.22
301 rpandey 1.24 //indicates whether an id should be generated , default true
302 fglorieux 1.38 boolean generateId = true;
303 rpandey 1.21 //if the att does not exist we keep the true setting, if it does and is equal to false we change it
304 fglorieux 1.38 if (atts.getValue("generateId") != null && atts.getValue("generateId").equalsIgnoreCase("false"))
305     generateId = false;
306 rpandey 1.21
307     //we generate if we have the appropriate value, this is an override, so we must check that too
308     if (generateId && overrideId) {
309     //generating an id, passing in the value of the generator attribute if it exists
310 rpandey 1.34 docId = generateId();
311     if (Utilities.checkString(docId)) setId(docId); //if we have a good doc idea after generation we set it
312 rpandey 1.21 } else if (overrideId) {
313     //we override any existing id's
314     //getting the value generated from the xslt tranformation, if it is not null or an empty string? is this ok
315 rpandey 1.73 if (Utilities.checkString(atts.getValue(ConfigurationNode.Name.ID))) {
316     docId = atts.getValue(ConfigurationNode.Name.ID);
317 rpandey 1.21 //setting the id's
318     setId(docId);
319     }
320     }
321 fglorieux 1.41
322 fglorieux 1.22 // we verify the document to see if we have at least a valid unique id for our
323     // internal purposes, if so we all is well, otherwise we throw an SDXException
324 rpandey 1.72 Utilities.checkDocument(super.getLog(), this);
325 rpandey 1.15 }
326 fglorieux 1.42 */
327 rpandey 1.15
328     }
329    
330 rpandey 1.49 protected String generateId() throws SDXException {
331     //TODO: handle a IDGenerator from the attributes
332 rpandey 1.34 String newId = "";
333 rpandey 1.15
334 rpandey 1.34 //we get the specified generator by id
335     IDGenerator idGen = this.idGenerator;
336    
337     if (idGen == null) {
338     //log a warn message saying if generator specified, could not retrieve the specified generator and continue
339 rpandey 1.72 SDXException sdxE = new SDXException(super.getLog(), SDXExceptionCode.ERROR_USING_DEFAULT_GENERATOR, null, null);
340     LoggingUtils.logWarn(super.getLog(), sdxE.getMessage(), sdxE);
341 rpandey 1.34 //we get the SDX default id generator
342     idGen = this.idGenerator = new DefaultIDGenerator();
343     }
344 rpandey 1.15
345 rpandey 1.34 //could not get a generator, we throw an exception
346 rpandey 1.72 if (idGen == null) throw new SDXException(super.getLog(), SDXExceptionCode.ERROR_NO_ID_GENERATOR, null, null);
347 rpandey 1.15
348 rpandey 1.65 //if we have a specified prefix and suffix we use it
349     if (Utilities.checkString(this.idPrefix) || Utilities.checkString(this.idSuffix))
350     newId = idGen.generate(this.idPrefix, this.idSuffix);
351     else//otherwise we use the prefix and suffix already set in the id generator
352     newId = idGen.generate();
353 rpandey 1.15
354     return newId;
355    
356    
357 rpandey 1.8 }
358    
359 rpandey 1.45 public void characters(char[] ch, int start, int length) throws SAXException {
360 rpandey 1.47 if (this.openSdxDocElems > 1) {
361     if (this.withinSdxElement && this.subDoc != null)
362     this.subDoc.characters(ch, start, length);
363     else if (!this.withinSdxElement) {
364     if (this.openSdxDocElems == 2 && this.xmlConsumer != null)
365     this.xmlConsumer.characters(ch, start, length);
366     else if (this.subDoc != null) this.subDoc.characters(ch, start, length);
367     }
368     } else if (characterBuffer != null) characterBuffer.append(ch, start, length);
369 msevigny 1.1 }
370    
371 rpandey 1.20 /**Currently has no function*/
372 msevigny 1.1 public void comment(char[] chars, int i, int i1) throws SAXException {
373     }
374    
375 rpandey 1.20 /**Currently has no function*/
376 msevigny 1.1 public void endCDATA() throws SAXException {
377     }
378    
379 rpandey 1.20 /**Currently has no function*/
380 msevigny 1.1 public void endDTD() throws SAXException {
381     }
382    
383 rpandey 1.20 /**Currently has no function*/
384 msevigny 1.1 public void endDocument() throws SAXException {
385     }
386    
387 rpandey 1.20 /**Currently has no function*/
388 msevigny 1.1 public void endEntity(String s) throws SAXException {
389     }
390    
391 rpandey 1.20 /**Currently has no function*/
392 msevigny 1.1 public void endPrefixMapping(String s) throws SAXException {
393 rpandey 1.49 if (this.openSdxDocElems > 1) {
394     if (this.withinSdxElement && this.subDoc != null)
395     this.subDoc.endPrefixMapping(s);
396     else if (!this.withinSdxElement) {
397     if (this.openSdxDocElems == 2 && this.xmlConsumer != null)
398     this.xmlConsumer.endPrefixMapping(s);
399     else if (this.subDoc != null) this.subDoc.endPrefixMapping(s);
400     }
401     }
402 rpandey 1.52 if (this.nsTable != null)
403     this.nsTable.removeParameter(s);
404    
405 rpandey 1.54 }
406 msevigny 1.1
407 rpandey 1.20 /**Currently has no function*/
408 msevigny 1.1 public void ignorableWhitespace(char[] chars, int i, int i1) throws SAXException {
409     }
410    
411 rpandey 1.20 /**Currently has no function*/
412 msevigny 1.1 public void processingInstruction(String s, String s1) throws SAXException {
413     }
414    
415 rpandey 1.20 /**Currently has no function*/
416 msevigny 1.1 public void setDocumentLocator(Locator locator) {
417     }
418    
419 rpandey 1.20 /**Currently has no function*/
420 msevigny 1.1 public void skippedEntity(String s) throws SAXException {
421     }
422    
423 rpandey 1.20 /**Currently has no function*/
424 msevigny 1.1 public void startCDATA() throws SAXException {
425     }
426    
427 rpandey 1.20 /**Currently has no function*/
428 msevigny 1.1 public void startDTD(String s, String s1, String s2) throws SAXException {
429     }
430    
431 rpandey 1.20 /**Currently has no function*/
432 msevigny 1.1 public void startDocument() throws SAXException {
433     }
434    
435 rpandey 1.20 /**Currently has no function*/
436 msevigny 1.1 public void startEntity(String s) throws SAXException {
437     }
438    
439 rpandey 1.20 /**Currently has no function*/
440 msevigny 1.1 public void startPrefixMapping(String s, String s1) throws SAXException {
441 rpandey 1.52 if (this.nsTable == null) this.nsTable = new Parameters();
442     if (!s1.equals(Framework.SDXNamespaceURI))//if it isnt our sdx namespace mapping we add it to the table
443     this.nsTable.setParameter(s, s1);
444 rpandey 1.49 if (this.openSdxDocElems > 1) {
445     if (this.withinSdxElement && this.subDoc != null)
446     this.subDoc.startPrefixMapping(s, s1);
447     else if (!this.withinSdxElement) {
448     if (this.openSdxDocElems == 2 && this.xmlConsumer != null)
449     this.xmlConsumer.startPrefixMapping(s, s1);
450     else if (this.subDoc != null) this.subDoc.startPrefixMapping(s, s1);
451     }
452     }
453 msevigny 1.1 }
454    
455 rpandey 1.47 public void endElement(String nsURI, String name, String qName) throws SAXException {
456 rpandey 1.62 if (DOC_NAMESPACE.equals(nsURI)) {
457 rpandey 1.45
458 rpandey 1.62 if (DOC_FIELD_ELEMENT_NAME.equals(name)) {
459 rpandey 1.45
460     if (this.openSdxDocElems > 1 && this.subDoc != null)
461     this.subDoc.endElement(nsURI, name, qName);
462    
463 rpandey 1.68 else if (properties != null && currentFieldName != null && characterBuffer != null && characterBuffer.length() > 0) {
464     IndexableFieldProperty currentFieldProperty = new IndexableFieldProperty();
465 rpandey 1.72 currentFieldProperty.enableLogging(super.getLog());
466 rpandey 1.68 currentFieldProperty.setName(currentFieldName);
467     currentFieldProperty.addValue(characterBuffer.toString());
468     currentFieldProperty.setBoost(this._currentFieldBoost);
469     properties.add(currentFieldProperty);
470     }
471 msevigny 1.1
472     currentFieldName = null;
473 rpandey 1.62 } else if (DOC_ATTACHEDOC_ELEMENT_NAME.equals(name)) {
474 rpandey 1.45
475 rpandey 1.47 if (this.openSdxDocElems > 1 && this.subDoc != null)
476 rpandey 1.45 this.subDoc.endElement(nsURI, name, qName);
477     }
478 msevigny 1.1 // else if ( name.equals("column") )
479     // {
480     // if ( tableName != null && tableValues != null )
481     // if ( content != null ) tableValues.put(columnName, content.toString());
482     // }
483     // else if ( name.equals("table") )
484     // {
485     // if ( tableName != null && tableValues != null )
486     // {
487     // try
488     // {
489     // addTableValue(new TableValue(tableName, tableValues));
490     // }
491     // catch ( SDXException e ) {}
492     // }
493     // tableName = null;
494     // }
495 rpandey 1.62 else if (DOC_ROOT_ELEMENT_NAME.equals(name)) {
496 rpandey 1.47 this.openSdxDocElems--;
497 rpandey 1.45
498 rpandey 1.47 if (this.openSdxDocElems >= 1) {
499     if (this.subDoc != null)
500     this.subDoc.endElement(nsURI, name, qName);
501    
502     if (this.openSdxDocElems == 1) {
503 rpandey 1.52 //ending the namespace mappings
504     endNamespaceMappings();
505    
506 rpandey 1.47 if (this.xmlConsumer != null)
507     this.xmlConsumer.endDocument();
508     //adding the subdocument to the attached docs vector
509     try {
510     if (this.subDocBytes != null && this.subDoc != null) {
511     this.subDocBytes.close();
512     this.subDoc.setContent(this.subDocBytes.toByteArray());
513     if (this.subDocuments == null) this.subDocuments = new Vector();
514     this.subDocuments.add(subDoc);
515 rpandey 1.54 this.subDoc = null;
516 rpandey 1.47 this.subDocBytes = null;
517     }
518     } catch (IOException e) {
519     throw new SAXException(e.getMessage(), e);
520 rpandey 1.45 }
521 rpandey 1.47 //sub document finished resetting the consumer
522     this.setConsumer(null);
523 rpandey 1.45 }
524 rpandey 1.47
525 rpandey 1.45 }
526    
527 rpandey 1.47
528 rpandey 1.68 } else if (DOC_MSG_ELEMENT_NAME.equals(name)) {
529 rpandey 1.72 if (super.getLog() != null && characterBuffer != null && characterBuffer.length() > 0) {
530 rpandey 1.67 String loggerId = "";
531 rpandey 1.68 String qualElemName = Framework.SDXNamespacePrefix + ":" + Node.Name.MESSAGE;
532 rpandey 1.67 if (Utilities.checkString(super.getId()))
533 rpandey 1.72 loggerId = super.getId() + ".";//trying to add a document id to the super.getLog() name
534 rpandey 1.68 loggerId += qualElemName;
535     String bufferContents = characterBuffer.toString();
536     char[] bufferChars = bufferContents.toCharArray();
537 rpandey 1.67 //loggerName.?docId?.sdx-message should appear in the logs
538 rpandey 1.72 LoggingUtils.logInfo(super.getLog().getChildLogger(loggerId), bufferContents);
539 rpandey 1.68 if (this._msgHandler != null) {
540     this._msgHandler.startElement(Framework.SDXNamespaceURI, Node.Name.MESSAGE, qualElemName, new AttributesImpl());
541     this._msgHandler.characters(bufferChars, 0, bufferChars.length);
542     this._msgHandler.endElement(Framework.SDXNamespaceURI, Node.Name.MESSAGE, qualElemName);
543     }
544 rpandey 1.67 }
545     }
546 rpandey 1.45 } else {
547     if (this.openSdxDocElems == 2 && this.xmlConsumer != null)
548     this.xmlConsumer.endElement(nsURI, name, qName);
549     else if (this.subDoc != null)
550     this.subDoc.endElement(nsURI, name, qName);
551 msevigny 1.1 }
552 rpandey 1.45
553 msevigny 1.1 characterBuffer = new StringBuffer();
554 rpandey 1.52 }
555    
556     private void startNamespaceMappings() throws SAXException {
557    
558 rpandey 1.54 String[] prefixes = null;
559 rpandey 1.52 if (this.nsTable != null) prefixes = this.nsTable.getNames();
560     if (prefixes != null) {
561     for (int i = 0; i < prefixes.length; i++) {
562     String prefix = prefixes[i];
563     String uri = null;
564     try {
565     uri = this.nsTable.getParameter(prefix);
566     } catch (ParameterException e) {
567     throw new SAXException(e.getMessage(), e);
568     }
569     if (uri != null) {
570 rpandey 1.54 if (this.subDoc != null)
571     this.subDoc.startPrefixMapping(prefix, uri);
572 rpandey 1.52 if (this.xmlConsumer != null)
573 rpandey 1.54 this.xmlConsumer.startPrefixMapping(prefix, uri);
574 rpandey 1.52 }
575     }
576     }
577    
578     }
579    
580     private void endNamespaceMappings() throws SAXException {
581    
582 rpandey 1.54 String[] prefixes = null;
583 rpandey 1.52 if (this.nsTable != null) prefixes = this.nsTable.getNames();
584     if (prefixes != null) {
585     for (int i = 0; i < prefixes.length; i++) {
586     String prefix = prefixes[i];
587     if (this.subDoc != null)
588 rpandey 1.54 this.subDoc.endPrefixMapping(prefix);
589 rpandey 1.52 if (this.xmlConsumer != null)
590 rpandey 1.54 this.xmlConsumer.endPrefixMapping(prefix);
591 rpandey 1.52 }
592     }
593    
594 msevigny 1.1 }
595    
596    
597     /** The <code>XMLConsumer</code> receiving SAX events. */
598     protected XMLConsumer xmlConsumer;
599    
600     /** The <code>ContentHandler</code> receiving SAX events. */
601     protected ContentHandler contentHandler;
602    
603     /** The <code>LexicalHandler</code> receiving SAX events. */
604     protected LexicalHandler lexicalHandler;
605    
606     /**
607     * Set the <code>XMLConsumer</code> that will receive XML data.
608     * <br>
609     * This method will simply call <code>setContentHandler(consumer)</code>
610     * and <code>setLexicalHandler(consumer)</code>.
611     */
612 rpandey 1.27 public void setConsumer(XMLConsumer consumer) {
613 rpandey 1.26 //verifying the consumer
614 msevigny 1.1 this.xmlConsumer = consumer;
615     this.contentHandler = consumer;
616     this.lexicalHandler = consumer;
617     }
618    
619     /**
620     * Set the <code>ContentHandler</code> that will receive XML data.
621     * <br>
622     * Subclasses may retrieve this <code>ContentHandler</code> instance
623     * accessing the protected <code>super.contentHandler</code> field.
624     */
625     public void setContentHandler(ContentHandler handler) {
626     this.contentHandler = handler;
627     }
628    
629     /**
630     * Set the <code>LexicalHandler</code> that will receive XML data.
631     * <br>
632     * Subclasses may retrieve this <code>LexicalHandler</code> instance
633     * accessing the protected <code>super.lexicalHandler</code> field.
634     *
635     * @exception IllegalStateException If the <code>LexicalHandler</code> or
636     * the <code>XMLConsumer</code> were
637     * already set.
638     */
639     public void setLexicalHandler(LexicalHandler handler) {
640     this.lexicalHandler = handler;
641     }
642    
643     /**
644     * Returns field values.
645     */
646     public Enumeration getFieldValues() {
647 rpandey 1.7 if (properties != null && properties.elements() != null)
648     return properties.elements();
649     else
650     return null;
651 msevigny 1.1 }
652 rpandey 1.4
653     /**
654 rpandey 1.8 * Add an attached document to the list for this document.
655     *
656     * @param id The document id.
657 msevigny 1.33 * @param baseURL The base URL, usually the parent document's URL
658 rpandey 1.8 * @param url URL of the attached document.
659     * @param mimetype Mime type of the document, can be null.
660     */
661 rpandey 1.43 public void addAttachedDocument(String id, URL baseURL, String url, String mimetype, String repoId) throws SDXException {
662 rpandey 1.8 if (id != null) {
663     if (attachedDocuments == null) attachedDocuments = new Vector();
664 rpandey 1.19 try {
665 rpandey 1.26 BinaryDocument doc = new BinaryDocument();
666 rpandey 1.72 //setting the super.getLog()
667     doc.enableLogging(super.getLog());
668 rpandey 1.26 //setting the id
669 rpandey 1.57 /*
670 rpandey 1.55 String bDocId = this.getId();
671     if (Utilities.checkString(bDocId) && Utilities.checkString(id))
672 rpandey 1.51 bDocId = bDocId + "_" + id;
673     else
674 rpandey 1.55 bDocId = id;
675 rpandey 1.57 doc.setId(bDocId); */
676     doc.setId(id);
677 rpandey 1.26 //setting the document mimetype
678     doc.setMimeType(mimetype);
679 rpandey 1.51 //setting the storage repository if provided
680     if (Utilities.checkString(repoId))
681     doc.setRepositoryForStorage(repoId);
682 rpandey 1.26 // set content
683 msevigny 1.33 // doc.setContent(new URL(url));
684     doc.setContent(Utilities.attUrl(baseURL, url));
685 rpandey 1.26 //adding the document to the vector
686     attachedDocuments.add(doc);
687     // attachedDocuments.add(new BinaryDocument(id, new URL(url), mimetype));
688 rpandey 1.19 } catch (MalformedURLException e) {
689 fglorieux 1.18 //here we should really be adding both the stack traces to one SDXException and then throw it
690     //we have a bad absolute path, when trying to build from only "url", sorry
691 rpandey 1.28 String[] args1 = new String[2];
692 fglorieux 1.18 args1[0] = getId();
693     args1[1] = url;
694     //throwing the second exception
695 rpandey 1.72 throw new SDXException(super.getLog(), SDXExceptionCode.ERROR_BUILD_DOC_URL, args1, e);
696 fglorieux 1.18
697     }
698 rpandey 1.8 }
699     }
700 rpandey 1.4
701     /**
702 rpandey 1.20 * Set's the list of attached documents for this document.
703 rpandey 1.8 *
704 rpandey 1.20 * @param list The list of attached documents.
705 rpandey 1.8 */
706     public void setAttachedDocuments(Vector list) {
707     attachedDocuments = list;
708 rpandey 1.4 }
709    
710     /**
711 rpandey 1.20 * Indicates wheter the documents must be refreshed.
712 rpandey 1.8 */
713     public boolean updateAttachedDocuments() {
714     return updateAttachedDocuments;
715     }
716 rpandey 1.4
717     /**
718 rpandey 1.20 * Indicates wheter the list of attached documents must be refreshed.
719 rpandey 1.8 *
720 rpandey 1.20 * @param updateAttachedDocuments A boolean indicator.
721 rpandey 1.8 */
722     public void setUpdateAttachedDocuments(boolean updateAttachedDocuments) {
723     this.updateAttachedDocuments = updateAttachedDocuments;
724     }
725 rpandey 1.4
726    
727     /**Retrieves an Enumeration of attached documents
728     *
729     * @return An Enumeration of "BinaryDocument" objects
730     */
731 rpandey 1.8 public Enumeration getAttachedDocuments() {
732 rpandey 1.45 if (attachedDocuments != null)
733     return attachedDocuments.elements();
734     else
735     return null;
736 rpandey 1.4 }
737 msevigny 1.1
738 rpandey 1.29 /**Resets the objects we need to store indexation data
739     * or creates them if they do not exist
740     *
741     */
742     protected void resetFields() {
743     properties = new Vector();
744     }
745 rpandey 1.69
746 rpandey 1.71 /**Reinits the Vector of attached documents*/
747     public void resetAttachedDocuments() {
748     attachedDocuments = new Vector();
749     }
750 rpandey 1.29
751 rpandey 1.30
752 rpandey 1.35 /**Returns the transformed document object or
753     * <code>null</null> if no transformed document
754     * during the indexation pipeline
755 rpandey 1.30 */
756 rpandey 1.31 public IndexableDocument getTransformedDocument() {
757 rpandey 1.30 return this.transformedDoc;
758     }
759    
760 rpandey 1.47 protected void setUpTransformedDocument() throws SDXException {
761 rpandey 1.32 //giving the transformed document the original filename and id
762 rpandey 1.46 this.transformedDoc.setId(this.getId());
763     if (Utilities.checkString(this.getPreferredFilename())) this.transformedDoc.setPreferredFilename(this.getPreferredFilename());
764     this.transformedDoc.setRepositoryForStorage(this.getRepositoryForStorage());
765    
766 rpandey 1.47 }
767    
768     /**Retrieves an Enumeration of sub(Indexable) documents
769     *
770     * @return An Enumeration of "XMLDocuments" objects
771     */
772     public Enumeration getSubDocuments() {
773     if (this.subDocuments != null)
774     return this.subDocuments.elements();
775     else
776     return null;
777 rpandey 1.30 }
778 rpandey 1.49 /*
779     protected IndexableDocument peekTopSubDoc(){
780     IndexableDocument subDoc = null;
781     if (this.subDocs.size() > 0){
782     subDoc = (IndexableDocument)subDocs.peek();
783     }
784     return subDoc;
785     }
786    
787     protected IndexableDocument popTopSubDoc(){
788     IndexableDocument subDoc = null;
789     if (this.subDocs.size() > 0){
790     subDoc = (IndexableDocument)subDocs.pop();
791     }
792     return subDoc;
793     }
794     */
795 rpandey 1.63
796 rpandey 1.64 public StoreHandler getStoreHandler() {
797 rpandey 1.63 return this.storeHandler;
798     }
799 rpandey 1.64
800 rpandey 1.63 protected StoreHandler storeHandler = new StoreHandler();
801    
802     public class StoreHandler {
803     ArrayList docsStored = new ArrayList();
804 rpandey 1.64
805     public void addDoc(Document doc) throws SDXException {
806 rpandey 1.63 Utilities.checkDocument(null, doc);
807     docsStored.add(doc);
808     }
809    
810 rpandey 1.64 public Document[] getDocs() {
811 rpandey 1.63 return (Document[]) docsStored.toArray(new Document[0]);
812     }
813    
814    
815 rpandey 1.68 }
816    
817     public void setMessageHandler(ContentHandler handler) {
818     this._msgHandler = handler;
819     }
820    
821     /** Sets a boost factor for scoring (currently Lucene specific)
822     *
823     *
824     * @see org.apache.lucene.document.Document#setBoost
825     */
826     public void setBoost(float boost) {
827     this._boost = boost;
828     }
829    
830     /** Gets a boost factor for scoring (currently Lucene specific)
831     *
832     *
833     * @see org.apache.lucene.document.Document#getBoost
834     */
835     public float getBoost() {
836     return _boost;
837 rpandey 1.63 }
838 msevigny 1.1
839     }

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26