freemarker.template.SimpleDate#freemarker.template.SimpleHash源码实例Demo

下面列出了freemarker.template.SimpleDate#freemarker.template.SimpleHash 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: scipio-erp   文件: LangFtlUtil.java
/**
 * Adapts a map to a TemplateHashModelEx using an appropriate simple adapter, normally
 * DefaultMapAdapter (or SimpleMapModel for BeansWrapper compatibility).
 * <p>
 * The ObjectWrapper is expected to implement at least ObjectWrapperWithAPISupport.
 * <p>
 * WARN: If impossible, it will duplicate the map using SimpleHash; but because this may result
 * in loss of ordering, a log warning will be printed.
 */
public static TemplateHashModelEx makeSimpleMapAdapter(Map<?, ?> map, ObjectWrapper objectWrapper, boolean permissive) throws TemplateModelException {
    // COMPATIBILITY MODE: check if exactly BeansWrapper, or a class that we know extends it WITHOUT extending DefaultObjectWrapper
    if (objectWrapper instanceof ScipioBeansWrapper || BeansWrapper.class.equals(objectWrapper.getClass())) {
        return new SimpleMapModel(map, (BeansWrapper) objectWrapper);
    } else if (objectWrapper instanceof ObjectWrapperWithAPISupport) {
        return DefaultMapAdapter.adapt(map, (ObjectWrapperWithAPISupport) objectWrapper);
    } else {
        if (permissive) {
            Debug.logWarning("Scipio: adaptSimpleMap: Unsupported Freemarker object wrapper (expected to implement ObjectWrapperWithAPISupport or BeansWrapper); forced to adapt map"
                    + " using SimpleHash; this could cause loss of map insertion ordering; please switch renderer setup to a different ObjectWrapper", module);
            return new SimpleHash(map, objectWrapper);
        } else {
            throw new TemplateModelException("Tried to wrap a Map using an adapter class,"
                    + " but our ObjectWrapper does not implement ObjectWrapperWithAPISupport or BeansWrapper"
                    + "; please switch renderer setup to a different ObjectWrapper");
        }
    }
}
 
源代码2 项目: scipio-erp   文件: LangFtlUtil.java
/**
 * Combines two maps with the given operator into a new hash.
 */
public static TemplateHashModelEx combineMaps(TemplateHashModelEx first, TemplateHashModelEx second, SetOperations ops,
        ObjectWrapper objectWrapper) throws TemplateModelException {
    SimpleHash res = new SimpleHash(objectWrapper);
    if (ops == null || ops == SetOperations.UNION) {
        // this is less efficient than freemarker + operator, but provides the "alternative" implementation, so have choice
        addToSimpleMap(res, first);
        addToSimpleMap(res, second);
    }
    else if (ops == SetOperations.INTERSECT) {
        Set<String> intersectKeys = toStringSet(second.keys());
        intersectKeys.retainAll(toStringSet(first.keys()));
        addToSimpleMap(res, second, intersectKeys);
    }
    else if (ops == SetOperations.DIFFERENCE) {
        Set<String> diffKeys = toStringSet(first.keys());
        diffKeys.removeAll(toStringSet(second.keys()));
        addToSimpleMap(res, first, diffKeys);
    }
    else {
        throw new TemplateModelException("Unsupported combineMaps operation");
    }
    return res;
}
 
源代码3 项目: javalite   文件: WrapTag.java
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    if(!params.containsKey("with")) {
        throw new RuntimeException("\"with\" param was not provided");
    }

    String withArgument = params.get("with").toString();

    StringWriter innerContent = new StringWriter();
    body.render(innerContent);        

    SimpleHash envValues = getHash(env);
    envValues.putAll(params);
    envValues.put("page_content", innerContent.toString());
    for(Object key : params.keySet()) {
        if(key.toString().equals("with")) {
            continue;
        } else {
            envValues.put(key.toString(), params.get(key));
        }
    }

    String path = getTemplatePath(env.getTemplate().getName(), withArgument);
    Template template = env.getConfiguration().getTemplate(path + ".ftl");
    template.process(envValues, env.getOut());
}
 
源代码4 项目: spring-analysis-note   文件: FreeMarkerView.java
/**
 * Build a FreeMarker template model for the given model Map.
 * <p>The default implementation builds a {@link AllHttpScopesHashModel}.
 * @param model the model to use for rendering
 * @param request current HTTP request
 * @param response current servlet response
 * @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof
 */
protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request,
		HttpServletResponse response) {

	AllHttpScopesHashModel fmModel = new AllHttpScopesHashModel(getObjectWrapper(), getServletContext(), request);
	fmModel.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory);
	fmModel.put(FreemarkerServlet.KEY_APPLICATION, this.servletContextHashModel);
	fmModel.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response));
	fmModel.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper()));
	fmModel.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request));
	fmModel.putAll(model);
	return fmModel;
}
 
源代码5 项目: java-technology-stack   文件: FreeMarkerView.java
/**
 * Build a FreeMarker template model for the given model Map.
 * <p>The default implementation builds a {@link AllHttpScopesHashModel}.
 * @param model the model to use for rendering
 * @param request current HTTP request
 * @param response current servlet response
 * @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof
 */
protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request,
		HttpServletResponse response) {

	AllHttpScopesHashModel fmModel = new AllHttpScopesHashModel(getObjectWrapper(), getServletContext(), request);
	fmModel.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory);
	fmModel.put(FreemarkerServlet.KEY_APPLICATION, this.servletContextHashModel);
	fmModel.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response));
	fmModel.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper()));
	fmModel.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request));
	fmModel.putAll(model);
	return fmModel;
}
 
源代码6 项目: lams   文件: FreeMarkerView.java
/**
 * Build a FreeMarker template model for the given model Map.
 * <p>The default implementation builds a {@link AllHttpScopesHashModel}.
 * @param model the model to use for rendering
 * @param request current HTTP request
 * @param response current servlet response
 * @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof
 */
protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
	AllHttpScopesHashModel fmModel = new AllHttpScopesHashModel(getObjectWrapper(), getServletContext(), request);
	fmModel.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory);
	fmModel.put(FreemarkerServlet.KEY_APPLICATION, this.servletContextHashModel);
	fmModel.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response));
	fmModel.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper()));
	fmModel.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request));
	fmModel.putAll(model);
	return fmModel;
}
 
源代码7 项目: scipio-erp   文件: MultiVarMethod.java
public TemplateHashModelEx toMapModel(Environment env) {
    SimpleHash map = new SimpleHash(env.getObjectWrapper());
    map.put("ctxVars", ctxVars);
    map.put("globalCtxVars", globalCtxVars);
    map.put("reqAttribs", reqAttribs);
    return map;
}
 
源代码8 项目: scipio-erp   文件: LangFtlUtil.java
/**
 * Adds to simple hash from source map.
 * <p>
 * <em>WARN</em>: This is not BeanModel-aware (complex map).
 */
public static void addToSimpleMap(SimpleHash dest, TemplateHashModelEx source) throws TemplateModelException {
    TemplateCollectionModel keysModel = source.keys();
    TemplateModelIterator modelIt = keysModel.iterator();
    while(modelIt.hasNext()) {
        String key = getAsStringNonEscaping((TemplateScalarModel) modelIt.next());
        dest.put(key, source.get(key));
    }
}
 
源代码9 项目: spring4-understanding   文件: FreeMarkerView.java
/**
 * Build a FreeMarker template model for the given model Map.
 * <p>The default implementation builds a {@link AllHttpScopesHashModel}.
 * @param model the model to use for rendering
 * @param request current HTTP request
 * @param response current servlet response
 * @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof
 */
protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
	AllHttpScopesHashModel fmModel = new AllHttpScopesHashModel(getObjectWrapper(), getServletContext(), request);
	fmModel.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory);
	fmModel.put(FreemarkerServlet.KEY_APPLICATION, this.servletContextHashModel);
	fmModel.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response));
	fmModel.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper()));
	fmModel.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request));
	fmModel.putAll(model);
	return fmModel;
}
 
/****
	 * must be invoke after contruction
	 */
	public void initialize() {
		if(isInitialized())
			return ;
		
		TemplateLoader loader = getTempateLoader();
		Assert.notNull(loader);
		try {
			this.configuration = new Configuration(Configuration.VERSION_2_3_0);
			this.configuration.setObjectWrapper(getBeansWrapper());
			this.configuration.setOutputEncoding(this.encoding);
			//设置默认不自动格式化数字……以防sb……
			this.configuration.setNumberFormat("#");
//			this.cfg.setDirectoryForTemplateLoading(new File(templateDir));
			
			/*if(templateProvider!=null){
				this.configuration.setTemplateLoader(new DynamicTemplateLoader(templateProvider));
			}*/
			if(LangUtils.isNotEmpty(getFreemarkerVariables()))
				configuration.setAllSharedVariables(new SimpleHash(freemarkerVariables, configuration.getObjectWrapper()));
			
			//template loader
			/*if(!LangUtils.isEmpty(templatePaths)){
				TemplateLoader loader = FtlUtils.getTemplateLoader(resourceLoader, templatePaths);
				this.configuration.setTemplateLoader(loader);
			}*/
			this.configuration.setTemplateLoader(loader);
			
			this.buildConfigration(this.configuration);
			
			initialized = true;
		} catch (Exception e) {
			throw new BaseException("create freemarker template error : " + e.getMessage(), e);
		}
	}
 
源代码11 项目: mwsc   文件: NanoWSClientModule.java
@Override
public Set<FileInfo> generate(WSCodeGenModel cgModel, CGConfig config)
		throws WscModuleException {
	// freemarker datamodel
	SimpleHash fmModel = this.getFreemarkerModel();
	
	// container for target codes
	Set<FileInfo> targetFileSet = new HashSet<FileInfo>();
	
	info("Generating the Nano web serivce client classes...");
	
	fmModel.put("group", config.picoServiceGroup);
	fmModel.put("config", config);
	
	// generate endpoint interface
	for (SEIInfo interfaceInfo : cgModel.getServiceEndpointInterfaces()) {
		fmModel.put("imports", this.getInterfaceImports(interfaceInfo));
		fmModel.put("endpointInterface", interfaceInfo);

		String relativePath = ClassNameUtil.packageNameToPath(interfaceInfo.getPackageName());
		relativePath += File.separator + "client";
		FileInfo eiSoapClient = this.generateFile(soapClientTemplate, fmModel, interfaceInfo.getName() + "_SOAPClient", "java", relativePath);
		targetFileSet.add(eiSoapClient);
		FileInfo eiXmlClient = this.generateFile(xmlClientTemplate, fmModel, interfaceInfo.getName() + "_XMLClient", "java", relativePath);
		targetFileSet.add(eiXmlClient);
	}
	
	return targetFileSet;
}
 
源代码12 项目: engine   文件: SiteContextResolvingFilter.java
protected void renderError(HttpServletResponse response) {
    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    try {
        Configuration configuration = freeMarkerConfigFactory.getObject().getConfiguration();
        Template template = configuration.getTemplate(errorTemplate);
        SimpleHash model = new SimpleHash(configuration.getObjectWrapper());
        configuration.setAllSharedVariables(model);
        template.process(model, response.getWriter());
    } catch (Exception e) {
        logger.error("Error rendering template for site resolving error", e);
    }
}
 
源代码13 项目: engine   文件: RenderComponentDirective.java
@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException {
    TemplateModel componentParentParam = (TemplateModel) params.get(COMPONENT_PARENT_PARAM_NAME);
    TemplateModel componentParam = (TemplateModel) params.get(COMPONENT_PARAM_NAME);
    TemplateModel componentPathParam = (TemplateModel) params.get(COMPONENT_PATH_PARAM_NAME);
    TemplateModel additionalModelParam = (TemplateModel) params.get(ADDITIONAL_MODEL_PARAM_NAME);
    Map<String, Object> additionalModel = null;
    SiteItem component;

    if (componentParam == null && componentPathParam == null) {
        throw new TemplateException("No '" + COMPONENT_PARAM_NAME + "' or '" + COMPONENT_PATH_PARAM_NAME +
                                    "' param specified", env);
    } else if (componentParam != null) {
        component = getComponentFromNode(componentParentParam, componentParam, env);
    } else {
        component = getComponentFromPath(componentPathParam, env);
    }

    if (additionalModelParam != null) {
        additionalModel = unwrap(ADDITIONAL_MODEL_PARAM_NAME, additionalModelParam, Map.class, env);
    }

    Map<String, Object> templateModel = executeScripts(component, additionalModel, env);
    SimpleHash model = getFullModel(component, templateModel, additionalModel);
    Template template = getTemplate(component, env);
    Writer output = env.getOut();

    processComponentTemplate(template, model, output, env);
}
 
源代码14 项目: engine   文件: RenderComponentDirective.java
protected SimpleHash getFullModel(SiteItem component, Map<String, Object> templateModel,
                                  Map<String, Object> additionalModel) throws TemplateException {
    SimpleHash model = modelFactory.getObject();
    model.put(KEY_MODEL, component);
    model.put(KEY_CONTENT_MODEL, component);

    if (MapUtils.isNotEmpty(templateModel)) {
        model.putAll(templateModel);
    }
    if (MapUtils.isNotEmpty(additionalModel)) {
        model.putAll(additionalModel);
    }

    return model;
}
 
源代码15 项目: engine   文件: RenderComponentDirective.java
protected void processComponentTemplate(Template template, SimpleHash model, Writer output, Environment env)
    throws TemplateException {
    try {
        template.process(model, output);
    } catch (IOException e) {
        throw new TemplateException("I/O exception while processing the component template", e, env);
    }
}
 
源代码16 项目: javalite   文件: WrapTag.java
private SimpleHash getHash(Environment env) throws TemplateModelException {
    Set names = env.getKnownVariableNames();
    SimpleHash simpleHash = new SimpleHash();
    for(Object name: names){
        simpleHash.put(name.toString(),env.getVariable(name.toString()));
    }
    return simpleHash;
}
 
/**
 * Prepare the FreeMarker Configuration and return it.
 * @return the FreeMarker Configuration object
 * @throws IOException if the config file wasn't found
 * @throws TemplateException on FreeMarker initialization failure
 */
public Configuration createConfiguration() throws IOException, TemplateException {
	Configuration config = newConfiguration();
	Properties props = new Properties();

	// Load config file if specified.
	if (this.configLocation != null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Loading FreeMarker configuration from " + this.configLocation);
		}
		PropertiesLoaderUtils.fillProperties(props, this.configLocation);
	}

	// Merge local properties if specified.
	if (this.freemarkerSettings != null) {
		props.putAll(this.freemarkerSettings);
	}

	// FreeMarker will only accept known keys in its setSettings and
	// setAllSharedVariables methods.
	if (!props.isEmpty()) {
		config.setSettings(props);
	}

	if (!CollectionUtils.isEmpty(this.freemarkerVariables)) {
		config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper()));
	}

	if (this.defaultEncoding != null) {
		config.setDefaultEncoding(this.defaultEncoding);
	}

	List<TemplateLoader> templateLoaders = new ArrayList<>(this.templateLoaders);

	// Register template loaders that are supposed to kick in early.
	if (this.preTemplateLoaders != null) {
		templateLoaders.addAll(this.preTemplateLoaders);
	}

	// Register default template loaders.
	if (this.templateLoaderPaths != null) {
		for (String path : this.templateLoaderPaths) {
			templateLoaders.add(getTemplateLoaderForPath(path));
		}
	}
	postProcessTemplateLoaders(templateLoaders);

	// Register template loaders that are supposed to kick in late.
	if (this.postTemplateLoaders != null) {
		templateLoaders.addAll(this.postTemplateLoaders);
	}

	TemplateLoader loader = getAggregateTemplateLoader(templateLoaders);
	if (loader != null) {
		config.setTemplateLoader(loader);
	}

	postProcessConfiguration(config);
	return config;
}
 
/**
 * Prepare the FreeMarker Configuration and return it.
 * @return the FreeMarker Configuration object
 * @throws IOException if the config file wasn't found
 * @throws TemplateException on FreeMarker initialization failure
 */
public Configuration createConfiguration() throws IOException, TemplateException {
	Configuration config = newConfiguration();
	Properties props = new Properties();

	// Load config file if specified.
	if (this.configLocation != null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Loading FreeMarker configuration from " + this.configLocation);
		}
		PropertiesLoaderUtils.fillProperties(props, this.configLocation);
	}

	// Merge local properties if specified.
	if (this.freemarkerSettings != null) {
		props.putAll(this.freemarkerSettings);
	}

	// FreeMarker will only accept known keys in its setSettings and
	// setAllSharedVariables methods.
	if (!props.isEmpty()) {
		config.setSettings(props);
	}

	if (!CollectionUtils.isEmpty(this.freemarkerVariables)) {
		config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper()));
	}

	if (this.defaultEncoding != null) {
		config.setDefaultEncoding(this.defaultEncoding);
	}

	List<TemplateLoader> templateLoaders = new ArrayList<>(this.templateLoaders);

	// Register template loaders that are supposed to kick in early.
	if (this.preTemplateLoaders != null) {
		templateLoaders.addAll(this.preTemplateLoaders);
	}

	// Register default template loaders.
	if (this.templateLoaderPaths != null) {
		for (String path : this.templateLoaderPaths) {
			templateLoaders.add(getTemplateLoaderForPath(path));
		}
	}
	postProcessTemplateLoaders(templateLoaders);

	// Register template loaders that are supposed to kick in late.
	if (this.postTemplateLoaders != null) {
		templateLoaders.addAll(this.postTemplateLoaders);
	}

	TemplateLoader loader = getAggregateTemplateLoader(templateLoaders);
	if (loader != null) {
		config.setTemplateLoader(loader);
	}

	postProcessConfiguration(config);
	return config;
}
 
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;
}
 
源代码20 项目: lams   文件: FreeMarkerConfigurationFactory.java
/**
 * Prepare the FreeMarker Configuration and return it.
 * @return the FreeMarker Configuration object
 * @throws IOException if the config file wasn't found
 * @throws TemplateException on FreeMarker initialization failure
 */
public Configuration createConfiguration() throws IOException, TemplateException {
	Configuration config = newConfiguration();
	Properties props = new Properties();

	// Load config file if specified.
	if (this.configLocation != null) {
		if (logger.isInfoEnabled()) {
			logger.info("Loading FreeMarker configuration from " + this.configLocation);
		}
		PropertiesLoaderUtils.fillProperties(props, this.configLocation);
	}

	// Merge local properties if specified.
	if (this.freemarkerSettings != null) {
		props.putAll(this.freemarkerSettings);
	}

	// FreeMarker will only accept known keys in its setSettings and
	// setAllSharedVariables methods.
	if (!props.isEmpty()) {
		config.setSettings(props);
	}

	if (!CollectionUtils.isEmpty(this.freemarkerVariables)) {
		config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper()));
	}

	if (this.defaultEncoding != null) {
		config.setDefaultEncoding(this.defaultEncoding);
	}

	List<TemplateLoader> templateLoaders = new LinkedList<TemplateLoader>(this.templateLoaders);

	// Register template loaders that are supposed to kick in early.
	if (this.preTemplateLoaders != null) {
		templateLoaders.addAll(this.preTemplateLoaders);
	}

	// Register default template loaders.
	if (this.templateLoaderPaths != null) {
		for (String path : this.templateLoaderPaths) {
			templateLoaders.add(getTemplateLoaderForPath(path));
		}
	}
	postProcessTemplateLoaders(templateLoaders);

	// Register template loaders that are supposed to kick in late.
	if (this.postTemplateLoaders != null) {
		templateLoaders.addAll(this.postTemplateLoaders);
	}

	TemplateLoader loader = getAggregateTemplateLoader(templateLoaders);
	if (loader != null) {
		config.setTemplateLoader(loader);
	}

	postProcessConfiguration(config);
	return config;
}
 
protected Configuration getInternalEngine() throws IOException, TemplateException{
	
	try {
		BeansWrapper beansWrapper = new BeansWrapper(Configuration.VERSION_2_3_23);
		this.templateModel = beansWrapper.getStaticModels().get(String.class.getName());
	} catch (TemplateModelException e) {
		throw new IOException(e.getMessage(),e.getCause());
	}

	// 创建 Configuration 实例
	Configuration config = new Configuration(Configuration.VERSION_2_3_23);
	
	Properties props = ConfigUtils.filterWithPrefix("docx4j.freemarker.", "docx4j.freemarker.", Docx4jProperties.getProperties(), false);

	// FreeMarker will only accept known keys in its setSettings and
	// setAllSharedVariables methods.
	if (!props.isEmpty()) {
		config.setSettings(props);
	}

	if (this.freemarkerVariables != null && !this.freemarkerVariables.isEmpty()) {
		config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper()));
	}

	if (this.defaultEncoding != null) {
		config.setDefaultEncoding(this.defaultEncoding);
	}

	List<TemplateLoader> templateLoaders = new LinkedList<TemplateLoader>(this.templateLoaders);
	
	// Register template loaders that are supposed to kick in early.
	if (this.preTemplateLoaders != null) {
		templateLoaders.addAll(this.preTemplateLoaders);
	}
	
	postProcessTemplateLoaders(templateLoaders);
	
	// Register template loaders that are supposed to kick in late.
	if (this.postTemplateLoaders != null) {
		templateLoaders.addAll(this.postTemplateLoaders);
	}
	
	TemplateLoader loader = getAggregateTemplateLoader(templateLoaders);
	if (loader != null) {
		config.setTemplateLoader(loader);
	}
	//config.setClassLoaderForTemplateLoading(classLoader, basePackagePath);
	//config.setCustomAttribute(name, value);
	//config.setDirectoryForTemplateLoading(dir);
	//config.setServletContextForTemplateLoading(servletContext, path);
	//config.setSharedVariable(name, value);
	//config.setSharedVariable(name, tm);
	config.setSharedVariable("fmXmlEscape", new XmlEscape());
	config.setSharedVariable("fmHtmlEscape", new HtmlEscape());
	//config.setSharedVaribles(map);
	
	// 设置模板引擎,减少重复初始化消耗
       this.setEngine(config);
       return config;
}
 
源代码22 项目: scipio-erp   文件: MakeAttribMapFromArgMapMethod.java
@Override
public Object exec(@SuppressWarnings("rawtypes") List args) throws TemplateModelException {
    if (args == null || args.size() < 1 || args.size() > 2 ) {
        throw new TemplateModelException("Invalid number of arguments (expected: 1-2)");
    }

    ObjectWrapper objectWrapper = CommonFtlUtil.getCurrentEnvironment().getObjectWrapper();

    // support empty list (ignore, treat as empty hash)
    TemplateModel argsObj = (TemplateModel) args.get(0);
    if (argsObj instanceof TemplateSequenceModel) {
        TemplateSequenceModel argsSeq = (TemplateSequenceModel) argsObj;
        if (argsSeq.size() == 0) {
            argsObj = TemplateHashModelEx.NOTHING;
        } else {
            throw new TemplateModelException("Invalid argument type (sequence) - expected hash");
        }
    }

    TemplateHashModelEx argsMap = (TemplateHashModelEx) argsObj;

    // caller-supplied excludes
    TemplateModel excludesModel = (args.size() >=2) ? (TemplateModel) args.get(1) : null;
    Set<String> excludes;
    if (excludesModel != null) {
        excludes = LangFtlUtil.getAsStringSet(excludesModel);
    } else {
        excludes = new HashSet<>();
    }

    SimpleHash res = null;

    final Boolean useExclude = Boolean.FALSE;

    // put attribs from explicit attribs map first, if any
    TemplateModel attribsModel = argsMap.get("attribs");
    if (attribsModel != null && OfbizFtlObjectType.isObjectType(OfbizFtlObjectType.MAP, attribsModel)) {
        if (OfbizFtlObjectType.isObjectType(OfbizFtlObjectType.COMPLEXMAP, attribsModel)) {
            attribsModel = LangFtlUtil.toSimpleMap(attribsModel, false, objectWrapper);
        }
        res = LangFtlUtil.copyMapToSimple((TemplateHashModel) attribsModel, excludes, useExclude, objectWrapper);
    }

    // to get inline attribs, add list of all arg names to excludes as well as the lists themselves
    TemplateModel allArgNamesModel = argsMap.get("allArgNames");
    if (allArgNamesModel != null) {
        excludes.addAll(LangFtlUtil.getAsStringSet(allArgNamesModel));
    }
    excludes.add("allArgNames");
    excludes.add("localArgNames");
    excludes.add("attribs"); // 2020-02-12: in most cases this was automatically added by makeArgMaps from default args list, but custom usages need this now

    // add the inline attribs over the attribs map (if any)
    if (res == null) {
        res = LangFtlUtil.copyMapToSimple(argsMap, excludes, useExclude, objectWrapper);
    } else {
        LangFtlUtil.putAll(res, argsMap, excludes, useExclude, objectWrapper);
    }

    return res;
}
 
源代码23 项目: scipio-erp   文件: LangFtlUtil.java
public static SimpleHash copyMapToSimple(TemplateHashModel hashModel, Set<String> inExKeys, Boolean include, ObjectWrapper objectWrapper) throws TemplateModelException {
    SimpleHash res = new SimpleHash(objectWrapper);
    putAll(res, hashModel, inExKeys, include, objectWrapper);
    return res;
}
 
源代码24 项目: scipio-erp   文件: LangFtlUtil.java
public static TemplateHashModelEx makeSimpleMapCopy(Map<?, ?> map, ObjectWrapper objectWrapper) throws TemplateModelException {
    return new SimpleHash(map, objectWrapper);
}
 
源代码25 项目: scipio-erp   文件: LangFtlUtil.java
public static void addToSimpleMap(SimpleHash dest, TemplateHashModel source, Set<String> keys) throws TemplateModelException {
    for(String key : keys) {
        dest.put(key, source.get(key));
    }
}
 
源代码26 项目: scipio-erp   文件: LangFtlUtil.java
/**
 * Makes a simple hash from source map; only specified keys.
 * <p>
 * <em>WARN</em>: This is not BeanModel-aware (complex map).
 */
public static SimpleHash makeSimpleMap(TemplateHashModel map, Set<String> keys, ObjectWrapper objectWrapper) throws TemplateModelException {
    SimpleHash res = new SimpleHash(objectWrapper);
    addToSimpleMap(res, map, keys);
    return res;
}
 
源代码27 项目: scipio-erp   文件: LangFtlUtil.java
public static SimpleHash makeSimpleMap(TemplateHashModel map, TemplateCollectionModel keys, ObjectWrapper objectWrapper) throws TemplateModelException {
    SimpleHash res = new SimpleHash(objectWrapper);
    addToSimpleMap(res, map, LangFtlUtil.toStringSet(keys));
    return res;
}
 
源代码28 项目: scipio-erp   文件: LangFtlUtil.java
public static SimpleHash makeSimpleMap(TemplateHashModel map, TemplateSequenceModel keys, ObjectWrapper objectWrapper) throws TemplateModelException {
    SimpleHash res = new SimpleHash(objectWrapper);
    addToSimpleMap(res, map, LangFtlUtil.toStringSet(keys));
    return res;
}
 
/**
 * Prepare the FreeMarker Configuration and return it.
 * @return the FreeMarker Configuration object
 * @throws IOException if the config file wasn't found
 * @throws TemplateException on FreeMarker initialization failure
 */
public Configuration createConfiguration() throws IOException, TemplateException {
	Configuration config = newConfiguration();
	Properties props = new Properties();

	// Load config file if specified.
	if (this.configLocation != null) {
		if (logger.isInfoEnabled()) {
			logger.info("Loading FreeMarker configuration from " + this.configLocation);
		}
		PropertiesLoaderUtils.fillProperties(props, this.configLocation);
	}

	// Merge local properties if specified.
	if (this.freemarkerSettings != null) {
		props.putAll(this.freemarkerSettings);
	}

	// FreeMarker will only accept known keys in its setSettings and
	// setAllSharedVariables methods.
	if (!props.isEmpty()) {
		config.setSettings(props);
	}

	if (!CollectionUtils.isEmpty(this.freemarkerVariables)) {
		config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper()));
	}

	if (this.defaultEncoding != null) {
		config.setDefaultEncoding(this.defaultEncoding);
	}

	List<TemplateLoader> templateLoaders = new LinkedList<TemplateLoader>(this.templateLoaders);

	// Register template loaders that are supposed to kick in early.
	if (this.preTemplateLoaders != null) {
		templateLoaders.addAll(this.preTemplateLoaders);
	}

	// Register default template loaders.
	if (this.templateLoaderPaths != null) {
		for (String path : this.templateLoaderPaths) {
			templateLoaders.add(getTemplateLoaderForPath(path));
		}
	}
	postProcessTemplateLoaders(templateLoaders);

	// Register template loaders that are supposed to kick in late.
	if (this.postTemplateLoaders != null) {
		templateLoaders.addAll(this.postTemplateLoaders);
	}

	TemplateLoader loader = getAggregateTemplateLoader(templateLoaders);
	if (loader != null) {
		config.setTemplateLoader(loader);
	}

	postProcessConfiguration(config);
	return config;
}
 
源代码30 项目: mwsc   文件: PicoWSClientModule.java
@Override
public Set<FileInfo> generate(WSCodeGenModel cgModel, CGConfig config)
		throws WscModuleException {
	// freemarker datamodel
	SimpleHash fmModel = this.getFreemarkerModel();
	
	// container for target codes
	Set<FileInfo> targetFileSet = new HashSet<FileInfo>();
	
	info("Generating the Pico web serivce client classes...");
	
	if (config.picoPrefix == null) {
		warn("No prefix is provided, it's recommended to add prefix for Pico binding to avoid possible conflict");
	}
	String prefix = config.picoPrefix == null ? "" : config.picoPrefix;
	prefixType(cgModel, prefix);
	
	fmModel.put("group", config.picoServiceGroup);
	
	// generate endpoint interface
	for (SEIInfo interfaceInfo : cgModel.getServiceEndpointInterfaces()) {
		fmModel.put("imports", this.getInterfaceImports(interfaceInfo));
		fmModel.put("endpointInterface", interfaceInfo);
		// special logic for ebay service demo, just a convenient for ebay service proxy generation
		if (config.eBaySOAService) {
			fmModel.put("eBaySOAService", config.eBaySOAService);
		} else if (config.eBayShoppingAPI) {
			fmModel.put("eBayShoppingAPI", config.eBayShoppingAPI);
		} else if (config.eBayTradingAPI) {
			fmModel.put("eBayTradingAPI", config.eBayTradingAPI);
		}
		String relativePath = ClassNameUtil.packageNameToPath(interfaceInfo.getPackageName());
		relativePath += File.separator + "client";
		FileInfo eiSoapIntf = this.generateFile(eiIntfSOAPTemplate, fmModel, interfaceInfo.getName() + "_SOAPClient", "h", relativePath);
		targetFileSet.add(eiSoapIntf);
		FileInfo eiSoapImpl = this.generateFile(eiImplSOAPTemplate, fmModel, interfaceInfo.getName() + "_SOAPClient", "m", relativePath);
		targetFileSet.add(eiSoapImpl);
		FileInfo eiXmlIntf = this.generateFile(eiIntfXMLTemplate, fmModel, interfaceInfo.getName() + "_XMLClient", "h", relativePath);
		targetFileSet.add(eiXmlIntf);
		FileInfo eiXmlImpl = this.generateFile(eiImplXMLTemplate, fmModel, interfaceInfo.getName() + "_XMLClient", "m", relativePath);
		targetFileSet.add(eiXmlImpl);
	}
	
	return targetFileSet;
}