001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.modules;
021:
022: import java.io.BufferedInputStream;
023: import java.io.ByteArrayOutputStream;
024: import java.io.IOException;
025: import java.net.URL;
026:
027: import org.jdom.Element;
028:
029: import com.ecyrd.jspwiki.FileUtil;
030:
031: /**
032: * A WikiModule describes whatever JSPWiki plugin there is: it can be a plugin,
033: * an editor, a filter, etc.
034: * @author jalkanen
035: * @since 2.4
036: */
037: public class WikiModuleInfo implements Comparable {
038: protected String m_name;
039: protected String m_scriptLocation;
040: protected String m_scriptText;
041: protected String m_stylesheetLocation;
042: protected String m_stylesheetText;
043: protected String m_author;
044: protected URL m_resource;
045: protected String m_minVersion;
046: protected String m_maxVersion;
047: protected String m_adminBeanClass;
048:
049: public WikiModuleInfo(String name) {
050: m_name = name;
051: }
052:
053: /**
054: * The WikiModuleInfo is equal to another WikiModuleInfo, if the name is equal. All
055: * objects are unique across JSPWiki.
056: */
057: public boolean equals(Object obj) {
058: if (obj instanceof WikiModuleInfo) {
059: return ((WikiModuleInfo) obj).m_name.equals(m_name);
060: }
061:
062: return false;
063: }
064:
065: public int hashCode() {
066: return m_name.hashCode();
067: }
068:
069: protected void initializeFromXML(Element el) {
070: m_scriptLocation = el.getChildText("script");
071: m_stylesheetLocation = el.getChildText("stylesheet");
072: m_author = el.getChildText("author");
073: m_minVersion = el.getChildText("minVersion");
074: m_maxVersion = el.getChildText("maxVersion");
075: m_adminBeanClass = el.getChildText("adminBean");
076: }
077:
078: public String getAdminBeanClass() {
079: return m_adminBeanClass;
080: }
081:
082: /**
083: * Returns the common name for this particular module. Note that
084: * this is not the class name, nor is it an alias. For different modules
085: * the name may have different meanings.
086: * <p>
087: * Every module defines a name, so this method should never return null.
088: *
089: * @return A module name.
090: */
091: public String getName() {
092: return m_name;
093: }
094:
095: public String getStylesheetLocation() {
096: return m_stylesheetLocation;
097: }
098:
099: public String getScriptLocation() {
100: return m_scriptLocation;
101: }
102:
103: /**
104: * Returns the name of the author of this plugin (if defined).
105: * @return Author name, or null.
106: */
107: public String getAuthor() {
108: return m_author;
109: }
110:
111: public String getMinVersion() {
112: return m_minVersion;
113: }
114:
115: public String getMaxVersion() {
116: return m_maxVersion;
117: }
118:
119: protected String getTextResource(String resourceLocation)
120: throws IOException {
121: if (m_resource == null) {
122: return "";
123: }
124:
125: // The text of this resource should be loaded from the same
126: // jar-file as the jspwiki_modules.xml -file! This is because 2 plugins
127: // could have the same name of the resourceLocation!
128: // (2 plugins could have their stylesheet-files in 'ini/jspwiki.css')
129:
130: // So try to construct a resource that loads this resource from the
131: // same jar-file.
132: String spec = m_resource.toString();
133:
134: // Replace the 'PLUGIN_RESOURCE_LOCATION' with the requested
135: // resourceLocation.
136: int length = ModuleManager.PLUGIN_RESOURCE_LOCATION.length();
137: spec = spec.substring(0, spec.length() - length)
138: + resourceLocation;
139:
140: URL url = new URL(spec);
141: BufferedInputStream in = new BufferedInputStream(url
142: .openStream());
143: ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
144:
145: FileUtil.copyContents(in, out);
146:
147: in.close();
148: String text = out.toString();
149: out.close();
150:
151: return text;
152: }
153:
154: public int compareTo(Object arg0) {
155: if (arg0 instanceof WikiModuleInfo) {
156: return m_name.compareTo(((WikiModuleInfo) arg0).getName());
157: }
158:
159: throw new ClassCastException(arg0.getClass().getName());
160: }
161:
162: }
|