From: <sp...@us...> - 2011-03-30 23:05:39
|
Revision: 7573 http://geonetwork.svn.sourceforge.net/geonetwork/?rev=7573&view=rev Author: sppigot Date: 2011-03-30 23:05:33 +0000 (Wed, 30 Mar 2011) Log Message: ----------- Add new doValidation method for harvesters to use Improve Z3950 harvester to use new doValidation method in DataManager and use categories with localization Modified Paths: -------------- sandbox/BlueNetMEST/ANZMEST/web/src/main/java/org/fao/geonet/kernel/DataManager.java sandbox/BlueNetMEST/ANZMEST/web/src/main/java/org/fao/geonet/kernel/harvest/harvester/z3950/Harvester.java Modified: sandbox/BlueNetMEST/ANZMEST/web/src/main/java/org/fao/geonet/kernel/DataManager.java =================================================================== --- sandbox/BlueNetMEST/ANZMEST/web/src/main/java/org/fao/geonet/kernel/DataManager.java 2011-03-30 13:47:27 UTC (rev 7572) +++ sandbox/BlueNetMEST/ANZMEST/web/src/main/java/org/fao/geonet/kernel/DataManager.java 2011-03-30 23:05:33 UTC (rev 7573) @@ -1,4 +1,4 @@ -//============================================================================== +//============================================================================= //=== //=== DataManager //=== @@ -71,6 +71,7 @@ import org.jdom.Attribute; import org.jdom.Document; import org.jdom.Element; +import org.jdom.JDOMException; import org.jdom.Namespace; import org.jdom.Text; import org.jdom.filter.ElementFilter; @@ -588,7 +589,7 @@ * in schema directory. * @param valTypeAndStatus */ - private Element getSchemaTronXmlReport(String schema, Element md, String lang, HashMap<String, Integer[]> valTypeAndStatus) throws Exception { + private Element getSchemaTronXmlReport(String schema, Element md, String lang, HashMap<String, Integer[]> valTypeAndStatus) { // NOTE: this method assumes that you've run enumerateTree on the // metadata @@ -657,7 +658,7 @@ * based on the xpath return by the ErrorHandler. * */ - private synchronized Element getXSDXmlReport(String schema, Element md) throws Exception { + private synchronized Element getXSDXmlReport(String schema, Element md) { // NOTE: this method assumes that enumerateTree has NOT been run on the // metadata @@ -687,7 +688,13 @@ message = "\\n" + message; //-- get the element from the xpath and add the error message to it - Element elem = Xml.selectElement(md, xpath, schemaNamespaces); + Element elem = null; + try { + Xml.selectElement(md, xpath, schemaNamespaces); + } catch (JDOMException je) { + je.printStackTrace(); + Log.error(Geonet.DATA_MANAGER,"Attach xsderror message to xpath "+xpath+" failed: "+je.getMessage()); + } if (elem != null) { String existing = elem.getAttributeValue("xsderror",Edit.NAMESPACE); if (existing != null) message = existing + message; @@ -2077,6 +2084,71 @@ } /** + * Used by harvesters that need to validate metadata. + * + * @param dbms + * @param schema + * @param id + * @param doc + * @return + * @throws Exception + */ + public boolean doValidate(Dbms dbms, String schema, String id, Document doc, String lang) { + HashMap <String, Integer[]> valTypeAndStatus = new HashMap<String, Integer[]>(); + boolean valid = true; + + if (doc.getDocType() != null) { + // if document has a doctype then validate using that + try { + Xml.validate(doc); + Integer[] results = {1, 0, 0}; + valTypeAndStatus.put("dtd", results); + } catch (Exception e) { + e.printStackTrace(); + Integer[] results = {0, 0, 0}; + valTypeAndStatus.put("dtd", results); + valid = false; + } + } else { + // do XSD validation + Element md = doc.getRootElement(); + Element xsdErrors = getXSDXmlReport(schema,md); + if (xsdErrors != null && xsdErrors.getContent().size() > 0) { + Integer[] results = {0, 0, 0}; + valTypeAndStatus.put("xsd", results); + } else { + Integer[] results = {1, 0, 0}; + valTypeAndStatus.put("xsd", results); + valid = false; + } + // then do schematron validation + Element schematronError = null; + try { + editLib.enumerateTree(md); + schematronError = getSchemaTronXmlReport(schema, md, lang, valTypeAndStatus); + editLib.removeEditingInfo(md); + } catch (Exception e) { + e.printStackTrace(); + Log.error(Geonet.DATA_MANAGER, "Could not run schematron validation on metadata "+id+": "+e.getMessage()); + valid = false; + } + if (schematronError != null && schematronError.getContent().size() > 0) { + valid = false; + } + } + + // now save the validation status + try { + saveValidationStatus(dbms, id, valTypeAndStatus, new ISODate().toString()); + } catch (Exception e) { + e.printStackTrace(); + Log.error(Geonet.DATA_MANAGER, "Could not save validation status on metadata "+id+": "+e.getMessage()); + } + + return valid; + } + + /** * Used by the validate embedded service. The validation report * is stored in the session. * Modified: sandbox/BlueNetMEST/ANZMEST/web/src/main/java/org/fao/geonet/kernel/harvest/harvester/z3950/Harvester.java =================================================================== --- sandbox/BlueNetMEST/ANZMEST/web/src/main/java/org/fao/geonet/kernel/harvest/harvester/z3950/Harvester.java 2011-03-30 13:47:27 UTC (rev 7572) +++ sandbox/BlueNetMEST/ANZMEST/web/src/main/java/org/fao/geonet/kernel/harvest/harvester/z3950/Harvester.java 2011-03-30 23:05:33 UTC (rev 7573) @@ -156,6 +156,7 @@ // -- build a map of collection code versus repository name for // -- assigning the categories Map <String,String> codes = new HashMap<String,String>(); + Map <String,String> catCodes = new HashMap<String,String>(); // -- add new category for each repository boolean addcateg = false; @@ -168,9 +169,14 @@ // create a result holder for this repository Z3950Result result = serverResults.getServerResult(repoName); - if (Xml.selectElement(categories,"record[name='"+repoName+"']") == null) { + // sanitize the name of the category + String categName = repoName.replaceAll("[^\\w]",""); + categName = categName.toLowerCase(); + catCodes.put(repoId.getAttributeValue("serverCode")+":"+repoId.getAttributeValue("code"), categName); + + if (Xml.selectElement(categories,"record[name='"+categName+"']") == null) { int newId = context.getSerialFactory().getSerial(dbms, "Categories"); - dbms.execute("INSERT INTO Categories(id, name) VALUES (?, ?)", newId, repoName); + dbms.execute("INSERT INTO Categories(id, name) VALUES (?, ?)", newId, categName); Lib.local.insert(dbms, "Categories", newId, repoName); addcateg = true; } @@ -233,28 +239,6 @@ continue; } - // validate it here if requested - if (params.validate) { - try { - // detect schema to see if its one we know - String schema = dataMan.autodetectSchema(md); - if (schema != null) { - // -- validate using GeoNetwork known schema unless the document - // -- has a doctype in which case we validate using that - if (doc.getDocType() != null) Xml.validate(doc); - else dataMan.validate(schema, md); - } else { - // -- validate using doctype/dtd or schemalocation hints - Xml.validate(doc); - } - } catch (Exception e) { - e.printStackTrace(); - log.error("Cannot validate XML, ignoring. Error was: "+e.getMessage()); - result.doesNotValidate++; - continue; // skip this one - } - } - // transform using importxslt if not none if (transformIt) { try { @@ -324,11 +308,18 @@ iId = Integer.parseInt(id); addPrivileges(id); - addCategories(id, codes.get(colCode)); + addCategories(id, catCodes.get(colCode)); dataMan.setTemplateExt(dbms, iId, "n", null); dataMan.setHarvestedExt(dbms, iId, params.uuid, params.name); + // validate it here if requested + if (params.validate) { + if (!dataMan.doValidate(dbms, schema, id, doc, context.getLanguage())) { + result.doesNotValidate++; + } + } + dataMan.indexMetadata(dbms, id); result.addedMetadata++; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |