001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.lucefix;
021:
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.Vector;
025:
026: import org.apache.lucene.document.Document;
027: import org.xml.sax.Attributes;
028: import org.xml.sax.SAXException;
029: import org.xml.sax.ext.LexicalHandler;
030: import org.xml.sax.helpers.DefaultHandler;
031:
032: import de.schlund.pfixxml.resources.DocrootResource;
033: import de.schlund.pfixxml.resources.FileResource;
034:
035: /**
036: * @author schuppi
037: * @date Jun 7, 2005
038: */
039: public class IncludeFileHandler extends DefaultHandler implements
040: LexicalHandler {
041:
042: public IncludeFileHandler(FileResource file) {
043: this .lasttouch = file.lastModified();
044: if (file instanceof DocrootResource) {
045: this .path = ((DocrootResource) file).getRelativePath();
046: } else {
047: throw new IllegalArgumentException(
048: "Parameter is non-docroot path: " + file.toURI());
049: }
050: alleDokumente = new Vector<PreDoc>();
051: }
052:
053: private static final String PRODUCT = "theme";
054: private static final String PART = "part";
055: private static final String INCLUDE_PARTS = "include_parts";
056:
057: private int includepart_count = 0;
058: private int part_count = 0;
059: private int product_count = 0;
060: private Part currentPart = null;
061: private PreDoc currentDoc = null;
062: private Collection<PreDoc> alleDokumente = null;
063: private String path;
064: private long lasttouch;
065:
066: public void comment(char[] arg0, int arg1, int arg2)
067: throws SAXException {
068: if (currentDoc != null)
069: for (int i = 0; i < arg0.length; i++) {
070: currentDoc.addComment(arg0[i]);
071: }
072: }
073:
074: public void startElement(String uri, String name, String qName,
075: Attributes atts) throws SAXException {
076: super .startElement(uri, name, qName, atts);
077: String internal = qName;
078: if (INCLUDE_PARTS.equals(internal)) {
079: includepart_count++;
080: return;
081: } else if (PART.equals(internal)) {
082: part_count++;
083: if (part_count == 1) {
084: currentPart = new Part(path, atts.getValue("name"));
085: return;
086: }
087: } else if (PRODUCT.equals(internal)) {
088: product_count++;
089: if (product_count == 1) {
090: currentDoc = new PreDoc(currentPart.getFilename(),
091: currentPart.getName(), atts.getValue("name"),
092: lasttouch);
093: return;
094: }
095: }
096: if (currentDoc != null) {
097: // if we are here, this is just a plain odd element
098: currentDoc.addTag(internal);
099: addAttribs(atts);
100: }
101: }
102:
103: private void addAttribs(Attributes atts) {
104: for (int i = 0; i < atts.getLength(); i++) {
105: currentDoc.addAttribKey(atts.getLocalName(i));
106: currentDoc.addAttribValue(atts.getValue(i));
107: }
108: }
109:
110: public Document[] getScannedDocuments() {
111: return (Document[]) getScannedDocumentsAsVector().toArray(
112: new Document[0]);
113: }
114:
115: public Vector<Document> getScannedDocumentsAsVector() {
116: Vector<Document> retval = new Vector<Document>();
117: for (Iterator<PreDoc> iter = alleDokumente.iterator(); iter
118: .hasNext();) {
119: PreDoc element = iter.next();
120: retval.add(element.toLuceneDocument());
121: }
122: return retval;
123: }
124:
125: public void endElement(String uri, String name, String qName)
126: throws SAXException {
127: super .endElement(uri, name, qName);
128: String internal = qName;
129: if (INCLUDE_PARTS.equals(internal)) {
130: includepart_count--;
131: } else if (PART.equals(internal)) {
132: part_count--;
133: } else if (PRODUCT.equals(internal)) {
134: product_count--;
135: if (product_count == 0) {
136: alleDokumente.add(currentDoc);
137: }
138: }
139: }
140:
141: public void characters(char ch[], int start, int length) {
142: for (int i = start; i < start + length; i++) {
143: switch (ch[i]) {
144: case '\\':
145: break;
146: case '"':
147: break;
148: case '\n':
149: break;
150: case '\r':
151: break;
152: case '\t':
153: break;
154: default:
155: if (currentDoc != null)
156: currentDoc.addContent(ch[i]);
157: break;
158: }
159: }
160: }
161:
162: public void endCDATA() throws SAXException {
163: }
164:
165: public void endDTD() throws SAXException {
166: }
167:
168: public void endEntity(String arg0) throws SAXException {
169: }
170:
171: public void startCDATA() throws SAXException {
172: }
173:
174: public void startDTD(String arg0, String arg1, String arg2)
175: throws SAXException {
176: }
177:
178: public void startEntity(String arg0) throws SAXException {
179: }
180:
181: }
|