/* * Copyright (C) 2005 Alfresco, Inc. * * Licensed under the Mozilla Public License version 1.1 * with a permitted attribution clause. You may obtain a * copy of the License at * * http://www.alfresco.org/legal/license.txt * * 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 org.alfresco.web.templating.extension; import org.alfresco.model.WCMModel; import org.alfresco.repo.avm.AVMRemote; import org.alfresco.repo.avm.AVMRemoteInputStream; import org.alfresco.repo.domain.PropertyValue; import org.alfresco.service.cmr.avm.AVMNodeDescriptor; import org.alfresco.service.cmr.avm.AVMNotFoundException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.*; import org.xml.sax.SAXException; import javax.xml.parsers.*; import java.io.*; import java.util.Map; import java.util.HashMap; /** * Common implementation of functions called in the context of FormDataRenderers. * This uses AVMRemote rather than AVMService so that in can be used in the context * of both the alfresco webapp and the virtualization server. * * @author Ariel Backenroth */ public class ExtensionFunctions { private static final Log LOGGER = LogFactory.getLog(ExtensionFunctions.class); private static DocumentBuilder documentBuilder; private final AVMRemote avmRemote; public ExtensionFunctions(final AVMRemote avmRemote) { this.avmRemote = avmRemote; } /** * Loads and parses an xml document at the specified path using avm remote. * * @param avmPath a path within the avm repository. * @return the parsed document. */ public Document getXMLDocument(final String avmPath) throws IOException, SAXException { final DocumentBuilder db = this.getDocumentBuilder(); final InputStream istream = new AVMRemoteInputStream(this.avmRemote.getInputHandle(-1, avmPath), this.avmRemote); Document result; try { return db.parse(istream); } finally { istream.close(); } } /** * Loads and parses all xml documents at the specified path generated by the * specified form using avm remote. * * @param formName a form name * @param avmPath a path within the avm repository. * @return the parsed document. */ public Map getXMLDocuments(final String formName, final String avmPath) throws IOException, SAXException { final Map entries = this.avmRemote.getDirectoryListing(-1, avmPath); final DocumentBuilder db = this.getDocumentBuilder(); final Map result = new HashMap(); for (Map.Entry entry : entries.entrySet()) { final String entryName = entry.getKey(); AVMNodeDescriptor entryNode = entry.getValue(); if (entryNode.isFile()) { PropertyValue pv = this.avmRemote.getNodeProperty(-1, avmPath + '/' + entryName, WCMModel.PROP_FORM_DERIVED_FROM_NAME); if (pv == null || pv.getStringValue() == null || !((String)pv.getStringValue()).equals(formName)) { // it's not generated by the same template type continue; } pv = this.avmRemote.getNodeProperty(-1, avmPath + '/' + entryName, WCMModel.PROP_FORM_TRANSFORMER_DERIVED_FROM); if (pv != null) { // it's generated by a transformer (not the xml file) continue; } final InputStream istream = new AVMRemoteInputStream(this.avmRemote.getInputHandle(-1, avmPath + '/' + entryName), this.avmRemote); try { result.put(entryName, db.parse(istream)); } finally { istream.close(); } } } return result; } private static DocumentBuilder getDocumentBuilder() { if (ExtensionFunctions.documentBuilder == null) { try { final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(false); ExtensionFunctions.documentBuilder = dbf.newDocumentBuilder(); } catch (ParserConfigurationException pce) { LOGGER.error(pce); } } return ExtensionFunctions.documentBuilder; } }