Ejemplo n.º 1
0
  private void addDCValue(Context c, Item i, String schema, Node n)
      throws TransformerException, SQLException, AuthorizeException {
    String value = getStringValue(n); // n.getNodeValue();
    // compensate for empty value getting read as "null", which won't display
    if (value == null) {
      value = "";
    }
    // //getElementData(n, "element");
    String element = getAttributeValue(n, "element");
    String qualifier = getAttributeValue(n, "qualifier"); // NodeValue();
    // //getElementData(n,
    // "qualifier");
    String language = getAttributeValue(n, "language");
    if (language != null) {
      language = language.trim();
    }

    if (!isQuiet) {
      System.out.println(
          "\tSchema: "
              + schema
              + " Element: "
              + element
              + " Qualifier: "
              + qualifier
              + " Value: "
              + value);
    }

    if ("none".equals(qualifier) || "".equals(qualifier)) {
      qualifier = null;
    }

    // if language isn't set, use the system's default value
    if (StringUtils.isEmpty(language)) {
      language = ConfigurationManager.getProperty("default.language");
    }

    // a goofy default, but there it is
    if (language == null) {
      language = "en";
    }

    if (!isTest) {
      i.addMetadata(schema, element, qualifier, language, value);
    } else {
      // If we're just test the import, let's check that the actual metadata field exists.
      MetadataSchema foundSchema = MetadataSchema.find(c, schema);

      if (foundSchema == null) {
        System.out.println("ERROR: schema '" + schema + "' was not found in the registry.");
        return;
      }

      int schemaID = foundSchema.getSchemaID();
      MetadataField foundField = MetadataField.findByElement(c, schemaID, element, qualifier);

      if (foundField == null) {
        System.out.println(
            "ERROR: Metadata field: '"
                + schema
                + "."
                + element
                + "."
                + qualifier
                + "' was not found in the registry.");
        return;
      }
    }
  }
Ejemplo n.º 2
0
 /**
  * Return true if and only if the schema has a field with the given element and qualifier pair.
  *
  * @param context dspace context
  * @param schemaID schema by ID
  * @param element element name
  * @param qualifier qualifier name
  * @return true if the field exists
  * @throws SQLException
  * @throws AuthorizeException
  */
 private static boolean hasElement(Context context, int schemaID, String element, String qualifier)
     throws SQLException, AuthorizeException {
   return MetadataField.findByElement(context, schemaID, element, qualifier) != null;
 }
  /**
   * Simple command-line rig for testing the DIM output of a stylesheet. Usage: java
   * XSLTIngestionCrosswalk <crosswalk-name> <input-file>
   */
  public static void main(String[] argv) throws Exception {
    if (argv.length < 2) {
      System.err.println("Usage:  java XSLTIngestionCrosswalk [-l] <crosswalk-name> <input-file>");
      System.exit(1);
    }

    int i = 0;
    boolean list = false;
    // skip first arg if it's the list option
    if (argv.length > 2 && argv[0].equals("-l")) {
      ++i;
      list = true;
    }
    IngestionCrosswalk xwalk =
        (IngestionCrosswalk) PluginManager.getNamedPlugin(IngestionCrosswalk.class, argv[i]);
    if (xwalk == null) {
      System.err.println(
          "Error, cannot find an IngestionCrosswalk plugin for: \"" + argv[i] + "\"");
      System.exit(1);
    }

    XSLTransformer xform = ((XSLTIngestionCrosswalk) xwalk).getTransformer(DIRECTION);
    if (xform == null)
      throw new CrosswalkInternalException(
          "Failed to initialize transformer, probably error loading stylesheet.");

    SAXBuilder builder = new SAXBuilder();
    Document inDoc = builder.build(new FileInputStream(argv[i + 1]));
    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    Document dimDoc = null;
    List dimList = null;
    if (list) {
      dimList = xform.transform(inDoc.getRootElement().getChildren());
      outputter.output(dimList, System.out);
    } else {
      dimDoc = xform.transform(inDoc);
      outputter.output(dimDoc, System.out);
      dimList = dimDoc.getRootElement().getChildren();
    }

    // Sanity-check the generated DIM, make sure it would load.
    Context context = new Context();
    Iterator di = dimList.iterator();
    while (di.hasNext()) {
      // skip over comment, text and other trash some XSLs generate..
      Object o = di.next();
      if (!(o instanceof Element)) continue;

      Element elt = (Element) o;
      if (elt.getName().equals("field") && elt.getNamespace().equals(DIM_NS)) {
        String schema = elt.getAttributeValue("mdschema");
        String element = elt.getAttributeValue("element");
        String qualifier = elt.getAttributeValue("qualifier");
        MetadataSchema ms = MetadataSchema.find(context, schema);
        if (ms == null) {
          System.err.println(
              "DIM Error, Cannot find metadata schema for: schema=\""
                  + schema
                  + "\" (... element=\""
                  + element
                  + "\", qualifier=\""
                  + qualifier
                  + "\")");
        } else {
          if (qualifier != null && qualifier.equals("")) {
            System.err.println(
                "DIM Warning, qualifier is empty string: "
                    + " schema=\""
                    + schema
                    + "\", element=\""
                    + element
                    + "\", qualifier=\""
                    + qualifier
                    + "\"");
            qualifier = null;
          }
          MetadataField mf =
              MetadataField.findByElement(context, ms.getSchemaID(), element, qualifier);
          if (mf == null)
            System.err.println(
                "DIM Error, Cannot find metadata field for: schema=\""
                    + schema
                    + "\", element=\""
                    + element
                    + "\", qualifier=\""
                    + qualifier
                    + "\"");
        }
      } else {
        // ("Got unexpected element in DIM list: "+elt.toString());
        throw new MetadataValidationException(
            "Got unexpected element in DIM list: " + elt.toString());
      }
    }
  }