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.content.MetadataField; 67 import org.dspace.content.MetadataSchema; 68 import org.dspace.authorize.AuthorizeException; 69 import org.dspace.core.ConfigurationManager; 70 import org.dspace.core.SelfNamedPlugin; 71 import org.dspace.core.PluginManager; 72 73 import org.jdom.*; 74 import org.jdom.output.XMLOutputter; 75 import org.jdom.output.Format; 76 import org.jdom.input.SAXBuilder; 77 import org.jdom.input.JDOMParseException; 78 import org.jdom.xpath.XPath; 79 import org.jdom.transform.XSLTransformer; 80 import org.jdom.transform.XSLTransformException; 81 82 91 public class XSLTIngestionCrosswalk 92 extends XSLTCrosswalk 93 implements IngestionCrosswalk 94 { 95 96 private static Logger log = Logger.getLogger(XSLTIngestionCrosswalk.class); 97 98 private final static String DIRECTION = "submission"; 99 100 private static String aliases[] = makeAliases(DIRECTION); 101 102 public static String [] getPluginNames() 103 { 104 return aliases; 105 } 106 107 private void applyDim(List dimList, Item item) 109 throws MetadataValidationException 110 { 111 Iterator di = dimList.iterator(); 112 while (di.hasNext()) 113 { 114 Element elt = (Element)di.next(); 115 if (elt.getName().equals("field") && elt.getNamespace().equals(DIM_NS)) 116 applyDimField(elt, item); 117 118 else if (elt.getName().equals("dim") && elt.getNamespace().equals(DIM_NS)) 120 applyDim(elt.getChildren(), item); 121 122 else 123 { 124 log.error("Got unexpected element in DIM list: "+elt.toString()); 125 throw new MetadataValidationException("Got unexpected element in DIM list: "+elt.toString()); 126 } 127 } 128 } 129 130 private void applyDimField(Element field, Item item) 132 { 133 String schema = field.getAttributeValue("mdschema"); 134 String element = field.getAttributeValue("element"); 135 String qualifier = field.getAttributeValue("qualifier"); 136 String lang = field.getAttributeValue("lang"); 137 138 item.addMetadata(schema, element, qualifier, lang, field.getText()); 139 } 140 141 147 public void ingest(Context context, DSpaceObject dso, List metadata) 148 throws CrosswalkException, 149 IOException , SQLException , AuthorizeException 150 { 151 if (dso.getType() != Constants.ITEM) 152 throw new CrosswalkObjectNotSupported("XsltSubmissionionCrosswalk can only crosswalk to an Item."); 153 Item item = (Item)dso; 154 155 XSLTransformer xform = getTransformer(DIRECTION); 156 if (xform == null) 157 throw new CrosswalkInternalException("Failed to initialize transformer, probably error loading stylesheet."); 158 try 159 { 160 List dimList = xform.transform(metadata); 161 applyDim(dimList, item); 162 } 163 catch (XSLTransformException e) 164 { 165 log.error("Got error: "+e.toString()); 166 throw new CrosswalkInternalException("XSL Transformation failed: "+e.toString()); 167 } 168 } 169 170 175 public void ingest(Context context, DSpaceObject dso, Element root) 176 throws CrosswalkException, IOException , SQLException , AuthorizeException 177 { 178 if (dso.getType() != Constants.ITEM) 179 throw new CrosswalkObjectNotSupported("XsltSubmissionionCrosswalk can only crosswalk to an Item."); 180 Item item = (Item)dso; 181 182 XSLTransformer xform = getTransformer(DIRECTION); 183 if (xform == null) 184 throw new CrosswalkInternalException("Failed to initialize transformer, probably error loading stylesheet."); 185 try 186 { 187 Document dimDoc = xform.transform(new Document((Element)root.clone())); 188 applyDim(dimDoc.getRootElement().getChildren(), item); 189 } 190 catch (XSLTransformException e) 191 { 192 log.error("Got error: "+e.toString()); 193 throw new CrosswalkInternalException("XSL Transformation failed: "+e.toString()); 194 } 195 196 } 197 198 202 public static void main(String [] argv) throws Exception 203 { 204 if (argv.length < 2) 205 { 206 System.err.println("Usage: java XSLTIngestionCrosswalk [-l] <crosswalk-name> <input-file>"); 207 System.exit(1); 208 } 209 210 int i = 0; 211 boolean list = false; 212 if (argv.length > 2 && argv[0].equals("-l")) 214 { 215 ++i; 216 list = true; 217 } 218 IngestionCrosswalk xwalk = (IngestionCrosswalk)PluginManager.getNamedPlugin( 219 IngestionCrosswalk.class, argv[i]); 220 if (xwalk == null) 221 { 222 System.err.println("Error, cannot find an IngestionCrosswalk plugin for: \""+argv[i]+"\""); 223 System.exit(1); 224 } 225 226 XSLTransformer xform = ((XSLTIngestionCrosswalk)xwalk).getTransformer(DIRECTION); 227 if (xform == null) 228 throw new CrosswalkInternalException("Failed to initialize transformer, probably error loading stylesheet."); 229 230 SAXBuilder builder = new SAXBuilder(); 231 Document inDoc = builder.build(new FileInputStream (argv[i+1])); 232 XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); 233 Document dimDoc = null; 234 List dimList = null; 235 if (list) 236 { 237 dimList = xform.transform(inDoc.getRootElement().getChildren()); 238 outputter.output(dimList, System.out); 239 } 240 else 241 { 242 dimDoc = xform.transform(inDoc); 243 outputter.output(dimDoc, System.out); 244 dimList = dimDoc.getRootElement().getChildren(); 245 } 246 247 Context context = new Context(); 249 Iterator di = dimList.iterator(); 250 while (di.hasNext()) 251 { 252 Object o = di.next(); 254 if (!(o instanceof Element)) 255 continue; 256 257 Element elt = (Element)o; 258 if (elt.getName().equals("field") && elt.getNamespace().equals(DIM_NS)) 259 { 260 String schema = elt.getAttributeValue("mdschema"); 261 String element = elt.getAttributeValue("element"); 262 String qualifier = elt.getAttributeValue("qualifier"); 263 MetadataSchema ms = MetadataSchema.find(context, schema); 264 if (ms == null ) 265 { 266 System.err.println("DIM Error, Cannot find metadata schema for: schema=\""+schema+ 267 "\" (... element=\""+element+"\", qualifier=\""+qualifier+"\")"); 268 } 269 else 270 { 271 if (qualifier != null && qualifier.equals("")) 272 { 273 System.err.println("DIM Warning, qualifier is empty string: "+ 274 " schema=\""+schema+"\", element=\""+element+"\", qualifier=\""+qualifier+"\""); 275 qualifier = null; 276 } 277 MetadataField mf = MetadataField.findByElement(context, 278 ms.getSchemaID(), element, qualifier); 279 if (mf == null) 280 System.err.println("DIM Error, Cannot find metadata field for: schema=\""+schema+ 281 "\", element=\""+element+"\", qualifier=\""+qualifier+"\""); 282 } 283 } 284 else 285 { 286 throw new MetadataValidationException("Got unexpected element in DIM list: "+elt.toString()); 288 } 289 } 290 } 291 292 } 293 | Popular Tags |