private void addHandlerFactoryConfig(Class handlerFactory, String type) { SmooksResourceConfiguration res = new SmooksResourceConfiguration(CDU_CREATOR); res.setTargetProfile("*"); res.setResource(handlerFactory.getName()); res.setParameter(ContentHandlerFactory.PARAM_RESTYPE, type); defaultList.add(res); }
/** * Public constructor. * @param resourceConfig Configuration. */ public void setConfiguration(SmooksResourceConfiguration resourceConfig) { delims = resourceConfig.getStringParameter("delims", ","); returnDelims = resourceConfig.getBoolParameter("returnDelims", false); trimTokens = resourceConfig.getBoolParameter("trimTokens", true); String paramType = resourceConfig.getStringParameter(Parameter.PARAM_TYPE_PREFIX, "string-list"); if(paramType.equals("string-list")) { returnType = Vector.class; } else if(paramType.equals("string-hashset")) { returnType = LinkedHashSet.class; } else { throw new ParameterDecodeException("Unsupported decoded return type [" + paramType + "]"); } }
public String getResourceType() { String restype; if (isJavaResource()) { return "class"; } restype = getStringParameter(PARAM_RESTYPE); if (restype != null && !restype.trim().equals("")) { // Ala DTD v1.0, where we weren't able to specify the type in any other way. if (getParameter(PARAM_RESDATA) == null) { logger.debug("Resource configuration defined with '" + PARAM_RESTYPE + "' parameter but no '" + PARAM_RESDATA + "' parameter."); } } else if (resourceType != null) { // Ala DTD v2.0, where the type is set through the "type" attribute on the <resource> element. restype = resourceType; } else { restype = getExtension(getResource()); } return restype; }
/** * Public default constructor. * * @see #setSelector(String) * @see #setSelectorNamespaceURI(String) * @see #setTargetProfile(String) * @see #setResource(String) * @see #setResourceType(String) * @see #setParameter(String, String) */ public SmooksResourceConfiguration() { setSelector(SELECTOR_NONE); setTargetProfile(Profile.DEFAULT_PROFILE); }
/** * Public constructor. * * @param selector The selector definition. * @param targetProfile Target Profile(s). Comma separated list of * {@link ProfileTargetingExpression ProfileTargetingExpressions}. * @param resource The resource. * @see #setSelectorNamespaceURI(String) * @see #setResourceType(String) * @see #setParameter(String, String) */ public SmooksResourceConfiguration(String selector, String targetProfile, String resource) { this(selector); setTargetProfile(targetProfile); setResource(resource); }
public SmooksResourceConfiguration createConfiguration() { SmooksResourceConfiguration config = new SmooksResourceConfiguration(); config.setParameter("executeBefore", Boolean.toString(executeBefore)); return config; }
private Object createFromTemplate(String groovyScript, SmooksResourceConfiguration configuration) throws InstantiationException, IllegalAccessException { GroovyClassLoader groovyClassLoader = new GroovyClassLoader(getClass().getClassLoader()); Map<String, Object> templateVars = new HashMap<String, Object>(); String imports = configuration.getStringParameter("imports", ""); templateVars.put("imports", cleanImportsConfig(imports)); templateVars.put("visitorName", createClassName()); templateVars.put("elementName", getElementName(configuration)); templateVars.put("visitBefore", configuration.getBoolParameter("executeBefore", false)); templateVars.put("visitorScript", groovyScript); String templatedClass = classTemplate.apply(templateVars); if(groovyScript.contains("writeFragment")) { configuration.setParameter("writeFragment", "true"); } try { Class groovyClass = groovyClassLoader.parseClass(templatedClass); return groovyClass.newInstance(); } catch(CompilationFailedException e) { throw new SmooksConfigurationException("Failed to compile Groovy scripted Visitor class:\n==========================\n" + templatedClass + "\n==========================\n", e); } }
public ContentHandler create(SmooksResourceConfiguration configuration) throws SmooksConfigurationException { try { byte[] groovyScriptBytes = configuration.getBytes(); String groovyScript = new String(groovyScriptBytes, "UTF-8"); Object groovyObject; GroovyClassLoader groovyClassLoader = new GroovyClassLoader(getClass().getClassLoader()); try { Class groovyClass = groovyClassLoader.parseClass(groovyScript); groovyObject = groovyClass.newInstance(); } catch(CompilationFailedException e) { logger.debug("Failed to create Visitor class instance directly from script:\n==========================\n" + groovyScript + "\n==========================\n Will try applying Visitor template to script.", e); groovyObject = null; } if(!(groovyObject instanceof Visitor)) { groovyObject = createFromTemplate(groovyScript, configuration); } ContentHandler groovyResource = (ContentHandler) groovyObject; Configurator.configure(groovyResource, configuration); return groovyResource; } catch (Exception e) { throw new SmooksConfigurationException("Error constructing class from Groovy script " + configuration.getResource(), e); } }
/** * Used by Smooks to retrieve the visitor configuration of this Value Configuration */ public void addVisitors(VisitorConfigMap visitorMap) { ValueBinder binder = new ValueBinder(getBeanId()); SmooksResourceConfiguration populatorConfig = new SmooksResourceConfiguration(dataSelector); SelectorPropertyResolver.resolveSelectorTokens(populatorConfig); binder.setDecoder(decoder); binder.setDefaultValue(defaultValue); binder.setValueAttributeName(populatorConfig.getStringParameter(BeanInstancePopulator.VALUE_ATTRIBUTE_NAME)); visitorMap.addVisitor(binder, populatorConfig.getSelector(), targetNamespace, true); }
public void visitBefore(Element element, ExecutionContext executionContext) throws SmooksException { ExtensionContext extensionContext = ExtensionContext.getExtensionContext(executionContext); // The current config on the stack must be a <jb:bean>... SmooksResourceConfiguration beanConfig = (SmooksResourceConfiguration) extensionContext.getResourceStack().peek(); String retain = beanConfig.getStringParameter("retain"); // If the "retain" attribute is not configured we configure it. If // this is the first bean config, we set it to "true" (i.e. retain it), // otherwise set it to "false" (i.e. do not retain it)... if(retain == null) { List<SmooksResourceConfiguration> creatorConfigs = extensionContext.lookupResource(new ConfigSearch().resource(BeanInstanceCreator.class.getName())); if(!creatorConfigs.isEmpty()) { // This is not the first bean config... set retain to "false" beanConfig.setParameter("retain", "false"); } } } }
public SmooksResourceConfiguration addVisitor(Visitor visitor, String targetSelector, String targetSelectorNS, boolean configure) { AssertArgument.isNotNull(visitor, "visitor"); AssertArgument.isNotNull(targetSelector, "targetSelector"); SmooksResourceConfiguration resourceConfig; if (visitor instanceof SmooksResourceConfigurationFactory) { resourceConfig = ((SmooksResourceConfigurationFactory)visitor).createConfiguration(); resourceConfig.setResource(visitor.getClass().getName()); resourceConfig.setSelector(targetSelector); } else { resourceConfig = new SmooksResourceConfiguration(targetSelector, visitor.getClass().getName()); } resourceConfig.setSelectorNamespaceURI(targetSelectorNS); addVisitor(visitor, resourceConfig, configure); return resourceConfig; }
/** * Add the supplied resource configuration to this configuration's main * resource configuration list. * @param config The configuration to be added. */ private void addResourceConfiguration(SmooksResourceConfiguration config) { String target = config.getSelector(); // If it's contextual, it's targeting an XML element... if(config.isSelectorContextual()) { target = config.getTargetElement(); } addResourceConfiguration(target, config); }
/** * Get the named SmooksResourceConfiguration parameter. * * @param name Name of parameter to get. * @return Parameter value, or null if not set. */ public String getStringParameter(String name) { Parameter parameter = getParameter(name); return (parameter != null ? parameter.value : null); }
public void visitBefore(Element element, ExecutionContext executionContext) throws SmooksException { ExtensionContext extensionContext = ExtensionContext.getExtensionContext(executionContext); SmooksResourceConfiguration config = extensionContext.getResourceStack().peek(); if(config.getSelector() == null || config.getSelector().equals("none")) { Parameter beanIdParam = config.getParameter("beanId"); String beanId = beanIdParam.getValue(); SmooksResourceConfiguration beanCreatorConfig = findBeanCreatorConfig(beanId, extensionContext); if(beanCreatorConfig == null) { throw new SmooksConfigurationException("No <jb:bean> configurations is found yet for beanId '" + beanId + "'. " + "This can mean that no <jb:bean> is present that creates the bean with the bean id or that it is configured after the <" + element.getNodeName() + ">. " + "In this case you must set the selector in the '" + selectorAttrName + "' attribute."); } else { config.setSelector(beanCreatorConfig.getSelector()); } } }
private void digestParameters(Element resourceConfigElement, SmooksResourceConfiguration resourceConfig) { NodeList configNodes = resourceConfigElement.getElementsByTagName("param"); for (int i = 0; i < configNodes.getLength(); i++) { Element paramNode = (Element) configNodes.item(i); String paramName = DomUtils.getAttributeValue(paramNode, "name"); String paramType = DomUtils.getAttributeValue(paramNode, "type"); String paramValue = DomUtils.getAllText(paramNode, true); Parameter paramInstance; paramInstance = resourceConfig.setParameter(paramName, paramType, paramValue); paramInstance.setXML(paramNode); } }
private void assertSelectorsNotAccessingText(ContentHandlerConfigMapTable saxVisitorMap) { Map<String, List<ContentHandlerConfigMap<? extends SAXVisitor>>> table = saxVisitorMap.getTable(); Collection<List<ContentHandlerConfigMap<? extends SAXVisitor>>> contentHandlerMaps = table.values(); for(List<ContentHandlerConfigMap<? extends SAXVisitor>> contentHandlerMapList : contentHandlerMaps) { for(ContentHandlerConfigMap<? extends SAXVisitor> contentHandlerMap : contentHandlerMapList) { SmooksResourceConfiguration resourceConfig = contentHandlerMap.getResourceConfig(); SelectorStep selectorStep = resourceConfig.getSelectorStep(); if(selectorStep.accessesText()) { throw new SmooksConfigurationException("Unsupported selector '" + selectorStep.getXPathExpression() + "' on resource '" + resourceConfig + "'. The 'text()' XPath token is only supported on SAX Visitor implementations that implement the " + SAXVisitAfter.class.getName() + " interface only. Class '" + resourceConfig.getResource() + "' implements other SAX Visitor interfaces."); } } } }
@Override protected void loadTemplate(SmooksResourceConfiguration resourceConfig) throws IOException, TransformerConfigurationException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); StreamSource xslStreamSource; boolean isInlineXSL = resourceConfig.isInline(); byte[] xslBytes = resourceConfig.getBytes(); xslString = new String(xslBytes, getEncoding().name()); // If it's not a full XSL template, we need to make it so by wrapping it... isTemplatelet = isTemplatelet(isInlineXSL, new String(xslBytes)); if (isTemplatelet) { String templateletWrapper = new String(StreamUtils.readStream(ClassUtil.getResourceAsStream("doc-files/templatelet.xsl", getClass()))); String templatelet = new String(xslBytes); templateletWrapper = StringUtils.replace(templateletWrapper, "@@@templatelet@@@", templatelet); xslBytes = templateletWrapper.getBytes(); xslString = new String(xslBytes, getEncoding().name()); } boolean failOnWarning = resourceConfig.getBoolParameter("failOnWarning", true); xslStreamSource = new StreamSource(new StringReader(xslString)); transformerFactory.setErrorListener(new XslErrorListener(failOnWarning)); xslTemplate = transformerFactory.newTemplates(xslStreamSource); }
configuration.setNumberFormat(defaultNumberFormat); if (config.isInline()) { byte[] templateBytes = config.getBytes(); String[] templates = (new String(templateBytes)).split(AbstractTemplateProcessor.TEMPLATE_SPLIT_PI); defaultTemplate = configuration.getTemplate(config.getResource()); } else { defaultTemplate = configuration.getTemplate(config.getResource());
public SmooksResourceConfiguration getResourceByName(String name) { for(int i = resourceStack.size() - 1; i >= 0; i--) { SmooksResourceConfiguration config = resourceStack.get(i); String resourceName = config.getResource(); if(name.equals(resourceName)) { return config; } } return null; }
public static void resolveSelectorTokens(SmooksResourceConfiguration populatorConfig) { QName valueAttributeQName = populatorConfig.getTargetAttributeQName(); if(valueAttributeQName != null) { String valueAttributeName = valueAttributeQName.getLocalPart(); String valueAttributePrefix = valueAttributeQName.getPrefix(); populatorConfig.setParameter(BeanInstancePopulator.VALUE_ATTRIBUTE_NAME, valueAttributeName); if(valueAttributePrefix != null && !valueAttributePrefix.trim().equals("")) { populatorConfig.setParameter(BeanInstancePopulator.VALUE_ATTRIBUTE_PREFIX, valueAttributePrefix); } } }