Example usage for org.xml.sax.helpers AttributesImpl AttributesImpl

List of usage examples for org.xml.sax.helpers AttributesImpl AttributesImpl

Introduction

In this page you can find the example usage for org.xml.sax.helpers AttributesImpl AttributesImpl.

Prototype

public AttributesImpl() 

Source Link

Document

Construct a new, empty AttributesImpl object.

Usage

From source file:org.tizzit.util.xml.SAXHelper.java

/**
 * @since tizzit-common 15.10.2009/*w ww. ja  v  a2 s  .  co  m*/
 */
public static void addElement(ContentHandler handler, String namespaceUri, String elementName,
        Integer elementValue) throws SAXException {
    String saveVal = (elementValue == null) ? "" : elementValue.toString();
    addElement(handler, namespaceUri, elementName, saveVal, new AttributesImpl());
}

From source file:org.tizzit.util.xml.SAXHelper.java

/**
 * @since tizzit-common 15.10.2009/*from www  . java  2s .  com*/
 */
public static void addElement(ContentHandler handler, String namespaceUri, String elementName,
        Boolean elementValue) throws SAXException {
    String saveVal = (elementValue == null) ? "" : elementValue.toString();
    addElement(handler, namespaceUri, elementName, saveVal, new AttributesImpl());
}

From source file:org.xchain.example.namespaces.xhtml.OptionCommand.java

public boolean execute(JXPathContext context) throws Exception {
    String value = getValue(context);
    String currentValue = (String) ((ScopedQNameVariables) context.getVariables())
            .getVariable(SelectCommand.SELECTED_VARIABLE_NAME, Scope.execution);

    // get the xhtml prefix and make sure that there is a mapping for it.
    String xhtmlPrefix = context.getPrefix(XHTML_NAMESPACE);
    if (xhtmlPrefix == null) {
        throw new IllegalStateException("There is no mapping defined for " + XHTML_NAMESPACE);
    }/*  w  w w  .  j  a  v  a  2s  . co m*/

    AttributesImpl attributes = new AttributesImpl();
    if (value != null) {
        attributes.addAttribute("", VALUE_LOCAL_NAME, VALUE_LOCAL_NAME, "CDATA", value);
    }
    if (value != null && value.equals(currentValue)) {
        attributes.addAttribute("", SELECTED_LOCAL_NAME, SELECTED_LOCAL_NAME, "CDATA", SELECTED_VALUE);
    }

    ContentHandler handler = getContentHandler();
    handler.startElement(XHTML_NAMESPACE, OPTION_LOCAL_NAME, qNameString(xhtmlPrefix, OPTION_LOCAL_NAME),
            attributes);

    boolean result = false;
    Exception exception = null;

    try {
        result = super.execute(context);
    } catch (Exception e) {
        exception = e;
    }
    if (exception == null || !(exception instanceof SAXException)) {
        handler.endElement(XHTML_NAMESPACE, OPTION_LOCAL_NAME, qNameString(xhtmlPrefix, OPTION_LOCAL_NAME));
    }

    if (exception != null) {
        throw exception;
    }
    return result;
}

From source file:org.xchain.example.namespaces.xhtml.SelectCommand.java

public boolean execute(JXPathContext context) throws Exception {
    // get the request and the name from the context.
    HttpServletRequest request = getRequest(context);
    String name = getName(context);

    // get the original value of the name.
    String currentValue = request.getParameter(name);

    // get the xhtml prefix and make sure that there is a mapping for it.
    String xhtmlPrefix = context.getPrefix(XHTML_NAMESPACE);
    if (xhtmlPrefix == null) {
        throw new IllegalStateException("There is no mapping defined for " + XHTML_NAMESPACE);
    }/*from   w  w  w  .  j ava2 s. c  o m*/

    // create the attributes for the element we are going to output.
    AttributesImpl attributes = new AttributesImpl();
    if (hasId()) {
        attributes.addAttribute("", ID_LOCAL_NAME, ID_LOCAL_NAME, "CDATA", getId(context));
    }
    attributes.addAttribute("", NAME_LOCAL_NAME, NAME_LOCAL_NAME, "CDATA", getName(context));

    ((ScopedQNameVariables) context.getVariables()).declareVariable(SELECTED_VARIABLE_NAME, currentValue,
            Scope.execution);
    ContentHandler handler = getContentHandler();
    handler.startElement(XHTML_NAMESPACE, SELECT_LOCAL_NAME, qNameString(xhtmlPrefix, SELECT_LOCAL_NAME),
            attributes);

    boolean result = false;
    Exception exception = null;

    // execute the children and catch any exceptions.
    try {
        result = super.execute(context);
    } catch (Exception e) {
        exception = e;
    }

    // if there was not an exception, or the exception is not a sax exception, then we need to finish the output.
    if (exception == null || !(exception instanceof SAXException)) {
        handler.endElement(XHTML_NAMESPACE, SELECT_LOCAL_NAME, qNameString(xhtmlPrefix, SELECT_LOCAL_NAME));
    }

    // rethrow the exception.
    if (exception != null) {
        throw exception;
    }
    return result;
}

From source file:org.xchain.framework.jsl.SaxTemplateHandler.java

/**
 * Handles start element events for all JSL elements.
 *///from www. ja  v  a  2  s  .c o m
protected void startJslElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
    // scan for jsl elements that come out of order.
    if (TEMPLATE_LOCAL_NAME.equals(localName)) {
        templateElementDepth++;
    } else if (templateElementDepth == 0) {
        throw new SAXException("The element {" + uri + "}" + localName + "was found outside of a {"
                + JSL_NAMESPACE + "}template element.");
    }

    boolean startSource = elementInfoStack.size() == 1
            || elementInfoStack.get(1).getElementType() == ElementType.COMMAND_ELEMENT_TYPE;
    // if the parent element is null or a command element, then we need to start a new template source.
    if (startSource) {
        sourceBuilder.startSource(elementInfoStack.getFirst().getSourcePrefixMapping(),
                elementInfoStack.getFirst().getSourceExcludeResultPrefixSet(),
                TEMPLATE_LOCAL_NAME.equals(localName));
    }

    if (TEMPLATE_LOCAL_NAME.equals(localName)) {
        startJslTemplateElement(uri, localName, qName, attributes);
    } else if (VALUE_OF_LOCAL_NAME.equals(localName)) {
        startJslValueOfElement(uri, localName, qName, attributes);
    } else if (TEXT_LOCAL_NAME.equals(localName)) {
        startJslTextElement(uri, localName, qName, attributes);
    } else if (COMMENT_LOCAL_NAME.equals(localName)) {
        startJslCommentElement(uri, localName, qName, attributes);
    } else if (ELEMENT_LOCAL_NAME.equals(localName)) {
        startJslDynamicElement(uri, localName, qName, attributes);
    } else if (ATTRIBUTE_LOCAL_NAME.equals(localName)) {
        startJslDynamicAttribute(uri, localName, qName, attributes);
    } else {
        throw new SAXException("Unknown template element '{" + uri + "}" + localName + "'.");
    }

    if (startSource) {
        // send start prefix mapping events to the digester.
        for (Map.Entry<String, String> mapping : elementInfoStack.getFirst().getHandlerPrefixMapping()
                .entrySet()) {
            getContentHandler().startPrefixMapping(mapping.getKey(), mapping.getValue());
        }

        // create a new attributes object with any attributes that are in an xchain namespace.
        AttributesImpl digesterAttributes = new AttributesImpl();
        for (int i = 0; i < attributes.getLength(); i++) {
            if (commandNamespaceSet.contains(attributes.getURI(i))) {
                digesterAttributes.addAttribute(attributes.getURI(i), attributes.getLocalName(i),
                        attributes.getQName(i), attributes.getType(i), attributes.getValue(i));
            }
        }

        // send the start template element to the digester.
        getContentHandler().startElement(JSL_NAMESPACE, TEMPORARY_CHAIN_LOCAL_NAME, temporaryChainQName(),
                digesterAttributes);
    }
}

From source file:org.xchain.framework.jsl.SaxTemplateHandler.java

/**
 * Handles start element events for template elements.
 *//*  w w w  . j  a v  a2 s.  c o  m*/
protected void startTemplateElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
    if (templateElementDepth == 0) {
        throw new SAXException(
                "The element {" + uri + "}" + localName + " was found outside of a template element.");
    }

    // push characters target.
    charactersTargetStack.addFirst(CharacterTarget.SOURCE_BUILDER);

    // push ignorable whitespace target.
    ignorableWhitespaceTargetStack.addFirst(CharacterTarget.SOURCE_BUILDER);

    boolean startSource = elementInfoStack.size() == 1
            || elementInfoStack.get(1).getElementType() == ElementType.COMMAND_ELEMENT_TYPE;
    // if the parent element is null or a command element, then we need to start a new template source.
    if (startSource) {
        sourceBuilder.startSource(elementInfoStack.getFirst().getSourcePrefixMapping(),
                elementInfoStack.getFirst().getSourceExcludeResultPrefixSet(), false);
    }

    try {
        // get the element info for the head of the stack.
        ElementInfo elementInfo = elementInfoStack.getFirst();

        // let the source builder know that we are starting a start element.
        sourceBuilder.startStartElement();

        // send the prefix mappings to the source builder.
        for (Map.Entry<String, String> prefixMapping : elementInfo.getPrefixMapping().entrySet()) {
            sourceBuilder.appendStartPrefixMapping(prefixMapping.getKey(), prefixMapping.getValue());
        }

        // send any exclude result prefix events that need to go out.
        for (String excludeResultPrefix : elementInfo.getExcludeResultPrefixSet()) {
            sourceBuilder.appendStartExcludeResultPrefix(excludeResultPrefix);
        }

        // send the attributes to the source builder.
        for (int i = 0; i < attributes.getLength(); i++) {
            sourceBuilder.appendAttributeValueTemplate(attributes.getURI(i), attributes.getLocalName(i),
                    attributes.getQName(i), attributes.getValue(i));
        }

        // append the start element.
        sourceBuilder.appendStartElement(uri, localName, qName);

        // let the source builder know that we are ending a start element.
        sourceBuilder.endStartElement();
    } catch (SAXException e) {
        throw e;
    } catch (Exception e) {
        throw new SAXException(e);
    }

    // we need to do this just before we stop templating, so this kind of code should be in start command, or end jsl template and end template when there was not
    // a nested command.  We need to delay this, since the passed through template must provide the namespaces state from all of the consumed elements.
    if (startSource) {
        // send start prefix mapping events to the digester.
        for (Map.Entry<String, String> mapping : elementInfoStack.getFirst().getHandlerPrefixMapping()
                .entrySet()) {
            getContentHandler().startPrefixMapping(mapping.getKey(), mapping.getValue());
        }

        // create a new attributes object with any attributes that are in an xchain namespace.
        AttributesImpl digesterAttributes = new AttributesImpl();
        for (int i = 0; i < attributes.getLength(); i++) {
            if (commandNamespaceSet.contains(attributes.getURI(i))) {
                digesterAttributes.addAttribute(attributes.getURI(i), attributes.getLocalName(i),
                        attributes.getQName(i), attributes.getType(i), attributes.getValue(i));
            }
        }

        // send the start template element to the digester.
        getContentHandler().startElement(JSL_NAMESPACE, TEMPORARY_CHAIN_LOCAL_NAME, temporaryChainQName(),
                digesterAttributes);
    }
}

From source file:org.xchain.framework.jsl.SaxTemplateHandler.java

/**
 * Handles characters found in the source document.
 *//*  w  ww. j a  va 2 s . c  o  m*/
public void flushCharacters() throws SAXException {
    if (!charactersTargetStack.isEmpty()) {
        // switch on the characters target.
        switch (charactersTargetStack.getFirst()) {
        case NONE:
            break;
        case SOURCE_BUILDER:
            if (!whitespacePattern.matcher(charactersBuilder).matches()) {
                // if we are in an xchain command, then we need to start a new source.
                boolean startSource = elementInfoStack.size() == 0
                        || elementInfoStack.get(0).getElementType() == ElementType.COMMAND_ELEMENT_TYPE;

                if (startSource) {
                    // don't send any mappings for the element.
                    sourceBuilder.startSource(new HashMap<String, String>(), new HashSet<String>(), false);
                    getContentHandler().startElement(JSL_NAMESPACE, TEMPORARY_CHAIN_LOCAL_NAME,
                            temporaryChainQName(), new AttributesImpl());
                }

                sourceBuilder.appendCharacters(charactersBuilder.toString());

                if (startSource) {
                    handleSource(sourceBuilder.endSource());
                }
            }
            break;
        case DIGESTER:
            char[] characters = charactersBuilder.toString().toCharArray();
            getContentHandler().characters(characters, 0, characters.length);
            break;
        }
        charactersBuilder = new StringBuilder();
    }
}

From source file:org.xchain.namespaces.test.FilterElement.java

public boolean execute(JXPathContext context) throws Exception {
    boolean result = false;

    QName name = getName(context);
    String executeChars = getExecuteChars(context);

    ContentHandler handler = getContentHandler();

    if (name != null) {
        handler.startElement(name.getNamespaceURI(), name.getLocalPart(), name.getPrefix(),
                new AttributesImpl());
    }/*ww  w . j  a  va2 s . c  om*/
    if (executeChars != null) {
        char[] characters = executeChars.toCharArray();
        handler.characters(characters, 0, characters.length);
    }

    result = super.execute(context);

    return result;
}

From source file:org.xwiki.portlet.view.HTMLIdAttributeXMLFilter.java

@Override
public void startDocument() throws SAXException {
    super.startDocument();

    if (wrapOutput) {
        // Start the portlet output container.
        AttributesImpl attributes = new AttributesImpl();
        attributes.addAttribute(null, ID, ID, ID.toUpperCase(), namespace);
        super.startElement(null, DIV, DIV, attributes);
    }//from ww w  . j  ava  2  s.  c o m
}

From source file:org.xwiki.portlet.view.HTMLMetaDataXMLFilter.java

/**
 * Outputs an {@code <input type="hidden">} HTML element that holds the given meta data.
 * /*from ww w .  ja  v  a2  s  .co  m*/
 * @param key the meta data key
 * @param value the meta data value
 * @param withId {@code true} to output the 'id' attribute, {@code false} otherwise
 * @param withName {@code true} to output the 'name' attribute, {@code false} otherwise
 * @throws SAXException if writing the meta data fails
 */
private void outputMetaData(String key, String value, boolean withId, boolean withName) throws SAXException {
    AttributesImpl attributes = new AttributesImpl();
    if (withId) {
        attributes.addAttribute(null, ID, ID, "ID", key);
    }
    if (withName) {
        attributes.addAttribute(null, NAME, NAME, CDATA, key);
    }
    attributes.addAttribute(null, TYPE, TYPE, CDATA, "hidden");
    attributes.addAttribute(null, VALUE, VALUE, CDATA, StringUtils.defaultString(value));
    super.startElement(null, INPUT, INPUT, attributes);
    super.endElement(null, INPUT, INPUT);
}