freemarker.template.SimpleDate Java Examples

The following examples show how to use freemarker.template.SimpleDate. 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: FreemarkerTemplateHandler.java    From myexcel with Apache License 2.0 6 votes vote down vote up
private void setObjectWrapper(Configuration configuration) {
    configuration.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23) {
        @Override
        public TemplateModel wrap(Object object) throws TemplateModelException {
            if (object instanceof LocalDate) {
                return new SimpleDate(Date.valueOf((LocalDate) object));
            }
            if (object instanceof LocalTime) {
                return new SimpleDate(Time.valueOf((LocalTime) object));
            }
            if (object instanceof LocalDateTime) {
                return new SimpleDate(Timestamp.valueOf((LocalDateTime) object));
            }
            return super.wrap(object);
        }
    });
}
 
Example #2
Source File: FreeMarkerAutoconfigure.java    From cms with Apache License 2.0 6 votes vote down vote up
@Override
public TemplateModel wrap(Object obj) throws TemplateModelException {
    if (obj instanceof LocalDateTime) {
        Timestamp timestamp = Timestamp.valueOf((LocalDateTime) obj);
        return new SimpleDate(timestamp);
    }
    if (obj instanceof LocalDate) {
        Date date = Date.valueOf((LocalDate) obj);
        return new SimpleDate(date);
    }
    if (obj instanceof LocalTime) {
        Time time = Time.valueOf((LocalTime) obj);
        return new SimpleDate(time);
    }
    return super.wrap(obj);
}
 
Example #3
Source File: FormatTimeMethod.java    From pippo with Apache License 2.0 5 votes vote down vote up
private Date getFormattableObject(Object value) {
    if (value instanceof SimpleDate) {
        return ((SimpleDate) value).getAsDate();
    } else {
        throw new PippoRuntimeException("Formattable object for FormatTime not found!");
    }
}
 
Example #4
Source File: PrettyTimeMethod.java    From pippo with Apache License 2.0 5 votes vote down vote up
private Date getFormattableObject(Object value) {
    if (value instanceof SimpleDate) {
        return ((SimpleDate) value).getAsDate();
    } else {
        throw new PippoRuntimeException("Formattable object for PrettyTime not found!");
    }
}
 
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;
}