KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > SnipDocument


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.snip;
27
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.document.Field;
30 import org.snipsnap.snip.label.Label;
31 import org.snipsnap.snip.label.Labels;
32 import org.radeox.filter.LinkTestFilter;
33 import org.radeox.filter.context.BaseFilterContext;
34 import org.radeox.filter.context.FilterContext;
35 import org.radeox.engine.context.BaseRenderContext;
36 import org.radeox.engine.context.BaseInitialRenderContext;
37 import org.radeox.api.engine.WikiRenderEngine;
38 import org.radeox.api.engine.RenderEngine;
39 import org.radeox.api.engine.context.RenderContext;
40 import org.radeox.test.filter.mock.MockWikiRenderEngine;
41
42 import java.util.Iterator JavaDoc;
43 import java.io.Writer JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.Reader JavaDoc;
46
47 /**
48  * Prepares a snip for indexing by lucene. Lucene needs
49  * data stored in "documents" to index them. SnipDocument
50  * gets information from a Snip and sets the corresponding
51  * fields in a document, e.g. "author", "content", "title" etc.
52  *
53  * @author stephan
54  * @version $Id: SnipDocument.java 1606 2004-05-17 10:56:18Z leo $
55  */

56
57 public class SnipDocument {
58   /**
59    * Get the information of a snip wrapped in a Lucene
60    * document. Lucene can only index documents.
61    *
62    * @param snip Snip to wrap
63    * @return document Document that Lucene can index
64    */

65   public static Document Document(Snip snip) {
66     final Document doc = new Document();
67     // We need this ID to correctly remove documents from
68
// the index. This is done with hashCode because
69
// lucene has problems with Unicode in id's
70
// (at least the version used when this was written)
71
doc.add(Field.Text("id", Integer.toHexString(snip.getName().hashCode())));
72     doc.add(Field.Text("content", snip.getContent()));
73     doc.add(Field.Text("title", snip.getName()));
74     // author instead of CUser is clearer to the user
75
doc.add(Field.Text("author", snip.getCUser()));
76     doc.add(Field.Text("muser", snip.getMUser()));
77     doc.add(Field.Text("owner", snip.getOwner()));
78
79     Labels labels = snip.getLabels();
80     Iterator JavaDoc iterator = labels.getAll().iterator();
81     while (iterator.hasNext()) {
82       Label label = (Label) iterator.next();
83       label.index(doc);
84     }
85
86     LinkTestFilter linkDetector = new LinkTestFilter();
87     linkDetector.setInitialContext(new BaseInitialRenderContext());
88
89     FilterContext context = new BaseFilterContext();
90     context.setRenderContext(new BaseRenderContext());
91     context.getRenderContext().setRenderEngine(new ReferenceRenderEngine(doc));
92 // System.err.println("reference from snip '" + snip.getName() + "'");
93
linkDetector.filter(snip.getContent(), context);
94
95     return doc;
96   }
97
98   private static class ReferenceRenderEngine implements RenderEngine, WikiRenderEngine {
99     Document doc;
100
101     public ReferenceRenderEngine(Document doc) {
102       this.doc = doc;
103     }
104
105     public String JavaDoc getName() {
106       return "ReferenceRenderEngine";
107     }
108
109     public String JavaDoc render(String JavaDoc content, RenderContext context) {
110       return "";
111     }
112
113     public void render(Writer JavaDoc out, String JavaDoc content, RenderContext context) throws IOException JavaDoc {
114     }
115
116     public String JavaDoc render(Reader JavaDoc in, RenderContext context) throws IOException JavaDoc {
117       return "";
118     }
119
120     public boolean exists(String JavaDoc name) {
121       return true;
122     }
123
124     public boolean showCreate() {
125       return true;
126     }
127
128     public void appendLink(StringBuffer JavaDoc buffer, String JavaDoc name, String JavaDoc view, String JavaDoc anchor) {
129 // System.err.println("Adding reference to '"+name+"'");
130
doc.add(Field.Text("reference", name));
131     }
132
133     public void appendLink(StringBuffer JavaDoc buffer, String JavaDoc name, String JavaDoc view) {
134 // System.err.println("Adding reference to '" + name + "'");
135
doc.add(Field.Text("reference", name));
136     }
137
138     public void appendCreateLink(StringBuffer JavaDoc buffer, String JavaDoc name, String JavaDoc view) {
139 // System.err.println("Adding reference to '" + name + "'");
140
doc.add(Field.Text("reference", name));
141     }
142   }
143 }
144
Popular Tags