Author: glandais Date: 2008-02-12 19:54:32 +0000 (Tue, 12 Feb 2008) New Revision: 874 Modified: trunk/simexplorer-is/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/factories/MetaDataFactory.java Log: Compute hash working Modified: trunk/simexplorer-is/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/factories/MetaDataFactory.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/factories/MetaDataFactory.java 2008-02-12 18:20:30 UTC (rev 873) +++ trunk/simexplorer-is/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/factories/MetaDataFactory.java 2008-02-12 19:54:32 UTC (rev 874) @@ -309,8 +309,18 @@ return is; } + /** + * Compute hash. + * + * @param xmlStream the xml stream + * + * @return the string + * + * @throws Exception the exception + */ public String computeHash(InputStream xmlStream) throws Exception { Document document = getXMLBuilder().parse(xmlStream); + int removeNodes = removeMetadatas(document); DigestGenerator digestGenerator = new DigestGenerator(); byte[] digest = digestGenerator.getDigest(document, @@ -319,23 +329,40 @@ return MD5.asHex(digest); } - private int removeMetadatas(Document document, Node node) { + /** + * Removes the metadatas. + * + * @param document the document + * @param node the node + * + * @return the int + */ + private int removeMetadatas(Node node) { int tot = 0; NodeList childNodes = node.getChildNodes(); List<Node> toRemove = new ArrayList<Node>(); + for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); if (child instanceof Element && KEY_METADATA.equals(((Element) child).getTagName())) { toRemove.add(child); - } else { - tot = tot + removeMetadatas(document, child); } } + for (Node metadata : toRemove) { node.removeChild(metadata); tot++; } + + if (tot == 0) { + childNodes = node.getChildNodes(); + for (int i = 0; i < childNodes.getLength(); i++) { + Node child = childNodes.item(i); + tot = tot + removeMetadatas(child); + } + } + return tot; }