001: package org.kohsuke.rngom.binary;
002:
003: import org.kohsuke.rngom.binary.visitor.PatternFunction;
004: import org.kohsuke.rngom.binary.visitor.PatternVisitor;
005: import org.xml.sax.Locator;
006: import org.xml.sax.SAXException;
007: import org.xml.sax.SAXParseException;
008:
009: public class RefPattern extends Pattern {
010: private Pattern p;
011: private Locator refLoc;
012: private String name;
013: private int checkRecursionDepth = -1;
014: private boolean combineImplicit = false;
015: private byte combineType = COMBINE_NONE;
016: private byte replacementStatus = REPLACEMENT_KEEP;
017: private boolean expanded = false;
018:
019: static final byte REPLACEMENT_KEEP = 0;
020: static final byte REPLACEMENT_REQUIRE = 1;
021: static final byte REPLACEMENT_IGNORE = 2;
022:
023: static final byte COMBINE_NONE = 0;
024: static final byte COMBINE_CHOICE = 1;
025: static final byte COMBINE_INTERLEAVE = 2;
026:
027: RefPattern(String name) {
028: this .name = name;
029: }
030:
031: Pattern getPattern() {
032: return p;
033: }
034:
035: void setPattern(Pattern p) {
036: this .p = p;
037: }
038:
039: Locator getRefLocator() {
040: return refLoc;
041: }
042:
043: void setRefLocator(Locator loc) {
044: this .refLoc = loc;
045: }
046:
047: void checkRecursion(int depth) throws SAXException {
048: if (checkRecursionDepth == -1) {
049: checkRecursionDepth = depth;
050: p.checkRecursion(depth);
051: checkRecursionDepth = -2;
052: } else if (depth == checkRecursionDepth)
053: // XXX try to recover from this?
054: throw new SAXParseException(SchemaBuilderImpl.localizer
055: .message("recursive_reference", name), refLoc);
056: }
057:
058: Pattern expand(SchemaPatternBuilder b) {
059: if (!expanded) {
060: p = p.expand(b);
061: expanded = true;
062: }
063: return p;
064: }
065:
066: boolean samePattern(Pattern other) {
067: return false;
068: }
069:
070: public void accept(PatternVisitor visitor) {
071: p.accept(visitor);
072: }
073:
074: public Object apply(PatternFunction f) {
075: return f.caseRef(this );
076: }
077:
078: byte getReplacementStatus() {
079: return replacementStatus;
080: }
081:
082: void setReplacementStatus(byte replacementStatus) {
083: this .replacementStatus = replacementStatus;
084: }
085:
086: boolean isCombineImplicit() {
087: return combineImplicit;
088: }
089:
090: void setCombineImplicit() {
091: combineImplicit = true;
092: }
093:
094: byte getCombineType() {
095: return combineType;
096: }
097:
098: void setCombineType(byte combineType) {
099: this .combineType = combineType;
100: }
101:
102: String getName() {
103: return name;
104: }
105: }
|