001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.parser;
017:
018: import java.io.Serializable;
019: import java.util.Vector;
020: import java.util.LinkedHashMap;
021:
022: /**
023: * This class represents the output from the JAMWiki parser. It holds parsed
024: * output text as well as metadata that is generated by the parser.
025: */
026: public class ParserOutput implements Serializable {
027:
028: private boolean cacheable = true;
029: private final LinkedHashMap categories = new LinkedHashMap();
030: private final Vector links = new Vector();
031: private String redirect = null;
032: private String sectionName = null;
033: private final Vector templates = new Vector();
034:
035: /**
036: *
037: */
038: public ParserOutput() {
039: }
040:
041: /**
042: * When a document contains a token indicating that the document belongs
043: * to a specific category this method should be called to add that
044: * category to the output metadata.
045: *
046: * @param categoryName The name of the category that the document belongs
047: * to.
048: * @param sortKey The sort key for the category, or <code>null</code> if
049: * no sort key has been specified. The sort key determines what order
050: * categories are sorted on category index pages, so a category for
051: * "John Doe" might be given a sort key of "Doe, John".
052: */
053: public void addCategory(String categoryName, String sortKey) {
054: this .categories.put(categoryName, sortKey);
055: }
056:
057: /**
058: * When a document contains a token indicating that the document links
059: * to another Wiki topic this method should be called to add that
060: * topic link to the output metadata.
061: *
062: * @param topicName The name of the topic that is linked to.
063: */
064: public void addLink(String topicName) {
065: this .links.add(topicName);
066: }
067:
068: /**
069: * When a document contains a token indicating that the document includes
070: * a Wiki template this method should be called to add that template
071: * to the output metadata.
072: *
073: * @param template The name of the template that is being included.
074: */
075: public void addTemplate(String template) {
076: this .templates.add(template);
077: }
078:
079: /**
080: *
081: */
082: public void appendMetadata(ParserOutput document) {
083: if (document.getCategories() != null) {
084: this .categories.putAll(document.getCategories());
085: }
086: if (document.getLinks() != null) {
087: this .links.addAll(document.getLinks());
088: }
089: if (document.getTemplates() != null) {
090: this .links.addAll(document.getTemplates());
091: }
092: if (!document.getCacheable()) {
093: this .cacheable = false;
094: }
095: }
096:
097: /**
098: * Return a flag indicating whether or not the current ParserOutput
099: * object can be cached. If the document contains user-specific,
100: * time-specific or other non-cacheable content then this method should
101: * return <code>false</code>.
102: *
103: * @return <code>true</code> if the current ParserOutput is cacheable,
104: * <code>false</code> if it contains any non-cacheable content.
105: */
106: public boolean getCacheable() {
107: return this .cacheable;
108: }
109:
110: /**
111: * Sets a flag indicating whether or not the current ParserOutput
112: * object can be cached. If the document contains user-specific,
113: * time-specific or other non-cacheable content then the cacheable flag
114: * should be set to <code>false</code>.
115: *
116: * @param cacheable Set to <code>true</code> if the current ParserOutput
117: * is cacheable, <code>false</code> if it contains any non-cacheable
118: * content.
119: */
120: public void setCacheable(boolean cacheable) {
121: this .cacheable = cacheable;
122: }
123:
124: /**
125: * Return the current mapping of categories associated with the document
126: * being parsed. The mapping contains key-value pairs with the category
127: * name as the key and the sort key (if any) as the value.
128: *
129: * @return A mapping of categories and their associated sort keys (if any)
130: * for all categories that are associated with the document being parsed.
131: */
132: public LinkedHashMap getCategories() {
133: return this .categories;
134: }
135:
136: /**
137: * For the document being parsed, return the current collection of topic
138: * names for all topics that are linked to from the current document.
139: *
140: * @return A collection of all topic names that are linked to from the
141: * current document.
142: */
143: public Vector getLinks() {
144: return this .links;
145: }
146:
147: /**
148: * When editing or parsing a section of a document, get the name of
149: * the heading for that section.
150: *
151: * @return The name of the heading for a section of a document being
152: * parsed, or <code>null</code> if a section is not being parsed. If not
153: * <code>null</code> then the section name should be encoded for use in a
154: * URL.
155: */
156: public String getSectionName() {
157: return this .sectionName;
158: }
159:
160: /**
161: * When editing or parsing a section of a document, set the name of
162: * the heading for that section.
163: *
164: * @param sectionName The name of the heading for a section of a document
165: * being parsed, or <code>null</code> if a section is not being parsed.
166: * If not <code>null</code> then the section name should be encoded for
167: * use in a URL.
168: */
169: public void setSectionName(String sectionName) {
170: this .sectionName = sectionName;
171: }
172:
173: /**
174: * For the document being parsed, return the current collection of
175: * templates names for all templates that are included in the current
176: * document.
177: *
178: * @return A collection of all template names that are included in the
179: * current document.
180: */
181: public Vector getTemplates() {
182: return this .templates;
183: }
184:
185: /**
186: * If a document being parsed represents a redirect, return the name of
187: * the topic that this document redirects to.
188: *
189: * @return The name of the topic that this document redirects to, or
190: * <code>null</code> if the document does not represent a redirect.
191: */
192: public String getRedirect() {
193: return this .redirect;
194: }
195:
196: /**
197: * If a document being parsed represents a redirect, set the name of
198: * the topic that this document redirects to.
199: *
200: * @param redirect The name of the topic that this document redirects to,
201: * or <code>null</code> if the document does not represent a redirect.
202: */
203: public void setRedirect(String redirect) {
204: this.redirect = redirect;
205: }
206: }
|