001: /*
002: Copyright (c) 2005, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.binding.model;
030:
031: /**
032: * Visitor for child tree of structure with an element definition. This
033: * verifies that text and CDATA components are only used in ways consistent
034: * with parsing (i.e., each use must be preceeded by a required element).
035: */
036: class SequenceVisitor extends ModelVisitor {
037: private final StructureElementBase m_baseStructure;
038: private final ValidationContext m_validationContext;
039: private boolean m_isTextAllowed;
040:
041: /**
042: * Constructor.
043: *
044: * @param base root of subtree being visited (<code>null</code> if not
045: * a structure)
046: * @param vctx validation context used for reporting errors
047: */
048: public SequenceVisitor(StructureElementBase base,
049: ValidationContext vctx) {
050: m_baseStructure = base;
051: m_validationContext = vctx;
052: m_isTextAllowed = true;
053: }
054:
055: /* (non-Javadoc)
056: * @see org.jibx.binding.model.ModelVisitor#visit(org.jibx.binding.model.StructureElementBase)
057: */
058: public boolean visit(StructureElementBase node) {
059: if (node != m_baseStructure && node.hasName()) {
060: m_isTextAllowed = !node.isOptional();
061: return false;
062: } else {
063: return true;
064: }
065: }
066:
067: /* (non-Javadoc)
068: * @see org.jibx.binding.model.ModelVisitor#visit(org.jibx.binding.model.ValueElement)
069: */
070: public boolean visit(ValueElement node) {
071: switch (node.getStyle()) {
072: case ValueElement.CDATA_STYLE:
073: case ValueElement.TEXT_STYLE:
074: if (m_isTextAllowed) {
075: m_isTextAllowed = false;
076: } else {
077: m_validationContext
078: .addError(
079: "Text value must be preceded by required element",
080: node);
081: }
082: break;
083:
084: case NestingAttributes.ELEMENT_STYLE:
085: m_isTextAllowed = !node.isOptional();
086: break;
087:
088: case NestingAttributes.ATTRIBUTE_STYLE:
089: break;
090: }
091: return false;
092: }
093:
094: /* (non-Javadoc)
095: * @see org.jibx.binding.model.ModelVisitor#exit(org.jibx.binding.model.StructureElementBase)
096: */
097: public void exit(StructureElementBase node) {
098: if (node.hasName()) {
099: m_isTextAllowed = !node.isOptional();
100: }
101: }
102: }
|