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 106 public class XSLTDisseminationCrosswalk 107 extends XSLTCrosswalk 108 implements DisseminationCrosswalk 109 { 110 111 private static Logger log = Logger.getLogger(XSLTDisseminationCrosswalk.class); 112 113 private final static String DIRECTION = "dissemination"; 114 115 private static XMLOutputter outputPretty = new XMLOutputter(Format.getPrettyFormat()); 116 117 private static String aliases[] = makeAliases(DIRECTION); 118 119 public static String [] getPluginNames() 120 { 121 return aliases; 122 } 123 124 private String schemaLocation = null; 127 128 private Namespace namespaces[] = null; 129 130 private String rootName = null; 131 132 private boolean preferList = false; 133 134 private void init() 136 throws CrosswalkInternalException 137 { 138 if (namespaces != null || schemaLocation != null) 139 return; 140 String myAlias = getPluginInstanceName(); 141 if (myAlias == null) 142 { 143 log.error("Must use PluginManager to instantiate XSLTDisseminationCrosswalk so the class knows its name."); 144 throw new CrosswalkInternalException("Must use PluginManager to instantiate XSLTDisseminationCrosswalk so the class knows its name."); 145 } 146 147 String prefix = CONFIG_PREFIX+DIRECTION+"."+myAlias+"."; 149 150 schemaLocation = ConfigurationManager.getProperty(prefix+"schemaLocation"); 153 if (schemaLocation == null) 154 log.warn("No schemaLocation for crosswalk="+myAlias+", key="+prefix+"schemaLocation"); 155 156 else if (schemaLocation.length() > 0 && schemaLocation.indexOf(" ") < 0) 158 log.warn("Possible INVALID schemaLocation (no space found) for crosswalk="+ 159 myAlias+", key="+prefix+"schemaLocation"+ 160 "\n\tCorrect format is \"{namespace} {schema-URL}\""); 161 162 String nsPrefix = prefix + "namespace."; 165 Enumeration pe = ConfigurationManager.propertyNames(); 166 List nsList = new ArrayList (); 167 while (pe.hasMoreElements()) 168 { 169 String key = (String )pe.nextElement(); 170 if (key.startsWith(nsPrefix)) 171 nsList.add(Namespace.getNamespace(key.substring(nsPrefix.length()), 172 ConfigurationManager.getProperty(key))); 173 } 174 namespaces = (Namespace[])nsList.toArray(new Namespace[nsList.size()]); 175 176 preferList = ConfigurationManager.getBooleanProperty(prefix+"preferList", false); 177 } 178 179 public Namespace[] getNamespaces() 180 { 181 try 182 { 183 init(); 184 } 185 catch (CrosswalkInternalException e) 186 { 187 log.error(e.toString()); 188 } 189 return namespaces; 190 } 191 192 public String getSchemaLocation() 193 { 194 try 195 { 196 init(); 197 } 198 catch (CrosswalkInternalException e) 199 { 200 log.error(e.toString()); 201 } 202 return schemaLocation; 203 } 204 205 public Element disseminateElement(DSpaceObject dso) 206 throws CrosswalkException, 207 IOException , SQLException , AuthorizeException 208 { 209 init(); 210 211 if (dso.getType() != Constants.ITEM) 212 throw new CrosswalkObjectNotSupported("XSLTDisseminationCrosswalk can only crosswalk an Item."); 213 Item item = (Item)dso; 214 XSLTransformer xform = getTransformer(DIRECTION); 215 if (xform == null) 216 throw new CrosswalkInternalException("Failed to initialize transformer, probably error loading stylesheet."); 217 218 try 219 { 220 Document ddim = new Document(getDim(item)); 221 Document result = xform.transform(ddim); 222 return result.getRootElement(); 223 } 224 catch (XSLTransformException e) 225 { 226 log.error("Got error: "+e.toString()); 227 throw new CrosswalkInternalException("XSL translation failed: "+e.toString()); 228 } 229 } 230 231 public List disseminateList(DSpaceObject dso) 232 throws CrosswalkException, 233 IOException , SQLException , AuthorizeException 234 { 235 init(); 236 237 if (dso.getType() != Constants.ITEM) 238 throw new CrosswalkObjectNotSupported("XSLTDisseminationCrosswalk can only crosswalk an Item."); 239 Item item = (Item)dso; 240 XSLTransformer xform = getTransformer(DIRECTION); 241 if (xform == null) 242 throw new CrosswalkInternalException("Failed to initialize transformer, probably error loading stylesheet."); 243 244 try 245 { 246 return xform.transform(getDim(item).getChildren()); 247 } 248 catch (XSLTransformException e) 249 { 250 log.error("Got error: "+e.toString()); 251 throw new CrosswalkInternalException("XSL translation failed: "+e.toString()); 252 } 253 } 254 255 256 private Element getDim(Item item) 258 { 259 DCValue[] dc = item.getMetadata(Item.ANY, Item.ANY, Item.ANY, Item.ANY); 260 Element dim = new Element("dim", DIM_NS); 261 for (int i = 0; i < dc.length; i++) 262 { 263 Element field = new Element("field", DIM_NS); 264 field.setAttribute("mdschema", dc[i].schema); 265 field.setAttribute("element", dc[i].element); 266 if (dc[i].qualifier != null) 267 field.setAttribute("qualifier", dc[i].qualifier); 268 if (dc[i].language != null) 269 field.setAttribute("lang", dc[i].language); 270 if (dc[i].value != null) 271 field.setText(dc[i].value); 272 dim.addContent(field); 273 } 274 return dim; 275 } 276 277 public boolean canDisseminate(DSpaceObject dso) 278 { 279 return dso.getType() == Constants.ITEM; 280 } 281 282 public boolean preferList() 283 { 284 try 285 { 286 init(); 287 } 288 catch (CrosswalkInternalException e) 289 { 290 log.error(e.toString()); 291 } 292 return preferList; 293 } 294 } 295 | Popular Tags |