001 /* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019 package org.apache.wiki.modules; 020 021 import java.io.BufferedInputStream; 022 import java.io.ByteArrayOutputStream; 023 import java.io.IOException; 024 import java.net.URL; 025 026 import org.jdom2.Element; 027 028 import org.apache.wiki.util.FileUtil; 029 030 /** 031 * A WikiModule describes whatever JSPWiki plugin there is: it can be a plugin, 032 * an editor, a filter, etc. 033 * 034 * @since 2.4 035 */ 036 public class WikiModuleInfo 037 implements Comparable<WikiModuleInfo> 038 { 039 protected String m_name; 040 protected String m_scriptLocation; 041 protected String m_scriptText; 042 protected String m_stylesheetLocation; 043 protected String m_stylesheetText; 044 protected String m_author; 045 protected URL m_resource; 046 protected String m_minVersion; 047 protected String m_maxVersion; 048 protected String m_adminBeanClass; 049 050 /** 051 * Create a new info container. 052 * 053 * @param name The name of the module. 054 */ 055 public WikiModuleInfo( String name ) 056 { 057 m_name = name; 058 } 059 060 /** 061 * The WikiModuleInfo is equal to another WikiModuleInfo, if the name is equal. All 062 * objects are unique across JSPWiki. 063 * 064 * @param obj {@inheritDoc} 065 * @return {@inheritDoc} 066 */ 067 @Override 068 public boolean equals(Object obj) 069 { 070 if( obj instanceof WikiModuleInfo ) 071 { 072 return ((WikiModuleInfo)obj).m_name.equals( m_name ); 073 } 074 075 return false; 076 } 077 078 /** 079 * {@inheritDoc} 080 */ 081 @Override 082 public int hashCode() 083 { 084 return m_name.hashCode(); 085 } 086 087 /** 088 * Initializes the ModuleInfo from some standard XML elements 089 * which are under the given element. 090 * 091 * @param el The element to parse. 092 */ 093 protected void initializeFromXML( Element el ) 094 { 095 m_scriptLocation = el.getChildText("script"); 096 m_stylesheetLocation = el.getChildText("stylesheet"); 097 m_author = el.getChildText("author"); 098 m_minVersion = el.getChildText("minVersion"); 099 m_maxVersion = el.getChildText("maxVersion"); 100 m_adminBeanClass = el.getChildText("adminBean"); 101 } 102 103 /** 104 * Returns the AdminBean class which is supposed to manage this module. 105 * 106 * @return A class name. 107 */ 108 public String getAdminBeanClass() 109 { 110 return m_adminBeanClass; 111 } 112 113 /** 114 * Returns the common name for this particular module. Note that 115 * this is not the class name, nor is it an alias. For different modules 116 * the name may have different meanings. 117 * <p> 118 * Every module defines a name, so this method should never return null. 119 * 120 * @return A module name. 121 */ 122 public String getName() 123 { 124 return m_name; 125 } 126 127 /** 128 * Returns the style sheet location for this module. 129 * 130 * @return The path to the location. 131 */ 132 public String getStylesheetLocation() 133 { 134 return m_stylesheetLocation; 135 } 136 137 /** 138 * Return the location of the script for this module. 139 * 140 * @return The path to the location. 141 */ 142 public String getScriptLocation() 143 { 144 return m_scriptLocation; 145 } 146 147 /** 148 * Returns the name of the author of this plugin (if defined). 149 * @return Author name, or null. 150 */ 151 public String getAuthor() 152 { 153 return m_author; 154 } 155 156 /** 157 * Returns the minimum version of JSPWiki that this module supports. 158 * 159 * @return The minimum version. 160 */ 161 public String getMinVersion() 162 { 163 return m_minVersion; 164 } 165 166 /** 167 * Returns the maximum version of JSPWiki that this module supports. 168 * 169 * @return The maximum version. 170 */ 171 public String getMaxVersion() 172 { 173 return m_maxVersion; 174 } 175 176 /** 177 * Attempts to locate a resource from a JAR file and returns it as a string. 178 * 179 * @param resourceLocation an URI of the resource 180 * @return The content of the file 181 * 182 * @throws IOException if the JAR file or the resource cannot be read 183 */ 184 protected String getTextResource(String resourceLocation) 185 throws IOException 186 { 187 if(m_resource == null) 188 { 189 return ""; 190 } 191 192 // The text of this resource should be loaded from the same 193 // jar-file as the jspwiki_modules.xml -file! This is because 2 plugins 194 // could have the same name of the resourceLocation! 195 // (2 plugins could have their stylesheet-files in 'ini/jspwiki.css') 196 197 // So try to construct a resource that loads this resource from the 198 // same jar-file. 199 String spec = m_resource.toString(); 200 201 // Replace the 'PLUGIN_RESOURCE_LOCATION' with the requested 202 // resourceLocation. 203 int length = ModuleManager.PLUGIN_RESOURCE_LOCATION.length(); 204 spec = spec.substring(0, spec.length() - length) + resourceLocation; 205 206 URL url = new URL(spec); 207 BufferedInputStream in = new BufferedInputStream(url.openStream()); 208 ByteArrayOutputStream out = new ByteArrayOutputStream(1024); 209 210 FileUtil.copyContents( in, out ); 211 212 in.close(); 213 String text = out.toString(); 214 out.close(); 215 216 return text; 217 } 218 219 /** 220 * {@inheritDoc} 221 */ 222 public int compareTo(WikiModuleInfo arg0) 223 { 224 return m_name.compareTo( arg0.getName() ); 225 } 226 227 }