mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
update to press release component of sample website
- adding sub navigation to press release page to generate navigation by category in xsl (demoware for virgin) - adding sub navigation to index.jsp to generate navigation by category (also demoware for virgin) - cleanup of freemarker template and all sorts of fancy xpath stuff - putting the company footer data type directly in the press release.xsd rather than having a seperate xsd. while this makes the form creation process a bit confusing, it demonstrates why the root element name field is there, and minimizes files. also forced me to clean up some major overloading of pr:company_footer. changes to form data functions - renamed getXMLDocument and getXMLDocuments to parseXMLDocument since it's now returning the document element rather than documents so that xpath traversal works (also since the document is completely useless in the context of templates) adding bsf.jar so that hopefully someday i can write extension functions in javascript within xsl templates (couldn't get it to work). git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/WCM-DEV2/root@4217 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -83,18 +83,25 @@ public abstract class AbstractRenderingEngine
|
||||
protected static String toAVMPath(final String parentAVMPath, final String path)
|
||||
{
|
||||
String parent = parentAVMPath;
|
||||
if (path != null && path.length() != 0 && path.charAt(0) == '/')
|
||||
if (path == null ||
|
||||
path.length() == 0 ||
|
||||
".".equals(path) ||
|
||||
"./".equals(path))
|
||||
{
|
||||
return parentAVMPath;
|
||||
}
|
||||
|
||||
if (path.charAt(0) == '/')
|
||||
{
|
||||
//XXXarielb this doesn't work with context paths for the website
|
||||
parent = parentAVMPath.substring(0,
|
||||
parentAVMPath.indexOf(':') +
|
||||
('/' + AVMConstants.DIR_APPBASE +
|
||||
'/' + AVMConstants.DIR_WEBAPPS).length() + 1);
|
||||
'/' + AVMConstants.DIR_WEBAPPS).length());
|
||||
}
|
||||
if (parent.endsWith("/"))
|
||||
{
|
||||
parent = parent.substring(0, parent.length() - 1);
|
||||
}
|
||||
final String result = parent + '/' + path;
|
||||
|
||||
final String result =
|
||||
parent + (parent.endsWith("/") || path.startsWith("/") ? path : '/' + path);
|
||||
LOGGER.debug("built full avmPath " + result +
|
||||
" for parent " + parentAVMPath +
|
||||
" and request path " + path);
|
||||
|
@@ -57,7 +57,7 @@ public class FormDataFunctions
|
||||
* @param avmPath a path within the avm repository.
|
||||
* @return the parsed document.
|
||||
*/
|
||||
public Document getXMLDocument(final String avmPath)
|
||||
public Document parseXMLDocument(final String avmPath)
|
||||
throws IOException,
|
||||
SAXException
|
||||
{
|
||||
@@ -85,7 +85,7 @@ public class FormDataFunctions
|
||||
* @param avmPath a path within the avm repository.
|
||||
* @return the parsed document.
|
||||
*/
|
||||
public Map<String, Document> getXMLDocuments(final String formName, final String avmPath)
|
||||
public Map<String, Document> parseXMLDocuments(final String formName, final String avmPath)
|
||||
throws IOException,
|
||||
SAXException
|
||||
{
|
||||
|
@@ -280,7 +280,7 @@ public final class FormsService
|
||||
final OutputStreamWriter out = new OutputStreamWriter(fileOut);
|
||||
|
||||
final HashMap<String, String> parameters =
|
||||
this.getOutputMethodParameters(formInstanceDataFileName,
|
||||
this.getRenderingEngineParameters(formInstanceDataFileName,
|
||||
renditionFileName,
|
||||
parentPath);
|
||||
re.generate(formInstanceData, parameters, out);
|
||||
@@ -356,7 +356,7 @@ public final class FormsService
|
||||
|
||||
final OutputStreamWriter writer = new OutputStreamWriter(out);
|
||||
final HashMap<String, String> parameters =
|
||||
this.getOutputMethodParameters(formInstanceDataFileName,
|
||||
this.getRenderingEngineParameters(formInstanceDataFileName,
|
||||
renditionFileName,
|
||||
parentPath);
|
||||
re.generate(formInstanceData, parameters, writer);
|
||||
@@ -366,15 +366,17 @@ public final class FormsService
|
||||
}
|
||||
}
|
||||
|
||||
private static HashMap<String, String> getOutputMethodParameters(final String formInstanceDataFileName,
|
||||
final String renditionFileName,
|
||||
final String parentAvmPath)
|
||||
private static HashMap<String, String> getRenderingEngineParameters(final String formInstanceDataFileName,
|
||||
final String renditionFileName,
|
||||
final String parentAvmPath)
|
||||
{
|
||||
final HashMap<String, String> parameters = new HashMap<String, String>();
|
||||
parameters.put("avm_sandbox_url", AVMConstants.buildAVMStoreUrl(parentAvmPath));
|
||||
parameters.put("form_instance_data_file_name", formInstanceDataFileName);
|
||||
parameters.put("rendition_file_name", renditionFileName);
|
||||
parameters.put("parent_path", parentAvmPath);
|
||||
final FacesContext fc = FacesContext.getCurrentInstance();
|
||||
parameters.put("request_context_path", fc.getExternalContext().getRequestContextPath());
|
||||
return parameters;
|
||||
}
|
||||
|
||||
|
@@ -23,6 +23,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.service.cmr.repository.ContentReader;
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
@@ -48,8 +49,8 @@ public class FreeMarkerRenderingEngine
|
||||
private static final Log LOGGER = LogFactory.getLog(FreeMarkerRenderingEngine.class);
|
||||
|
||||
public FreeMarkerRenderingEngine(final NodeRef nodeRef,
|
||||
final NodeService nodeService,
|
||||
final ContentService contentService)
|
||||
final NodeService nodeService,
|
||||
final ContentService contentService)
|
||||
{
|
||||
super(nodeRef, nodeService, contentService);
|
||||
}
|
||||
@@ -78,7 +79,7 @@ public class FreeMarkerRenderingEngine
|
||||
final TemplateHashModel instanceDataModel = NodeModel.wrap(xmlContent);
|
||||
|
||||
// build models for each of the extension functions
|
||||
final TemplateModel getXMLDocumentModel = new TemplateMethodModel()
|
||||
final TemplateModel parseXMLDocumentModel = new TemplateMethodModel()
|
||||
{
|
||||
public Object exec(final List args)
|
||||
throws TemplateModelException
|
||||
@@ -87,8 +88,9 @@ public class FreeMarkerRenderingEngine
|
||||
{
|
||||
final FormDataFunctions ef = FreeMarkerRenderingEngine.getFormDataFunctions();
|
||||
final String path = FreeMarkerRenderingEngine.toAVMPath(parameters.get("parent_path"),
|
||||
(String)args.get(0));
|
||||
return ef.getXMLDocument(path);
|
||||
(String)args.get(0));
|
||||
final Document d = ef.parseXMLDocument(path);
|
||||
return d != null ? d.getDocumentElement() : null;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -96,7 +98,7 @@ public class FreeMarkerRenderingEngine
|
||||
}
|
||||
}
|
||||
};
|
||||
final TemplateModel getXMLDocumentsModel = new TemplateMethodModel()
|
||||
final TemplateModel parseXMLDocumentsModel = new TemplateMethodModel()
|
||||
{
|
||||
public Object exec(final List args)
|
||||
throws TemplateModelException
|
||||
@@ -104,20 +106,35 @@ public class FreeMarkerRenderingEngine
|
||||
try
|
||||
{
|
||||
final FormDataFunctions ef = FreeMarkerRenderingEngine.getFormDataFunctions();
|
||||
final String path = FreeMarkerRenderingEngine.toAVMPath(parameters.get("parent_path"),
|
||||
args.size() == 1 ? "" : (String)args.get(1));
|
||||
final Map<String, Document> resultMap = ef.getXMLDocuments((String)args.get(0), path);
|
||||
final String path =
|
||||
FreeMarkerRenderingEngine.toAVMPath(parameters.get("parent_path"),
|
||||
args.size() == 1 ? "" : (String)args.get(1));
|
||||
final Map<String, Document> resultMap = ef.parseXMLDocuments((String)args.get(0), path);
|
||||
LOGGER.debug("received " + resultMap.size() + " documents in " + path);
|
||||
|
||||
// create a root document for rooting all the results. we do this
|
||||
// so that each document root element has a common parent node
|
||||
// and so that xpath axes work properly
|
||||
final FormsService fs = FormsService.getInstance();
|
||||
final DocumentBuilder documentBuilder = fs.getDocumentBuilder();
|
||||
final Document rootNodeDocument = documentBuilder.newDocument();
|
||||
final Element rootNodeDocumentEl =
|
||||
rootNodeDocument.createElementNS(ALFRESCO_NS,
|
||||
ALFRESCO_NS_PREFIX + ":file_list");
|
||||
rootNodeDocumentEl.setAttribute("xmlns:" + ALFRESCO_NS_PREFIX, ALFRESCO_NS);
|
||||
rootNodeDocument.appendChild(rootNodeDocumentEl);
|
||||
|
||||
final List<NodeModel> result = new ArrayList<NodeModel>(resultMap.size());
|
||||
for (Map.Entry<String, Document> e : resultMap.entrySet())
|
||||
{
|
||||
final Document d = e.getValue();
|
||||
final Element documentEl = d.getDocumentElement();
|
||||
final Element documentEl = e.getValue().getDocumentElement();
|
||||
documentEl.setAttribute("xmlns:" + ALFRESCO_NS_PREFIX, ALFRESCO_NS);
|
||||
documentEl.setAttributeNS(ALFRESCO_NS,
|
||||
ALFRESCO_NS_PREFIX + ":file-name",
|
||||
ALFRESCO_NS_PREFIX + ":file_name",
|
||||
e.getKey());
|
||||
result.add(NodeModel.wrap(d));
|
||||
final Node n = rootNodeDocument.importNode(documentEl, true);
|
||||
rootNodeDocumentEl.appendChild(n);
|
||||
result.add(NodeModel.wrap(n));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -135,13 +152,13 @@ public class FreeMarkerRenderingEngine
|
||||
public TemplateModel get(final String key)
|
||||
throws TemplateModelException
|
||||
{
|
||||
if ("getXMLDocument".equals(key))
|
||||
if ("parseXMLDocument".equals(key))
|
||||
{
|
||||
return getXMLDocumentModel;
|
||||
return parseXMLDocumentModel;
|
||||
}
|
||||
if ("getXMLDocuments".equals(key))
|
||||
if ("parseXMLDocuments".equals(key))
|
||||
{
|
||||
return getXMLDocumentsModel;
|
||||
return parseXMLDocumentsModel;
|
||||
}
|
||||
return super.get(key);
|
||||
}
|
||||
|
@@ -48,18 +48,18 @@ public class ServletContextFormDataFunctionsAdapter
|
||||
return path;
|
||||
}
|
||||
|
||||
public Document getXMLDocument(final String path)
|
||||
public Document parseXMLDocument(final String path)
|
||||
throws IOException,
|
||||
SAXException
|
||||
{
|
||||
return super.getXMLDocument(this.toAVMPath(path));
|
||||
return super.parseXMLDocument(this.toAVMPath(path));
|
||||
}
|
||||
|
||||
public Map<String, Document> getXMLDocuments(final String formName,
|
||||
final String path)
|
||||
public Map<String, Document> parseXMLDocuments(final String formName,
|
||||
final String path)
|
||||
throws IOException,
|
||||
SAXException
|
||||
{
|
||||
return super.getXMLDocuments(formName, this.toAVMPath(path));
|
||||
return super.parseXMLDocuments(formName, this.toAVMPath(path));
|
||||
}
|
||||
}
|
@@ -20,6 +20,7 @@ import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.*;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Templates;
|
||||
@@ -37,6 +38,7 @@ import org.alfresco.model.WCMModel;
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.web.forms.FormsService;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.xalan.extensions.ExpressionContext;
|
||||
@@ -46,7 +48,14 @@ import org.w3c.dom.*;
|
||||
import org.w3c.dom.traversal.NodeFilter;
|
||||
import org.w3c.dom.traversal.NodeIterator;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.apache.bsf.BSFManager;
|
||||
|
||||
/**
|
||||
* A rendering engine which uses xsl templates to generate renditions of
|
||||
* form instance data.
|
||||
*
|
||||
* @author Ariel Backenroth
|
||||
*/
|
||||
public class XSLTRenderingEngine
|
||||
extends AbstractRenderingEngine
|
||||
{
|
||||
@@ -54,8 +63,8 @@ public class XSLTRenderingEngine
|
||||
private static final Log LOGGER = LogFactory.getLog(XSLTRenderingEngine.class);
|
||||
|
||||
public XSLTRenderingEngine(final NodeRef nodeRef,
|
||||
final NodeService nodeService,
|
||||
final ContentService contentService)
|
||||
final NodeService nodeService,
|
||||
final ContentService contentService)
|
||||
{
|
||||
super(nodeRef, nodeService, contentService);
|
||||
}
|
||||
@@ -67,37 +76,60 @@ public class XSLTRenderingEngine
|
||||
return o == null ? null : XSLTRenderingEngine.toAVMPath(o.toString(), path);
|
||||
}
|
||||
|
||||
public static Document getXMLDocument(final ExpressionContext ec, final String path)
|
||||
public static Node parseXMLDocument(final ExpressionContext ec, final String path)
|
||||
throws TransformerException,
|
||||
IOException,
|
||||
SAXException
|
||||
{
|
||||
final FormDataFunctions ef = XSLTRenderingEngine.getFormDataFunctions();
|
||||
return ef.getXMLDocument(XSLTRenderingEngine.toAVMPath(ec, path));
|
||||
final Document d = ef.parseXMLDocument(XSLTRenderingEngine.toAVMPath(ec, path));
|
||||
return d != null ? d.getDocumentElement() : null;
|
||||
}
|
||||
|
||||
public static NodeIterator getXMLDocuments(final ExpressionContext ec,
|
||||
final String formName)
|
||||
public static NodeIterator parseXMLDocuments(final ExpressionContext ec,
|
||||
final String formName)
|
||||
throws TransformerException,
|
||||
IOException,
|
||||
SAXException
|
||||
{
|
||||
return XSLTRenderingEngine.getXMLDocuments(ec, formName, "");
|
||||
return XSLTRenderingEngine.parseXMLDocuments(ec, formName, "");
|
||||
}
|
||||
|
||||
public static NodeIterator getXMLDocuments(final ExpressionContext ec,
|
||||
final String formName,
|
||||
String path)
|
||||
public static NodeIterator parseXMLDocuments(final ExpressionContext ec,
|
||||
final String formName,
|
||||
String path)
|
||||
throws TransformerException,
|
||||
IOException,
|
||||
SAXException
|
||||
{
|
||||
final FormDataFunctions ef = XSLTRenderingEngine.getFormDataFunctions();
|
||||
path = XSLTRenderingEngine.toAVMPath(ec, path);
|
||||
final Map<String, Document> resultMap = ef.getXMLDocuments(formName, path);
|
||||
final Map<String, Document> resultMap = ef.parseXMLDocuments(formName, path);
|
||||
LOGGER.debug("received " + resultMap.size() + " documents in " + path);
|
||||
final List<Map.Entry<String, Document>> documents =
|
||||
new ArrayList<Map.Entry<String, Document>>(resultMap.entrySet());
|
||||
|
||||
// create a root document for rooting all the results. we do this
|
||||
// so that each document root element has a common parent node
|
||||
// and so that xpath axes work properly
|
||||
final FormsService fs = FormsService.getInstance();
|
||||
final DocumentBuilder documentBuilder = fs.getDocumentBuilder();
|
||||
final Document rootNodeDocument = documentBuilder.newDocument();
|
||||
final Element rootNodeDocumentEl =
|
||||
rootNodeDocument.createElementNS(ALFRESCO_NS,
|
||||
ALFRESCO_NS_PREFIX + ":file_list");
|
||||
rootNodeDocumentEl.setAttribute("xmlns:" + ALFRESCO_NS_PREFIX, ALFRESCO_NS);
|
||||
rootNodeDocument.appendChild(rootNodeDocumentEl);
|
||||
|
||||
final List<Node> documents = new ArrayList<Node>(resultMap.size());
|
||||
for (Map.Entry<String, Document> mapEntry : resultMap.entrySet())
|
||||
{
|
||||
final Element documentEl = mapEntry.getValue().getDocumentElement();
|
||||
documentEl.setAttributeNS(ALFRESCO_NS,
|
||||
ALFRESCO_NS_PREFIX + ":file_name",
|
||||
mapEntry.getKey());
|
||||
final Node n = rootNodeDocument.importNode(documentEl, true);
|
||||
documents.add(n);
|
||||
rootNodeDocumentEl.appendChild(n);
|
||||
}
|
||||
|
||||
return new NodeIterator()
|
||||
{
|
||||
@@ -130,8 +162,7 @@ public class XSLTRenderingEngine
|
||||
|
||||
public Node getRoot()
|
||||
{
|
||||
LOGGER.error("NodeIterator.getRoot() unexpectedly called");
|
||||
throw new UnsupportedOperationException();
|
||||
return rootNodeDocumentEl;
|
||||
}
|
||||
|
||||
public int getWhatToShow()
|
||||
@@ -147,7 +178,7 @@ public class XSLTRenderingEngine
|
||||
throw new DOMException(DOMException.INVALID_STATE_ERR, null);
|
||||
if (index == documents.size())
|
||||
return null;
|
||||
return this.getNodeAt(index++);
|
||||
return documents.get(index++);
|
||||
}
|
||||
|
||||
public Node previousNode()
|
||||
@@ -158,18 +189,7 @@ public class XSLTRenderingEngine
|
||||
throw new DOMException(DOMException.INVALID_STATE_ERR, null);
|
||||
if (index == -1)
|
||||
return null;
|
||||
return this.getNodeAt(index--);
|
||||
}
|
||||
|
||||
private Document getNodeAt(int index)
|
||||
{
|
||||
final Document d = documents.get(index).getValue();
|
||||
final Element documentEl = d.getDocumentElement();
|
||||
documentEl.setAttribute("xmlns:" + ALFRESCO_NS_PREFIX, ALFRESCO_NS);
|
||||
documentEl.setAttributeNS(ALFRESCO_NS,
|
||||
ALFRESCO_NS_PREFIX + ":file-name",
|
||||
documents.get(index).getKey());
|
||||
return d;
|
||||
return documents.get(index--);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -214,8 +234,8 @@ public class XSLTRenderingEngine
|
||||
throws IOException,
|
||||
RenderingEngine.RenderingException
|
||||
{
|
||||
// XXXarielb - dirty - fix this
|
||||
final String sandBoxUrl = (String)parameters.get("avm_store_url");
|
||||
System.setProperty("org.apache.xalan.extensions.bsf.BSFManager",
|
||||
BSFManager.class.getName());
|
||||
final TransformerFactory tf = TransformerFactory.newInstance();
|
||||
final FormsService ts = FormsService.getInstance();
|
||||
Document xslDocument = null;
|
||||
@@ -243,11 +263,17 @@ public class XSLTRenderingEngine
|
||||
LOGGER.error(tce);
|
||||
throw new RenderingEngine.RenderingException(tce);
|
||||
}
|
||||
|
||||
// create a uri resolver to resolve document() calls to the virtualized
|
||||
// web application
|
||||
t.setURIResolver(new URIResolver()
|
||||
{
|
||||
public Source resolve(final String href, final String base)
|
||||
throws TransformerException
|
||||
{
|
||||
// XXXarielb - dirty - fix this
|
||||
final String sandBoxUrl = (String)parameters.get("avm_sandbox_url");
|
||||
|
||||
URI uri = null;
|
||||
try
|
||||
{
|
||||
|
Reference in New Issue
Block a user