Hello guys; I am posting a batch plugin I wrote to help out with a specific problem I had. Again, hope this helps someone.
Hippo guys, maybe you want to pick this up and give it a good cleanup? It does a very specific task; read the last modificationdate, and injects that into the content at the node /document/content/datum. This is a very specific bit of code that would need tweaking for anyone else to use (usefully), but its pretty easy to change. Sorry for all the System.outs and hard codedness - not great code by any strech. No warranty or guarantees! Toolman
/* * Copyright 2001-2006 Hippo (www.hippo.nl) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package nl.hippo.webdav.batchprocessor.datumInject; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.StringWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.w3c.dom.Text; import nl.hippo.webdav.batchprocessor.Configuration; import nl.hippo.webdav.batchprocessor.Node; import nl.hippo.webdav.batchprocessor.NonCollectionPlugin; import nl.hippo.webdav.batchprocessor.OperationOnDeletedNodeException; import nl.hippo.webdav.batchprocessor.PluginConfiguration; import nl.hippo.webdav.batchprocessor.ProcessingException; import nl.hippo.webdav.batchprocessor.WebdavBatchProcessor; public class DatumInject extends NonCollectionPlugin { private WebdavBatchProcessor m_processor; private Configuration m_configuration; private static int doccount = 0; private List m_properties = new ArrayList(); private Set types = new HashSet(); public DatumInject() { super(); } public void configure(WebdavBatchProcessor processor, Configuration configuration, PluginConfiguration pluginConfiguration) { m_processor = processor; m_configuration = configuration; } public boolean requiresNodeOrderPreservation() { return false; } protected void processNonCollection(Node node) throws OperationOnDeletedNodeException, ProcessingException { synchronized (this) { doccount++; } boolean favorMeta = false; boolean alreadyCorrectDate = false; System.out.println(doccount + " : " + node.getUri()); String nodeType = node.getProperty("http://hippo.nl/cms/1.0", "type").getPropertyAsString(); System.out.println(">" + nodeType + "<"); if ("campagne".equals(nodeType) || "comment".equals(nodeType) || "match".equals(nodeType) || "news".equals(nodeType) || "person".equals(nodeType) || "statement".equals(nodeType)) { types.add(nodeType); // System.out.print("Attempting date recovery: "); String dateString = node.getProperty("DAV:", "modificationdate").getPropertyAsString(); // System.out.print(dateString + " "); if (dateString != null && dateString.length() >= "yyyy-MM-ddTHH:mm".length()) { dateString = dateString.substring(0, "yyyy-MM-ddTHH:mm".length()) + ":00.000Z"; // System.out.println(" passed, value: " + dateString); } else { dateString = "2008-05-01T00:00:00.000Z"; // System.out.println(" failed, using default: " + dateString); } // update content try { byte[] contents = node.getContents(); // System.out.println("PRE: " + new String(contents)); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(contents); Document doc = builder.parse(byteArrayInputStream); org.w3c.dom.Node root = doc.getDocumentElement(); // System.out.print(root.getNodeName() + ", "); NodeList childNodes = root.getChildNodes(); for (int childIndex = 0; childIndex < childNodes.getLength(); childIndex++) { org.w3c.dom.Node childNode = childNodes.item(childIndex); if ("content".equals(childNode.getNodeName())) { Element newGrandChild = null; NodeList grandChildNodes = childNode.getChildNodes(); for (int grandChildIndex = 0; grandChildIndex < grandChildNodes.getLength(); grandChildIndex++) { org.w3c.dom.Node grandChildNode = grandChildNodes.item(grandChildIndex); if ("datum".equals(grandChildNode.getNodeName())) { alreadyCorrectDate = true; newGrandChild = (Element) grandChildNode; if (newGrandChild.getChildNodes().getLength() < 1 || favorMeta) { // System.out.print("replacing datum contents"); try { // trim all kids, should only be < 1 or == 1 but who knows newGrandChild.removeChild(newGrandChild.getFirstChild()); newGrandChild.removeChild(newGrandChild.getFirstChild()); newGrandChild.removeChild(newGrandChild.getFirstChild()); } catch (Exception e) { } finally { // ad date from meta Text nGCText = doc.createTextNode(dateString); newGrandChild.appendChild(nGCText); alreadyCorrectDate = false; } } break; } } if (newGrandChild == null) { // System.out.println("no datum, creating new."); newGrandChild = doc.createElement("datum"); Text nGCText = doc.createTextNode(dateString); newGrandChild.appendChild(nGCText); } childNode.insertBefore(newGrandChild, childNode.getFirstChild()); break; } } if (!alreadyCorrectDate) { Source source = new DOMSource(doc); ByteArrayOutputStream out = new ByteArrayOutputStream(); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(out); TransformerFactory xmlToStringFact = TransformerFactory.newInstance(); Transformer transformer = xmlToStringFact.newTransformer(); transformer.transform(source, result); byte[] outArray = out.toByteArray(); System.out.println("OUT: " + new String(outArray)); node.setContents(outArray); } if (false) { Iterator iterator = types.iterator(); while (iterator.hasNext()) { System.out.print(iterator.next() + ", "); } System.out.println("."); } } catch (Exception e) { System.out.println("ARRG! Bad Error follows"); System.err.println(e); } } } public void postprocess() { } }
******************************************** Hippocms-dev: Hippo CMS development public mailinglist
