1 40 41 package org.dspace.content.crosswalk; 42 43 import java.io.InputStream ; 44 import java.io.IOException ; 45 import java.sql.SQLException ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 import java.util.ArrayList ; 49 import java.util.HashMap ; 50 import java.util.Properties ; 51 import java.util.Enumeration ; 52 import java.io.OutputStream ; 53 import java.io.StringReader ; 54 import java.io.File ; 55 import java.io.FileInputStream ; 56 57 import java.sql.SQLException ; 58 import org.apache.log4j.Logger; 59 60 import org.dspace.core.Context; 61 import org.dspace.core.Constants; 62 import org.dspace.content.Item; 63 import org.dspace.content.DCDate; 64 import org.dspace.content.DCValue; 65 import org.dspace.content.DSpaceObject; 66 import org.dspace.authorize.AuthorizeException; 67 import org.dspace.core.ConfigurationManager; 68 import org.dspace.core.SelfNamedPlugin; 69 70 import org.jdom.*; 71 import org.jdom.output.XMLOutputter; 72 import org.jdom.output.Format; 73 import org.jdom.input.SAXBuilder; 74 import org.jdom.input.JDOMParseException; 75 import org.jdom.xpath.XPath; 76 import org.jdom.transform.XSLTransformer; 77 import org.jdom.transform.XSLTransformException; 78 79 138 public abstract class XSLTCrosswalk extends SelfNamedPlugin 139 { 140 141 private static Logger log = Logger.getLogger(XSLTCrosswalk.class); 142 143 146 public static final Namespace DIM_NS = 147 Namespace.getNamespace("dim", "http://www.dspace.org/xmlns/dspace/dim"); 148 149 150 protected final static String CONFIG_PREFIX = "crosswalk."; 151 152 private final static String CONFIG_STYLESHEET = ".stylesheet"; 153 154 160 protected static String [] makeAliases(String direction) 161 { 162 String prefix = CONFIG_PREFIX+direction+"."; 163 String suffix = CONFIG_STYLESHEET; 164 165 List aliasList = new ArrayList (); 166 Enumeration pe = ConfigurationManager.propertyNames(); 167 168 log.debug("XSLTCrosswalk: Looking for config prefix = "+prefix); 169 while (pe.hasMoreElements()) 170 { 171 String key = (String )pe.nextElement(); 172 if (key.startsWith(prefix) && key.endsWith(suffix)) 173 { 174 log.debug("Getting XSLT plugin name from config line: "+key); 175 aliasList.add(key.substring(prefix.length(), key.length()-suffix.length())); 176 } 177 } 178 return (String [])aliasList.toArray(new String [aliasList.size()]); 179 } 180 181 private XSLTransformer transformer = null; 182 private File transformerFile = null; 183 private long transformerLastModified = 0; 184 185 191 protected XSLTransformer getTransformer(String direction) 192 { 193 if (transformerFile == null) 194 { 195 String myAlias = getPluginInstanceName(); 196 if (myAlias == null) 197 { 198 log.error("Must use PluginManager to instantiate XSLTCrosswalk so the class knows its name."); 199 return null; 200 } 201 String cmPropName = CONFIG_PREFIX+direction+"."+myAlias+CONFIG_STYLESHEET; 202 String fname = ConfigurationManager.getProperty(cmPropName); 203 if (fname == null) 204 { 205 log.error("Missing configuration filename for XSLT-based crosswalk: no "+ 206 "value for property = "+cmPropName); 207 return null; 208 } 209 else 210 { 211 String parent = ConfigurationManager.getProperty("dspace.dir") + 212 File.separator + "config" + File.separator; 213 transformerFile = new File (parent, fname); 214 } 215 } 216 217 if (transformer == null || 219 transformerFile.lastModified() > transformerLastModified) 220 { 221 try 222 { 223 log.debug((transformer == null ? "Loading " : "Reloading")+ 224 getPluginInstanceName()+" XSLT stylesheet from "+transformerFile.toString()); 225 transformer = new XSLTransformer(transformerFile); 226 transformerLastModified = transformerFile.lastModified(); 227 } 228 catch (XSLTransformException e) 229 { 230 log.error("Failed to initialize XSLTCrosswalk("+getPluginInstanceName()+"):"+e.toString()); 231 } 232 } 233 return transformer; 234 } 235 } 236 | Popular Tags |