freemarker.ext.dom.NodeModel Java Examples

The following examples show how to use freemarker.ext.dom.NodeModel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FreemarkerParser.java    From boubei-tss with Apache License 2.0 5 votes vote down vote up
/**
 * 将XML数据转换为NodeModel
 */
public static NodeModel translateValue(String xmlValue){
	NodeModel node = null;
    try {
    	node = freemarker.ext.dom.NodeModel.parse(new InputSource(new StringReader(xmlValue)));
    } catch (Exception e) { }
    
    return node;
}
 
Example #2
Source File: TemplateNode.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @return FreeMarker NodeModel for the XML content of this node, or null if no parsable XML found
 */
public NodeModel getXmlNodeModel()
{
    try
    {
        return NodeModel.parse(new InputSource(new StringReader(getContent())));
    }
    catch (Throwable err)
    {
        if (logger.isDebugEnabled())
            logger.debug(err.getMessage(), err);
        
        return null;
    }
}
 
Example #3
Source File: IDLReader.java    From cougar with Apache License 2.0 5 votes vote down vote up
private Node getRootNode(NodeModel model) {
    NodeList list = model.getNode().getChildNodes();
    for (int i=0; i<list.getLength(); i++) {
        Node n = list.item(i);
        if (n instanceof Element) {
            return n;
        }
    }
    throw new IllegalStateException("Can't find root node!");
}
 
Example #4
Source File: GeneratorControl.java    From stategen with GNU Affero General Public License v3.0 4 votes vote down vote up
/** load xml data */
public NodeModel loadXml(String file) {
    return loadXml(file,true);
}
 
Example #5
Source File: StandardRenditionLocationResolverImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
private String renderPathTemplate(String pathTemplate, NodeRef sourceNode, NodeRef tempRenditionLocation, NodeRef companyHome)
{
    NodeService nodeService = serviceRegistry.getNodeService();
    FileFolderService fileFolderService = serviceRegistry.getFileFolderService();
    
    final Map<String, Object> root = new HashMap<String, Object>();

    List<FileInfo> sourcePathInfo;
    String fullSourceName;
    String cwd;
    try
    {
        //Since the root of the store is typically not a folder, we use company home as the root of this tree
        sourcePathInfo = fileFolderService.getNamePath(companyHome, sourceNode);
        //Remove the last element (the actual source file name)
        FileInfo sourceFileInfo = sourcePathInfo.remove(sourcePathInfo.size() - 1);
        fullSourceName = sourceFileInfo.getName();
        
        StringBuilder cwdBuilder = new StringBuilder("/");
        for (FileInfo file : sourcePathInfo)
        {
            cwdBuilder.append(file.getName());
            cwdBuilder.append('/');
        }
        cwd = cwdBuilder.toString();
    }
    catch (FileNotFoundException e)
    {
        log.warn("Failed to resolve path to source node: " + sourceNode + ". Default to Company Home");
        fullSourceName = nodeService.getPrimaryParent(sourceNode).getQName().getLocalName();
        cwd = "/";
    }

    String trimmedSourceName = fullSourceName;
    String sourceExtension = "";
    int extensionIndex = fullSourceName.lastIndexOf('.');
    if (extensionIndex != -1)
    {
        trimmedSourceName = (extensionIndex == 0) ? "" : fullSourceName.substring(0, extensionIndex);
        sourceExtension = (extensionIndex == fullSourceName.length() - 1) ? "" : fullSourceName
                    .substring(extensionIndex + 1);
    }

    root.put("name", trimmedSourceName);
    root.put("extension", sourceExtension);
    root.put("date", new SimpleDate(new Date(), SimpleDate.DATETIME));
    root.put("cwd", cwd);
    TemplateNode companyHomeNode = new TemplateNode(companyHome, serviceRegistry, null); 
    root.put("companyHome", companyHomeNode); 
    root.put("companyhome", companyHomeNode);   //Added this to be consistent with the script API
    root.put("sourceNode", new TemplateNode(sourceNode, serviceRegistry, null));
    root.put("sourceContentType", nodeService.getType(sourceNode).getLocalName());
    root.put("renditionContentType", nodeService.getType(tempRenditionLocation).getLocalName());
    NodeRef person = serviceRegistry.getPersonService().getPerson(AuthenticationUtil.getFullyAuthenticatedUser());
    root.put("person", new TemplateNode(person, serviceRegistry, null));

    if (sourceNodeIsXml(sourceNode))
    {
        try
        {
            Document xml = XMLUtil.parse(sourceNode, serviceRegistry.getContentService());
            pathTemplate = buildNamespaceDeclaration(xml) + pathTemplate;
            root.put("xml", NodeModel.wrap(xml));
        } catch (Exception ex)
        {
            log.warn("Failed to parse XML content into path template model: Node = " + sourceNode);
        }
    }

    if (log.isDebugEnabled())
    {
        log.debug("Path template model: " + root);
    }

    String result = null;
    try
    {
        if (log.isDebugEnabled())
        {
            log.debug("Processing " + pathTemplate + " using source node " + cwd + fullSourceName);
        }
        result = serviceRegistry.getTemplateService().processTemplateString("freemarker", pathTemplate,
                    new SimpleHash(root));
    } catch (TemplateException te)
    {
        log.error("Error while trying to process rendition path template: " + pathTemplate);
        log.error(te.getMessage(), te);
    }
    if (log.isDebugEnabled())
    {
        log.debug("processed pattern " + pathTemplate + " as " + result);
    }
    return result;
}
 
Example #6
Source File: IDLReader.java    From cougar with Apache License 2.0 4 votes vote down vote up
public void init(
        Document iddDoc,
        Document extensionDoc,
        final String service,
        String packageName,
        final String basedir,
        final String genSrcDir,
        final Log log,
        final String outputDir,
        boolean client,
        boolean server)
        throws Exception {

    try {
        output = new File(basedir, genSrcDir);
        if (outputDir != null) {
            iDDOutputDir = new File(basedir+"/"+outputDir);
            if (!iDDOutputDir.exists()) {
                if (!iDDOutputDir.mkdirs()) {
                    throw new IllegalArgumentException("IDD Output Directory "+iDDOutputDir+" could not be created");
                }
            }
            if (!iDDOutputDir.isDirectory() || (!iDDOutputDir.canWrite())) {
                throw new IllegalArgumentException("IDD Output Directory "+iDDOutputDir+" is not a directory or cannot be written to.");
            }
        }
        config = new Configuration();
        config.setClassForTemplateLoading(IDLReader.class, "/templates");

        config.setStrictSyntaxMode(true);
        this.log = log;
        this.packageName = packageName;
        this.service = service;
        this.client = client;
        this.server = server || !client; // server must be true if client if false.

        dataModel = NodeModel.wrap(iddDoc.cloneNode(true));

        if (extensionDoc != null) {
            NodeModel extensionModel = NodeModel.wrap(extensionDoc);
            mergeExtensionsIntoDocument(getRootNode(dataModel),
                    getRootNode(extensionModel));
            removeUndefinedOperations(getRootNode(dataModel),
                    getRootNode(extensionModel));
        }
        if(log.isDebugEnabled()) {
            log.debug(serialize());
        }
    } catch (final Exception e) {
        log.error("Failed to initialise FTL", e);
        throw e;
    }
}