Example usage for org.xml.sax ContentHandler startElement

List of usage examples for org.xml.sax ContentHandler startElement

Introduction

In this page you can find the example usage for org.xml.sax ContentHandler startElement.

Prototype

public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException;

Source Link

Document

Receive notification of the beginning of an element.

Usage

From source file:org.orbeon.oxf.xforms.processor.handlers.xhtml.XFormsBaseHandlerXHTML.java

protected void handleLabelHintHelpAlert(LHHAAnalysis lhhaAnalysis, String targetControlEffectiveId,
        String forEffectiveId, XFormsBaseHandler.LHHAC lhhaType, XFormsControl control, boolean isTemplate,
        boolean isExternal) throws SAXException {

    final AttributesImpl staticLHHAAttributes = Dom4jUtils.getSAXAttributes(lhhaAnalysis.element());

    final boolean isLabel = lhhaType == LHHAC.LABEL;
    final boolean isHelp = lhhaType == LHHAC.HELP;
    final boolean isHint = lhhaType == LHHAC.HINT;
    final boolean isAlert = lhhaType == LHHAC.ALERT;

    if (staticLHHAAttributes != null || isAlert) {
        // If no attributes were found, there is no such label / help / hint / alert

        if (handlerContext.isNoScript() && isHelp) {
            if (control != null) {

                final ContentHandler contentHandler = handlerContext.getController().getOutput();
                final String xhtmlPrefix = handlerContext.findXHTMLPrefix();

                // <a href="#my-control-id-help">

                final AttributesImpl aAttributes = new AttributesImpl();
                aAttributes.addAttribute("", "href", "href", XMLReceiverHelper.CDATA,
                        "#" + getLHHACId(containingDocument, targetControlEffectiveId,
                                LHHAC_CODES.get(LHHAC.HELP)));
                aAttributes.addAttribute("", "class", "class", XMLReceiverHelper.CDATA, "xforms-help-anchor");

                final String aQName = XMLUtils.buildQName(xhtmlPrefix, "a");
                contentHandler.startElement(XMLConstants.XHTML_NAMESPACE_URI, "a", aQName, aAttributes);
                contentHandler.endElement(XMLConstants.XHTML_NAMESPACE_URI, "a", aQName);
            }/*  www .  ja v a 2s. c  o  m*/
        } else {

            final String labelHintHelpAlertValue;
            final boolean mustOutputHTMLFragment;
            if (control != null) {
                // Get actual value from control
                if (isLabel) {
                    labelHintHelpAlertValue = control.getLabel();
                    mustOutputHTMLFragment = control.isHTMLLabel();
                } else if (isHelp) {
                    // NOTE: Special case here where we get the escaped help to facilitate work below. Help is a special
                    // case because it is stored as escaped HTML within a <label> element.
                    labelHintHelpAlertValue = control.getEscapedHelp();
                    mustOutputHTMLFragment = false;
                } else if (isHint) {
                    labelHintHelpAlertValue = control.getHint();
                    mustOutputHTMLFragment = control.isHTMLHint();
                } else if (isAlert) {
                    labelHintHelpAlertValue = control.getAlert();
                    mustOutputHTMLFragment = control.isHTMLAlert();
                } else {
                    throw new IllegalStateException("Illegal type requested");
                }
            } else {
                // Placeholder
                labelHintHelpAlertValue = null;
                mustOutputHTMLFragment = false;
            }

            final String elementName;
            {
                if (isLabel) {
                    elementName = handlerContext.getLabelElementName();
                } else if (isHelp) {
                    elementName = handlerContext.getHelpElementName();
                } else if (isHint) {
                    elementName = handlerContext.getHintElementName();
                } else if (isAlert) {
                    elementName = handlerContext.getAlertElementName();
                } else {
                    throw new IllegalStateException("Illegal type requested");
                }
            }

            final StringBuilder classes = new StringBuilder(30);

            // Put user classes first if any
            if (staticLHHAAttributes != null) {
                final String userClass = staticLHHAAttributes.getValue("class");
                if (userClass != null)
                    classes.append(userClass);
            }

            // Mark alert as active if needed
            if (isAlert) {
                if (control instanceof XFormsSingleNodeControl) {
                    final XFormsSingleNodeControl singleNodeControl = (XFormsSingleNodeControl) control;
                    final scala.Option<ValidationLevels.ValidationLevel> constraintLevel = singleNodeControl
                            .alertLevel();

                    if (constraintLevel.isDefined()) {
                        if (classes.length() > 0)
                            classes.append(' ');
                        classes.append("xforms-active");
                    }

                    // Constraint classes are placed on the control if the alert is not external
                    if (isExternal)
                        addConstraintClasses(classes, constraintLevel);
                }
            }

            // Handle visibility
            // TODO: It would be great to actually know about the relevance of help, hint, and label. Right now, we just look at whether the value is empty
            if (control != null) {
                if (isAlert || isLabel) {
                    // Allow empty labels and alerts
                    if (!control.isRelevant()) {
                        if (classes.length() > 0)
                            classes.append(' ');
                        classes.append("xforms-disabled");
                    }
                } else {
                    // For help and hint, consider "non-relevant" if empty
                    final boolean isHintHelpRelevant = control.isRelevant()
                            && StringUtils.isNotEmpty(labelHintHelpAlertValue);
                    if (!isHintHelpRelevant) {
                        if (classes.length() > 0)
                            classes.append(' ');
                        classes.append("xforms-disabled");
                    }
                }
            } else if (!isTemplate || isHelp) {
                // Null control outside of template OR help within template
                if (classes.length() > 0)
                    classes.append(' ');
                classes.append("xforms-disabled");
            }

            // LHHA name
            if (classes.length() > 0)
                classes.append(' ');
            classes.append("xforms-");
            classes.append(lhhaType.name().toLowerCase());

            // We handle null attributes as well because we want a placeholder for "alert" even if there is no xf:alert
            final Attributes newAttributes = (staticLHHAAttributes != null) ? staticLHHAAttributes
                    : new AttributesImpl();

            lhhaAnalysis.encodeAndAppendAppearances(classes);

            outputLabelFor(handlerContext, getIdClassXHTMLAttributes(newAttributes, classes.toString(), null),
                    targetControlEffectiveId, forEffectiveId, lhhaType, elementName, labelHintHelpAlertValue,
                    mustOutputHTMLFragment, isExternal);
        }
    }
}

From source file:org.orbeon.oxf.xforms.processor.handlers.xhtml.XFormsBaseHandlerXHTML.java

protected static void outputLabelForStart(HandlerContext handlerContext, Attributes attributes,
        String targetControlEffectiveId, String forEffectiveId, LHHAC lhha, String elementName, boolean addIds)
        throws SAXException {

    assert lhha != null;
    assert !addIds || targetControlEffectiveId != null;

    // Replace id attribute to be foo-label, foo-hint, foo-help, or foo-alert
    final AttributesImpl newAttribute;
    if (addIds && targetControlEffectiveId != null) {
        // Add or replace existing id attribute
        // NOTE: addIds == true for external LHHA
        newAttribute = SAXUtils.addOrReplaceAttribute(attributes, "", "", "id", getLHHACId(
                handlerContext.getContainingDocument(), targetControlEffectiveId, LHHAC_CODES.get(lhha)));
    } else {//w  w w.  j  av  a 2  s .  c  om
        // Remove existing id attribute if any
        newAttribute = SAXUtils.removeAttribute(attributes, "", "id");
    }

    // Add @for attribute if specified and element is a label
    if (forEffectiveId != null && elementName.equals("label"))
        newAttribute.addAttribute("", "for", "for", XMLReceiverHelper.CDATA, forEffectiveId);

    final String xhtmlPrefix = handlerContext.findXHTMLPrefix();
    final String labelQName = XMLUtils.buildQName(xhtmlPrefix, elementName);
    final ContentHandler contentHandler = handlerContext.getController().getOutput();

    contentHandler.startElement(XMLConstants.XHTML_NAMESPACE_URI, elementName, labelQName, newAttribute);
}

From source file:org.orbeon.oxf.xforms.processor.handlers.xhtml.XFormsControlLifecyleHandler.java

@Override
public final void start(String uri, String localname, String qName, Attributes attributes) throws SAXException {
    if (isMustOutputControl(currentControlOrNull())) {

        final ContentHandler contentHandler = handlerContext.getController().getOutput();
        if (isMustOutputContainerElement()) {
            // Open control element, usually <span>
            final AttributesImpl containerAttributes = getContainerAttributes(uri, localname, attributes);
            contentHandler.startElement(XMLConstants.XHTML_NAMESPACE_URI, getContainingElementName(),
                    getContainingElementQName(), containerAttributes);
        }//from  w w  w . j a v a 2s . co  m

        // Get local order for control
        final String localOrder = attributes.getValue(XFormsConstants.XXFORMS_ORDER_QNAME.getNamespaceURI(),
                XFormsConstants.XXFORMS_ORDER_QNAME.getName());

        // Use local or default config
        final String[] config = (localOrder != null) ? StringUtils.split(localOrder)
                : handlerContext.getDocumentOrder();

        // 2012-12-17: Removed nested <a name="effective-id"> because the enclosing <span> for the control has the
        // same id and will be handled first by the browser as per HTML 5. This means the named anchor is actually
        // redundant.

        // Process everything up to and including the control
        for (int i = 0; i < config.length; i++) {
            final String current = config[i];

            if ("control".equals(current)) {
                // Handle control
                handleControlStart(uri, localname, qName, attributes, getEffectiveId(), currentControlOrNull());
                // Do the rest in end() below if needed
                if (i < config.length - 1) {
                    // There remains stuff to process
                    final int endConfigLength = config.length - i;
                    endConfig = new String[endConfigLength];
                    System.arraycopy(config, i, endConfig, 0, endConfigLength);
                    this.attributes = new AttributesImpl(attributes);
                }
                break;
            } else if ("label".equals(current)) {
                // xf:label
                if (hasLocalLabel())
                    handleLabel();
            } else if ("alert".equals(current)) {
                // xf:alert
                if (hasLocalAlert())
                    handleAlert();
            } else if ("hint".equals(current)) {
                // xf:hint
                if (hasLocalHint())
                    handleHint();
            } else {
                // xf:help
                if (hasLocalHelp())
                    handleHelp();
            }
        }
    }
}

From source file:org.orbeon.oxf.xforms.processor.handlers.xhtml.XFormsGroupFieldsetHandler.java

@Override
public void handleControlStart(String uri, String localname, String qName, Attributes attributes,
        final String effectiveId, XFormsControl control) throws SAXException {

    final XFormsGroupControl groupControl = (XFormsGroupControl) control;
    final String xhtmlPrefix = handlerContext.findXHTMLPrefix();
    final ElementHandlerController controller = handlerContext.getController();
    final ContentHandler contentHandler = controller.getOutput();

    // Output an xhtml:legend element if and only if there is an xf:label element. This help with
    // styling in particular.
    final boolean hasLabel = LHHASupport.hasLabel(containingDocument, getPrefixedId());
    if (hasLabel) {

        // Handle label classes
        reusableAttributes.clear();/*from  ww  w  .  j  a v a2  s. co m*/
        reusableAttributes.addAttribute("", "class", "class", XMLReceiverHelper.CDATA,
                getLabelClasses(groupControl));
        reusableAttributes.addAttribute("", "id", "id", XMLReceiverHelper.CDATA,
                getLHHACId(containingDocument, effectiveId, LHHAC_CODES.get(LHHAC.LABEL)));

        // Output xhtml:legend with label content
        final String legendQName = XMLUtils.buildQName(xhtmlPrefix, "legend");
        contentHandler.startElement(XMLConstants.XHTML_NAMESPACE_URI, "legend", legendQName,
                reusableAttributes);
        {
            final String labelValue = getLabelValue(groupControl);
            if (StringUtils.isNotEmpty(labelValue))
                contentHandler.characters(labelValue.toCharArray(), 0, labelValue.length());
        }
        contentHandler.endElement(XMLConstants.XHTML_NAMESPACE_URI, "legend", legendQName);
    }
}

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

/**
 * @since tizzit-common 15.10.2009//  w  w w  .ja v  a  2s.  co  m
 */
public static void addElement(ContentHandler handler, String namespaceUri, String elementName,
        String elementValue, Attributes attr) throws SAXException {
    if (elementName != null) {
        handler.startElement(namespaceUri, elementName, elementName, attr);
        String saveVal = (elementValue == null) ? "" : elementValue;
        char[] ca = saveVal.toCharArray();
        handler.characters(ca, 0, ca.length);
        handler.endElement(namespaceUri, elementName, elementName);
    }
}

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 ww  . jav a  2s .  c  o 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);
    }/*  w ww.  j ava 2s .co 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.examples.tutorial.TraceUtil.java

public static void startDiv(ContentHandler handler, String xhtmlPrefix, Attributes attributes)
        throws SAXException {
    handler.startElement(XHTML_NAMESPACE_URI, DIV_ELEMENT, createQName(xhtmlPrefix, DIV_ELEMENT), attributes);
}

From source file:org.xchain.examples.tutorial.TraceUtil.java

public static void startSpan(ContentHandler handler, String xhtmlPrefix, Attributes attributes)
        throws SAXException {
    handler.startElement(XHTML_NAMESPACE_URI, SPAN_ELEMENT, createQName(xhtmlPrefix, SPAN_ELEMENT), attributes);
}

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());
    }// w  ww .j  a  va  2 s  .c o  m
    if (executeChars != null) {
        char[] characters = executeChars.toCharArray();
        handler.characters(characters, 0, characters.length);
    }

    result = super.execute(context);

    return result;
}