mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
. WCM UI
- Create Web Content action (HTML and plain text) now creates content in the current AVM store - TODO: create in the correct sub-folder - Refactoring of the modified Create Content Wizard into a new wizard Create Web Content Wizard - Integrated XML form templating into the current website AVM path (still needs some testing/fixing up…) git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/WCM-DEV2/root@3867 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -16,15 +16,13 @@
|
||||
*/
|
||||
package org.alfresco.web.templating;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.avm.AVMContext;
|
||||
import org.alfresco.service.cmr.avm.AVMExistsException;
|
||||
import org.alfresco.service.cmr.avm.AVMNotFoundException;
|
||||
import org.alfresco.repo.avm.AVMNodeConverter;
|
||||
import org.alfresco.service.cmr.avm.AVMService;
|
||||
import org.alfresco.service.cmr.model.FileFolderService;
|
||||
import org.alfresco.service.cmr.model.FileInfo;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.ContentReader;
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
@@ -53,7 +51,7 @@ public class OutputUtil
|
||||
|
||||
private static String getAVMParentPath(NodeRef nodeRef,
|
||||
NodeService nodeService)
|
||||
throws Exception
|
||||
throws Exception
|
||||
{
|
||||
ChildAssociationRef caf = nodeService.getPrimaryParent(nodeRef);
|
||||
final String parentName = (String)
|
||||
@@ -72,80 +70,47 @@ public class OutputUtil
|
||||
}
|
||||
}
|
||||
|
||||
public static void generate(NodeRef createdNode,
|
||||
public static void generate(String parentPath,
|
||||
Document xml,
|
||||
TemplateType tt,
|
||||
String fileName,
|
||||
NodeRef containerNodeRef,
|
||||
FileFolderService fileFolderService,
|
||||
ContentService contentService,
|
||||
NodeService nodeService)
|
||||
throws Exception
|
||||
NodeService nodeService,
|
||||
AVMService avmService)
|
||||
throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
// get the node ref of the node that will contain the content
|
||||
String generatedFileName = stripExtension(fileName) + ".shtml";
|
||||
FileInfo fileInfo =
|
||||
fileFolderService.create(containerNodeRef,
|
||||
generatedFileName,
|
||||
ContentModel.TYPE_CONTENT);
|
||||
NodeRef fileNodeRef = fileInfo.getNodeRef();
|
||||
|
||||
OutputStream fileOut = avmService.createFile(parentPath, generatedFileName);
|
||||
|
||||
String fullAvmPath = parentPath + '/' + generatedFileName;
|
||||
|
||||
if (LOGGER.isDebugEnabled())
|
||||
LOGGER.debug("Created file node for file: " +
|
||||
generatedFileName);
|
||||
fullAvmPath);
|
||||
|
||||
// get a writer for the content and put the file
|
||||
ContentWriter writer = contentService.getWriter(fileNodeRef,
|
||||
ContentModel.PROP_CONTENT, true);
|
||||
// set the mimetype and encoding
|
||||
writer.setMimetype("text/html");
|
||||
writer.setEncoding("UTF-8");
|
||||
TemplateOutputMethod tom = tt.getOutputMethods().get(0);
|
||||
OutputStreamWriter out =
|
||||
new OutputStreamWriter(writer.getContentOutputStream());
|
||||
OutputStreamWriter out = new OutputStreamWriter(fileOut);
|
||||
tom.generate(xml, tt, out);
|
||||
out.close();
|
||||
nodeService.setProperty(fileNodeRef,
|
||||
|
||||
NodeRef outputNodeRef = AVMNodeConverter.ToNodeRef(-1, fullAvmPath);
|
||||
nodeService.setProperty(outputNodeRef,
|
||||
TemplatingService.TT_QNAME,
|
||||
tt.getName());
|
||||
|
||||
LOGGER.debug("generated " + generatedFileName + " using " + tom);
|
||||
|
||||
if (createdNode != null)
|
||||
{
|
||||
nodeService.setProperty(createdNode,
|
||||
TemplatingService.TT_GENERATED_OUTPUT_QNAME,
|
||||
fileNodeRef.toString());
|
||||
}
|
||||
NodeRef createdNodeRef = AVMNodeConverter.ToNodeRef(-1, parentPath + '/' + fileName);
|
||||
nodeService.setProperty(createdNodeRef,
|
||||
TemplatingService.TT_GENERATED_OUTPUT_QNAME,
|
||||
outputNodeRef.toString());
|
||||
|
||||
AVMService avmService = (AVMService)AVMContext.fgInstance.fAppContext.getBean("avmService");
|
||||
final String parentAVMPath = getAVMParentPath(createdNode, nodeService);
|
||||
try
|
||||
{
|
||||
out = new OutputStreamWriter(avmService.createFile(parentAVMPath, generatedFileName));
|
||||
}
|
||||
catch (AVMExistsException e)
|
||||
{
|
||||
out = new OutputStreamWriter(avmService.getFileOutputStream(parentAVMPath + "/" + generatedFileName));
|
||||
}
|
||||
LOGGER.debug("generating " + generatedFileName + " to avm");
|
||||
tom.generate(xml, tt, out);
|
||||
out.close();
|
||||
try
|
||||
{
|
||||
out = new OutputStreamWriter(avmService.createFile(parentAVMPath, generatedFileName));
|
||||
}
|
||||
catch (AVMExistsException e)
|
||||
{
|
||||
out = new OutputStreamWriter(avmService.getFileOutputStream(parentAVMPath + "/" + generatedFileName));
|
||||
}
|
||||
LOGGER.debug("generating " + generatedFileName + " to avm");
|
||||
tom.generate(xml, tt, out);
|
||||
out.close();
|
||||
|
||||
try
|
||||
// TODO: should this output go anywhere in the AVM world now we are writing directly?
|
||||
/*try
|
||||
{
|
||||
out = new OutputStreamWriter(avmService.createFile(parentAVMPath, fileName));
|
||||
}
|
||||
@@ -156,7 +121,7 @@ public class OutputUtil
|
||||
LOGGER.debug("writing xml " + fileName + " to avm");
|
||||
final TemplatingService ts = TemplatingService.getInstance();
|
||||
ts.writeXML(xml, out);
|
||||
out.close();
|
||||
out.close();*/
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -169,7 +134,7 @@ public class OutputUtil
|
||||
public static void regenerate(final NodeRef nodeRef,
|
||||
final ContentService contentService,
|
||||
final NodeService nodeService)
|
||||
throws Exception
|
||||
throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -203,14 +168,14 @@ public class OutputUtil
|
||||
writer.setEncoding("UTF-8");
|
||||
// put a loop to generate all output methods
|
||||
TemplateOutputMethod tom = tt.getOutputMethods().get(0);
|
||||
OutputStreamWriter out =
|
||||
new OutputStreamWriter(writer.getContentOutputStream());
|
||||
OutputStreamWriter out = new OutputStreamWriter(writer.getContentOutputStream());
|
||||
tom.generate(xml, tt, out);
|
||||
out.close();
|
||||
|
||||
LOGGER.debug("generated " + fileName + " using " + tom);
|
||||
|
||||
AVMService avmService = (AVMService)AVMContext.fgInstance.fAppContext.getBean("avmService");
|
||||
// TODO: do we need these now - as the NodeRef's above are now AVM NodeRefs...?
|
||||
/*AVMService avmService = (AVMService)AVMContext.fgInstance.fAppContext.getBean("avmService");
|
||||
final String parentAVMPath = getAVMParentPath(nodeRef, nodeService);
|
||||
try
|
||||
{
|
||||
@@ -222,9 +187,9 @@ public class OutputUtil
|
||||
}
|
||||
LOGGER.debug("generating " + generatedFileName + " to avm");
|
||||
tom.generate(xml, tt, out);
|
||||
out.close();
|
||||
out.close();*/
|
||||
|
||||
try
|
||||
/*try
|
||||
{
|
||||
out = new OutputStreamWriter(avmService.createFile(parentAVMPath, fileName));
|
||||
}
|
||||
@@ -234,7 +199,7 @@ public class OutputUtil
|
||||
}
|
||||
LOGGER.debug("writing xml " + fileName + " to avm");
|
||||
ts.writeXML(xml, out);
|
||||
out.close();
|
||||
out.close();*/
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@@ -50,345 +50,342 @@ import org.alfresco.web.bean.repository.Repository;
|
||||
/**
|
||||
* Provides management of template types.
|
||||
*/
|
||||
public final class TemplatingService
|
||||
implements Serializable
|
||||
public final class TemplatingService implements Serializable
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Encapsulation of configuration file management.
|
||||
*/
|
||||
private static class Configuration
|
||||
{
|
||||
|
||||
/** indicates whether or not the configuration file has been loaded */
|
||||
public static boolean loaded = false;
|
||||
private static NodeRef configFileNodeRef = null;
|
||||
|
||||
/**
|
||||
* locate the configuration file. currently it is stored as
|
||||
* <tt>templating_config.xml</tt> at the root of the data dictionary.
|
||||
*
|
||||
* @return the configuration file, which is currently all the
|
||||
* <tt>TemplateTypes</tt> serialized using object serialization.
|
||||
*/
|
||||
private static NodeRef getConfigFile()
|
||||
{
|
||||
if (configFileNodeRef == null)
|
||||
{
|
||||
final TemplatingService ts = TemplatingService.INSTANCE;
|
||||
LOGGER.debug("loading config file");
|
||||
// get the template from the special Email Templates folder
|
||||
FacesContext fc = FacesContext.getCurrentInstance();
|
||||
String xpath = (Application.getRootPath(fc) + "/" +
|
||||
Application.getGlossaryFolderName(fc));
|
||||
NodeRef rootNodeRef = ts.nodeService.getRootNode(Repository.getStoreRef());
|
||||
List<NodeRef> results = ts.searchService.selectNodes(rootNodeRef, xpath, null, ts.namespaceService, false);
|
||||
if (results.size() != 1)
|
||||
throw new RuntimeException("expected one result for " + xpath);
|
||||
NodeRef dataDictionaryNodeRef = results.get(0);
|
||||
LOGGER.debug("loaded data dictionary " + dataDictionaryNodeRef);
|
||||
try
|
||||
{
|
||||
Configuration.configFileNodeRef =
|
||||
ts.fileFolderService.create(dataDictionaryNodeRef,
|
||||
"templating_configuration.xml",
|
||||
ContentModel.TYPE_CONTENT).getNodeRef();
|
||||
}
|
||||
catch (FileExistsException fee)
|
||||
{
|
||||
Configuration.configFileNodeRef =
|
||||
ts.fileFolderService.searchSimple(dataDictionaryNodeRef, "templating_configuration.xml");
|
||||
}
|
||||
LOGGER.debug("loaded config file " + configFileNodeRef);
|
||||
assert Configuration.configFileNodeRef != null : "unable to load templating_configuration.xml";
|
||||
}
|
||||
return Configuration.configFileNodeRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the configuration file into the templating service.
|
||||
*/
|
||||
public static void load()
|
||||
throws IOException
|
||||
{
|
||||
final TemplatingService ts = TemplatingService.INSTANCE;
|
||||
final NodeRef configFileNodeRef = getConfigFile();
|
||||
FacesContext fc = FacesContext.getCurrentInstance();
|
||||
final ContentReader contentReader = ts.contentService.getReader(configFileNodeRef,
|
||||
ContentModel.TYPE_CONTENT);
|
||||
if (contentReader == null)
|
||||
LOGGER.debug("templating_config.xml is empty");
|
||||
else
|
||||
{
|
||||
LOGGER.debug("parsing templating_config.xml");
|
||||
final InputStream contentIn = contentReader.getContentInputStream();
|
||||
final ObjectInputStream in = new ObjectInputStream(contentIn);
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
final TemplateType tt = (TemplateType)in.readObject();
|
||||
TemplatingService.INSTANCE.registerTemplateType(tt);
|
||||
}
|
||||
catch (EOFException eof)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
catch (ClassNotFoundException cnfe)
|
||||
{
|
||||
TemplatingService.LOGGER.error(cnfe);
|
||||
}
|
||||
}
|
||||
loaded = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the current state of the templating service to the configuration file.
|
||||
*/
|
||||
public static void save()
|
||||
throws IOException
|
||||
{
|
||||
final TemplatingService ts = TemplatingService.INSTANCE;
|
||||
FacesContext fc = FacesContext.getCurrentInstance();
|
||||
final NodeRef configFileNodeRef = getConfigFile();
|
||||
final OutputStream contentOut = ts.contentService.getWriter(configFileNodeRef, ContentModel.TYPE_CONTENT, true).getContentOutputStream();
|
||||
final ObjectOutputStream out = new ObjectOutputStream(contentOut);
|
||||
for (TemplateType tt : TemplatingService.INSTANCE.getTemplateTypes())
|
||||
{
|
||||
out.writeObject(tt);
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* temporary location of the property on nodes that are xml files created
|
||||
* by templating.
|
||||
*/
|
||||
public static final org.alfresco.service.namespace.QName TT_QNAME =
|
||||
org.alfresco.service.namespace.QName.createQName(org.alfresco.service.namespace.NamespaceService.CONTENT_MODEL_1_0_URI, "tt");
|
||||
|
||||
/**
|
||||
* temporary location of the property on nodes generated from xml assets.
|
||||
*/
|
||||
public static final org.alfresco.service.namespace.QName TT_GENERATED_OUTPUT_QNAME =
|
||||
org.alfresco.service.namespace.QName.createQName(org.alfresco.service.namespace.NamespaceService.CONTENT_MODEL_1_0_URI, "tt_generated_output");
|
||||
|
||||
private static final Log LOGGER = LogFactory.getLog(TemplatingService.class);
|
||||
|
||||
/** the single instance initialized using spring */
|
||||
private static TemplatingService INSTANCE;
|
||||
|
||||
/** internal storage of template types, keyed by the template name */
|
||||
private HashMap<String, TemplateType> templateTypes =
|
||||
new HashMap<String, TemplateType>();
|
||||
|
||||
private final ContentService contentService;
|
||||
private final NodeService nodeService;
|
||||
private final FileFolderService fileFolderService;
|
||||
private final DictionaryService dictionaryService;
|
||||
private final NamespaceService namespaceService;
|
||||
private final SearchService searchService;
|
||||
|
||||
/** instantiated using spring */
|
||||
public TemplatingService(final ContentService contentService,
|
||||
final NodeService nodeService,
|
||||
final FileFolderService fileFolderService,
|
||||
final DictionaryService dictionaryService,
|
||||
final NamespaceService namespaceService,
|
||||
final SearchService searchService)
|
||||
{
|
||||
this.contentService = contentService;
|
||||
this.nodeService = nodeService;
|
||||
this.fileFolderService = fileFolderService;
|
||||
this.dictionaryService = dictionaryService;
|
||||
this.namespaceService = namespaceService;
|
||||
this.searchService = searchService;
|
||||
if (INSTANCE == null)
|
||||
INSTANCE = this;
|
||||
}
|
||||
|
||||
/** Provides the templating service instance, loads config if necessary */
|
||||
public static TemplatingService getInstance()
|
||||
{
|
||||
if (!Configuration.loaded)
|
||||
{
|
||||
LOGGER.debug("loading configuration");
|
||||
try
|
||||
{
|
||||
Configuration.load();
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
LOGGER.error(t);
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return TemplatingService.INSTANCE;
|
||||
}
|
||||
|
||||
/** returns all registered template types */
|
||||
public Collection<TemplateType> getTemplateTypes()
|
||||
{
|
||||
return this.templateTypes.values();
|
||||
}
|
||||
|
||||
/** return the template type by name or <tt>null</tt> if not found */
|
||||
public TemplateType getTemplateType(final String name)
|
||||
{
|
||||
return this.templateTypes.get(name);
|
||||
}
|
||||
|
||||
/** registers a template type. if one exists with the same name, it is replaced */
|
||||
public void registerTemplateType(final TemplateType tt)
|
||||
{
|
||||
this.templateTypes.put(tt.getName(), tt);
|
||||
try
|
||||
{
|
||||
Configuration.save();
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
LOGGER.error(ioe);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* instantiate a template type. for now this will always generate the
|
||||
* xforms implementation, but will at some point be configurable such that
|
||||
* the template type implementation can be configured for the system,
|
||||
* or specified in the gui.
|
||||
*/
|
||||
public TemplateType newTemplateType(final String name,
|
||||
final NodeRef schemaNodeRef)
|
||||
{
|
||||
return new TemplateTypeImpl(name, schemaNodeRef);
|
||||
}
|
||||
|
||||
/** utility function for creating a document */
|
||||
public Document newDocument()
|
||||
{
|
||||
try
|
||||
{
|
||||
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
dbf.setNamespaceAware(true);
|
||||
dbf.setValidating(false);
|
||||
final DocumentBuilder db = dbf.newDocumentBuilder();
|
||||
return db.newDocument();
|
||||
}
|
||||
catch (ParserConfigurationException pce)
|
||||
{
|
||||
assert false : pce;
|
||||
LOGGER.error(pce);
|
||||
return null;
|
||||
}
|
||||
// catch (SAXException saxe)
|
||||
// {
|
||||
// LOGGER.error(saxe);
|
||||
// }
|
||||
// catch (IOException ioe)
|
||||
// {
|
||||
// LOGGER.error(ioe);
|
||||
// }
|
||||
}
|
||||
|
||||
/** utility function for serializing a node */
|
||||
public void writeXML(final Node n, final Writer output)
|
||||
{
|
||||
try
|
||||
{
|
||||
final TransformerFactory tf = TransformerFactory.newInstance();
|
||||
final Transformer t = tf.newTransformer();
|
||||
t.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
|
||||
if (LOGGER.isDebugEnabled())
|
||||
{
|
||||
LOGGER.debug("writing out a document for " +
|
||||
(n instanceof Document
|
||||
? ((Document)n).getDocumentElement()
|
||||
: n).getNodeName() +
|
||||
" to " + output);
|
||||
final StringWriter sw = new StringWriter();
|
||||
t.transform(new DOMSource(n), new StreamResult(sw));
|
||||
LOGGER.debug(sw.toString());
|
||||
}
|
||||
t.transform(new DOMSource(n), new StreamResult(output));
|
||||
}
|
||||
catch (TransformerException te)
|
||||
{
|
||||
te.printStackTrace();
|
||||
assert false : te.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/** utility function for serializing a node */
|
||||
public void writeXML(final Node n, final File output)
|
||||
throws IOException
|
||||
{
|
||||
this.writeXML(n, new FileWriter(output));
|
||||
}
|
||||
|
||||
/** utility function for serializing a node */
|
||||
public String writeXMLToString(final Node n)
|
||||
{
|
||||
final StringWriter result = new StringWriter();
|
||||
this.writeXML(n, result);
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/** utility function for parsing xml */
|
||||
public Document parseXML(final String source)
|
||||
throws ParserConfigurationException,
|
||||
SAXException,
|
||||
IOException
|
||||
{
|
||||
return this.parseXML(new ByteArrayInputStream(source.getBytes()));
|
||||
}
|
||||
|
||||
/** utility function for parsing xml */
|
||||
public Document parseXML(final NodeRef nodeRef)
|
||||
throws ParserConfigurationException,
|
||||
SAXException,
|
||||
IOException
|
||||
{
|
||||
final ContentReader contentReader =
|
||||
this.contentService.getReader(nodeRef, ContentModel.TYPE_CONTENT);
|
||||
final InputStream in = contentReader.getContentInputStream();
|
||||
return this.parseXML(in);
|
||||
}
|
||||
|
||||
/** utility function for parsing xml */
|
||||
public Document parseXML(final File source)
|
||||
throws ParserConfigurationException,
|
||||
SAXException,
|
||||
IOException
|
||||
{
|
||||
return this.parseXML(new FileInputStream(source));
|
||||
}
|
||||
|
||||
/** utility function for parsing xml */
|
||||
public Document parseXML(final InputStream source)
|
||||
throws ParserConfigurationException,
|
||||
SAXException,
|
||||
IOException
|
||||
{
|
||||
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
dbf.setNamespaceAware(true);
|
||||
dbf.setValidating(false);
|
||||
final DocumentBuilder db = dbf.newDocumentBuilder();
|
||||
final Document result = db.parse(source);
|
||||
source.close();
|
||||
return result;
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Encapsulation of configuration file management.
|
||||
*/
|
||||
private static class Configuration
|
||||
{
|
||||
/** indicates whether or not the configuration file has been loaded */
|
||||
public static boolean loaded = false;
|
||||
private static NodeRef configFileNodeRef = null;
|
||||
|
||||
/**
|
||||
* locate the configuration file. currently it is stored as
|
||||
* <tt>templating_config.xml</tt> at the root of the data dictionary.
|
||||
*
|
||||
* @return the configuration file, which is currently all the
|
||||
* <tt>TemplateTypes</tt> serialized using object serialization.
|
||||
*/
|
||||
private static NodeRef getConfigFile()
|
||||
{
|
||||
if (configFileNodeRef == null)
|
||||
{
|
||||
final TemplatingService ts = TemplatingService.INSTANCE;
|
||||
LOGGER.debug("loading config file");
|
||||
// get the template from the special Email Templates folder
|
||||
FacesContext fc = FacesContext.getCurrentInstance();
|
||||
String xpath = (Application.getRootPath(fc) + "/" +
|
||||
Application.getGlossaryFolderName(fc));
|
||||
NodeRef rootNodeRef = ts.nodeService.getRootNode(Repository.getStoreRef());
|
||||
List<NodeRef> results = ts.searchService.selectNodes(rootNodeRef, xpath, null, ts.namespaceService, false);
|
||||
if (results.size() != 1)
|
||||
throw new RuntimeException("expected one result for " + xpath);
|
||||
NodeRef dataDictionaryNodeRef = results.get(0);
|
||||
LOGGER.debug("loaded data dictionary " + dataDictionaryNodeRef);
|
||||
try
|
||||
{
|
||||
Configuration.configFileNodeRef =
|
||||
ts.fileFolderService.create(dataDictionaryNodeRef,
|
||||
"templating_configuration.xml",
|
||||
ContentModel.TYPE_CONTENT).getNodeRef();
|
||||
}
|
||||
catch (FileExistsException fee)
|
||||
{
|
||||
Configuration.configFileNodeRef =
|
||||
ts.fileFolderService.searchSimple(dataDictionaryNodeRef, "templating_configuration.xml");
|
||||
}
|
||||
LOGGER.debug("loaded config file " + configFileNodeRef);
|
||||
assert Configuration.configFileNodeRef != null : "unable to load templating_configuration.xml";
|
||||
}
|
||||
return Configuration.configFileNodeRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the configuration file into the templating service.
|
||||
*/
|
||||
public static void load()
|
||||
throws IOException
|
||||
{
|
||||
final TemplatingService ts = TemplatingService.INSTANCE;
|
||||
final NodeRef configFileNodeRef = getConfigFile();
|
||||
FacesContext fc = FacesContext.getCurrentInstance();
|
||||
final ContentReader contentReader = ts.contentService.getReader(configFileNodeRef,
|
||||
ContentModel.TYPE_CONTENT);
|
||||
if (contentReader == null)
|
||||
LOGGER.debug("templating_config.xml is empty");
|
||||
else
|
||||
{
|
||||
LOGGER.debug("parsing templating_config.xml");
|
||||
final InputStream contentIn = contentReader.getContentInputStream();
|
||||
final ObjectInputStream in = new ObjectInputStream(contentIn);
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
final TemplateType tt = (TemplateType)in.readObject();
|
||||
TemplatingService.INSTANCE.registerTemplateType(tt);
|
||||
}
|
||||
catch (EOFException eof)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
catch (ClassNotFoundException cnfe)
|
||||
{
|
||||
TemplatingService.LOGGER.error(cnfe);
|
||||
}
|
||||
}
|
||||
loaded = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the current state of the templating service to the configuration file.
|
||||
*/
|
||||
public static void save()
|
||||
throws IOException
|
||||
{
|
||||
final TemplatingService ts = TemplatingService.INSTANCE;
|
||||
FacesContext fc = FacesContext.getCurrentInstance();
|
||||
final NodeRef configFileNodeRef = getConfigFile();
|
||||
final OutputStream contentOut = ts.contentService.getWriter(configFileNodeRef, ContentModel.TYPE_CONTENT, true).getContentOutputStream();
|
||||
final ObjectOutputStream out = new ObjectOutputStream(contentOut);
|
||||
for (TemplateType tt : TemplatingService.INSTANCE.getTemplateTypes())
|
||||
{
|
||||
out.writeObject(tt);
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* temporary location of the property on nodes that are xml files created
|
||||
* by templating.
|
||||
*/
|
||||
public static final org.alfresco.service.namespace.QName TT_QNAME =
|
||||
org.alfresco.service.namespace.QName.createQName(org.alfresco.service.namespace.NamespaceService.CONTENT_MODEL_1_0_URI, "tt");
|
||||
|
||||
/**
|
||||
* temporary location of the property on nodes generated from xml assets.
|
||||
*/
|
||||
public static final org.alfresco.service.namespace.QName TT_GENERATED_OUTPUT_QNAME =
|
||||
org.alfresco.service.namespace.QName.createQName(org.alfresco.service.namespace.NamespaceService.CONTENT_MODEL_1_0_URI, "tt_generated_output");
|
||||
|
||||
private static final Log LOGGER = LogFactory.getLog(TemplatingService.class);
|
||||
|
||||
/** the single instance initialized using spring */
|
||||
private static TemplatingService INSTANCE;
|
||||
|
||||
/** internal storage of template types, keyed by the template name */
|
||||
private HashMap<String, TemplateType> templateTypes =
|
||||
new HashMap<String, TemplateType>();
|
||||
|
||||
private final ContentService contentService;
|
||||
private final NodeService nodeService;
|
||||
private final FileFolderService fileFolderService;
|
||||
private final DictionaryService dictionaryService;
|
||||
private final NamespaceService namespaceService;
|
||||
private final SearchService searchService;
|
||||
|
||||
/** instantiated using spring */
|
||||
public TemplatingService(final ContentService contentService,
|
||||
final NodeService nodeService,
|
||||
final FileFolderService fileFolderService,
|
||||
final DictionaryService dictionaryService,
|
||||
final NamespaceService namespaceService,
|
||||
final SearchService searchService)
|
||||
{
|
||||
this.contentService = contentService;
|
||||
this.nodeService = nodeService;
|
||||
this.fileFolderService = fileFolderService;
|
||||
this.dictionaryService = dictionaryService;
|
||||
this.namespaceService = namespaceService;
|
||||
this.searchService = searchService;
|
||||
if (INSTANCE == null)
|
||||
INSTANCE = this;
|
||||
}
|
||||
|
||||
/** Provides the templating service instance, loads config if necessary */
|
||||
public static TemplatingService getInstance()
|
||||
{
|
||||
if (!Configuration.loaded)
|
||||
{
|
||||
LOGGER.debug("loading configuration");
|
||||
try
|
||||
{
|
||||
Configuration.load();
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
LOGGER.error(t);
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return TemplatingService.INSTANCE;
|
||||
}
|
||||
|
||||
/** returns all registered template types */
|
||||
public Collection<TemplateType> getTemplateTypes()
|
||||
{
|
||||
return this.templateTypes.values();
|
||||
}
|
||||
|
||||
/** return the template type by name or <tt>null</tt> if not found */
|
||||
public TemplateType getTemplateType(final String name)
|
||||
{
|
||||
return this.templateTypes.get(name);
|
||||
}
|
||||
|
||||
/** registers a template type. if one exists with the same name, it is replaced */
|
||||
public void registerTemplateType(final TemplateType tt)
|
||||
{
|
||||
this.templateTypes.put(tt.getName(), tt);
|
||||
try
|
||||
{
|
||||
Configuration.save();
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
LOGGER.error(ioe);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* instantiate a template type. for now this will always generate the
|
||||
* xforms implementation, but will at some point be configurable such that
|
||||
* the template type implementation can be configured for the system,
|
||||
* or specified in the gui.
|
||||
*/
|
||||
public TemplateType newTemplateType(final String name,
|
||||
final NodeRef schemaNodeRef)
|
||||
{
|
||||
return new TemplateTypeImpl(name, schemaNodeRef);
|
||||
}
|
||||
|
||||
/** utility function for creating a document */
|
||||
public Document newDocument()
|
||||
{
|
||||
try
|
||||
{
|
||||
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
dbf.setNamespaceAware(true);
|
||||
dbf.setValidating(false);
|
||||
final DocumentBuilder db = dbf.newDocumentBuilder();
|
||||
return db.newDocument();
|
||||
}
|
||||
catch (ParserConfigurationException pce)
|
||||
{
|
||||
assert false : pce;
|
||||
LOGGER.error(pce);
|
||||
return null;
|
||||
}
|
||||
// catch (SAXException saxe)
|
||||
// {
|
||||
// LOGGER.error(saxe);
|
||||
// }
|
||||
// catch (IOException ioe)
|
||||
// {
|
||||
// LOGGER.error(ioe);
|
||||
// }
|
||||
}
|
||||
|
||||
/** utility function for serializing a node */
|
||||
public void writeXML(final Node n, final Writer output)
|
||||
{
|
||||
try
|
||||
{
|
||||
final TransformerFactory tf = TransformerFactory.newInstance();
|
||||
final Transformer t = tf.newTransformer();
|
||||
t.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
|
||||
if (LOGGER.isDebugEnabled())
|
||||
{
|
||||
LOGGER.debug("writing out a document for " +
|
||||
(n instanceof Document
|
||||
? ((Document)n).getDocumentElement()
|
||||
: n).getNodeName() +
|
||||
" to " + output);
|
||||
final StringWriter sw = new StringWriter();
|
||||
t.transform(new DOMSource(n), new StreamResult(sw));
|
||||
LOGGER.debug(sw.toString());
|
||||
}
|
||||
t.transform(new DOMSource(n), new StreamResult(output));
|
||||
}
|
||||
catch (TransformerException te)
|
||||
{
|
||||
te.printStackTrace();
|
||||
assert false : te.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/** utility function for serializing a node */
|
||||
public void writeXML(final Node n, final File output)
|
||||
throws IOException
|
||||
{
|
||||
this.writeXML(n, new FileWriter(output));
|
||||
}
|
||||
|
||||
/** utility function for serializing a node */
|
||||
public String writeXMLToString(final Node n)
|
||||
{
|
||||
final StringWriter result = new StringWriter();
|
||||
this.writeXML(n, result);
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/** utility function for parsing xml */
|
||||
public Document parseXML(final String source)
|
||||
throws ParserConfigurationException,
|
||||
SAXException,
|
||||
IOException
|
||||
{
|
||||
return this.parseXML(new ByteArrayInputStream(source.getBytes()));
|
||||
}
|
||||
|
||||
/** utility function for parsing xml */
|
||||
public Document parseXML(final NodeRef nodeRef)
|
||||
throws ParserConfigurationException,
|
||||
SAXException,
|
||||
IOException
|
||||
{
|
||||
final ContentReader contentReader =
|
||||
this.contentService.getReader(nodeRef, ContentModel.TYPE_CONTENT);
|
||||
final InputStream in = contentReader.getContentInputStream();
|
||||
return this.parseXML(in);
|
||||
}
|
||||
|
||||
/** utility function for parsing xml */
|
||||
public Document parseXML(final File source)
|
||||
throws ParserConfigurationException,
|
||||
SAXException,
|
||||
IOException
|
||||
{
|
||||
return this.parseXML(new FileInputStream(source));
|
||||
}
|
||||
|
||||
/** utility function for parsing xml */
|
||||
public Document parseXML(final InputStream source)
|
||||
throws ParserConfigurationException,
|
||||
SAXException,
|
||||
IOException
|
||||
{
|
||||
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
dbf.setNamespaceAware(true);
|
||||
dbf.setValidating(false);
|
||||
final DocumentBuilder db = dbf.newDocumentBuilder();
|
||||
final Document result = db.parse(source);
|
||||
source.close();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user