public void visitBefore(final SAXElement element, final ExecutionContext executionContext) throws SmooksException, IOException { if(targetAttribute == null) { // The selected text is not an attribute, which means it's the element text, // which means we need to turn on text accumulation for SAX... element.accumulateText(); } }
public Object getValue(SAXElement element) { String attribValue; if(namespaceURI != null) { attribValue = element.getAttributeNS(namespaceURI, localPart); } else { attribValue = element.getAttribute(localPart); } return decoder.decode(attribValue); }
private static void addXPathElement(SAXElement element, StringBuilder builder) { if(builder.length() > 0) { builder.insert(0, "/"); builder.insert(0, element.getName().getLocalPart()); } else { builder.append(element.getName().getLocalPart()); } SAXElement parent = element.getParent(); if(parent != null) { addXPathElement(parent, builder); } }
public void visitBefore(SAXElement element, ExecutionContext executionContext) throws SmooksException, IOException { // Claim ownership of the writer for this fragment element... Writer writer = element.getWriter(this); if(!keepChildren) { // Swap in a NullWriter instance for the whole fragment... element.setWriter(new NullWriter(), this); // Stash the real writer instance on the element so we can reset it at the end... element.setCache(this, writer); } else { // Just don't write this element, but write the child elements... } }
private SAXElement reconstructElement(SAXElement element) { QName qName = element.getName(); SAXElement newElement = new SAXElement(qName, element.getAttributes(), element.getParent()); newElement.setWriter(element.getWriter(this), this); String prefix = attribName.getPrefix(); if(prefix != null && prefix.length() > 0) { newElement.setAttributeNS(namespaceURI, prefix + ":" + attribName.getLocalPart(), valueTemplate.apply(beans)); } else { newElement.setAttributeNS(namespaceURI, attribName.getLocalPart(), valueTemplate.apply(beans)); newElement.setAttribute(attribName.getLocalPart(), valueTemplate.apply(beans));
private void bindSaxDataValue(SAXElement element, ExecutionContext executionContext) { String propertyName; if(mapKeyAttribute != null) { propertyName = SAXUtil.getAttribute(mapKeyAttribute, element.getAttributes(), null); if(propertyName == null) { propertyName = element.getName().getLocalPart(); } } else if(property != null) { propertyName = property; } else { propertyName = element.getName().getLocalPart(); } String dataString = null; if(expressionEvaluator == null || expressionHasDataVariable) { if (isAttribute) { if(valueAttributeNS != null) { dataString = SAXUtil.getAttribute(valueAttributeNS, valueAttributeName, element.getAttributes(), null); } else { dataString = SAXUtil.getAttribute(valueAttributeName, element.getAttributes(), null); } } else { dataString = element.getTextContent(); } } if(expressionEvaluator != null) { bindExpressionValue(propertyName, dataString, executionContext, new Fragment(element)); } else { decodeAndSetPropertyValue(propertyName, dataString, executionContext, new Fragment(element)); } }
private static void writeUnclosedElement(SAXElement element, Writer writer, boolean encodeSpecialChars) throws IOException { QName name = element.getName(); String prefix = name.getPrefix(); writer.write('<'); if(prefix != null && !prefix.equals(XMLConstants.NULL_NS_URI)) { writer.write(prefix); writer.write(':'); } writer.write(name.getLocalPart()); writeAttributes(element.getAttributes(), writer, encodeSpecialChars); }
public void visitBefore(SAXElement element, ExecutionContext executionContext) throws SmooksException, IOException { if(isAttribute) { bindValue(SAXUtil.getAttribute(valueAttributeName, element.getAttributes()), executionContext, new Fragment(element)); } else { // Turn on Text Accumulation... element.accumulateText(); } }
public void visitBefore(SAXElement element, ExecutionContext executionContext) throws SmooksException, IOException { // Do nothing here apart from acquiring ownership of the element writer. // See is there any child text/elements first... element.getWriter(writerOwner); }
private void addNamespace(String prefix, String namespaceURI, SAXElement element) { if (prefix == null || namespaceURI == null) { // No namespace. Ignore... return; } else if(prefix.equals(XMLConstants.DEFAULT_NS_PREFIX) && namespaceURI.equals(XMLConstants.NULL_NS_URI)) { // No namespace. Ignore... return; } else { String prefixNS = element.getAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, prefix); if(prefixNS != null && prefixNS.length() != 0) { // Already declared (on the element)... return; } } Attributes attributes = element.getAttributes(); AttributesImpl attributesCopy = new AttributesImpl(); attributesCopy.setAttributes(attributes); if(prefix.length() > 0) { attributesCopy.addAttribute(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, prefix, "xmlns:" + prefix, null, namespaceURI); } else { attributesCopy.addAttribute(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, prefix, "xmlns", null, namespaceURI); } element.setAttributes(attributesCopy); } }
public boolean isTargetedAtElement(SAXElement element) { QName qname = element.getName(); if(isStar || isStarStar) { return true; } if(!qname.getLocalPart().equalsIgnoreCase(targetElement.getLocalPart())) { return false; } return isTargetedAtNamespace(qname.getNamespaceURI()); }
private void bindSaxDataValue(SAXElement element, ExecutionContext executionContext) { String dataString; if (isAttribute) { dataString = SAXUtil.getAttribute(valueAttributeName, element.getAttributes()); } else { dataString = element.getTextContent(); } if(expression != null) { bindExpressionValue(executionContext); } else { populateAndSetPropertyValue(dataString, executionContext); } }
public Object getValue(SAXElement element) { return decoder.decode(element.getTextContent()); }
public void visitAfter(final SAXElement element, final ExecutionContext executionContext) throws SmooksException { if(targetAttribute != null) { OnFailResultImpl result = _validate(element.getAttribute(targetAttribute), executionContext); if(result != null) { result.setFailFragmentPath(SAXUtil.getXPath(element) + "/@" + targetAttribute); assertValidationException(result, executionContext); } } else { OnFailResultImpl result = _validate(element.getTextContent(), executionContext); if(result != null) { result.setFailFragmentPath(SAXUtil.getXPath(element)); assertValidationException(result, executionContext); } } }
private void setWriter(Writer writer) { super.setWriter(writer, null); }
public void onChildText(SAXElement element, SAXText text, ExecutionContext executionContext) throws SmooksException, IOException { writeStartElement(element); if(element.isWriterOwner(writerOwner)) { text.toWriter(element.getWriter(writerOwner), rewriteEntities); } }
/** * Get the named attribute from this element. * @param namespaceURI The namespace URI of the required attribute. * @param attribute The attribute name. * @return The attribute value, or an empty string if the attribute is not specified. * @deprecated Use {@link #getAttributeNS(String, String)}. */ public String getAttribute(String namespaceURI, String attribute) { return getAttributeNS(namespaceURI, attribute); }
/** * Add a text object to this instance. * <p/> * Utility method, userd mainly for testing. * * @param text The text to be added. * @see #accumulateText() * @see TextConsumer */ public void addText (String text) { addText(text, TextType.TEXT); }