001 package com.thaiopensource.relaxng.impl;
002
003 class GroupPattern extends BinaryPattern {
004 GroupPattern(Pattern p1, Pattern p2) {
005 super(p1.isNullable() && p2.isNullable(),
006 combineHashCode(GROUP_HASH_CODE, p1.hashCode(), p2.hashCode()),
007 p1,
008 p2);
009 }
010
011 Pattern expand(SchemaPatternBuilder b) {
012 Pattern ep1 = p1.expand(b);
013 Pattern ep2 = p2.expand(b);
014 if (ep1 != p1 || ep2 != p2)
015 return b.makeGroup(ep1, ep2);
016 else
017 return this;
018 }
019
020 void checkRestrictions(int context, DuplicateAttributeDetector dad, Alphabet alpha) throws RestrictionViolationException {
021 switch (context) {
022 case START_CONTEXT:
023 throw new RestrictionViolationException("start_contains_group");
024 case DATA_EXCEPT_CONTEXT:
025 throw new RestrictionViolationException("data_except_contains_group");
026 }
027 super.checkRestrictions(context == ELEMENT_REPEAT_CONTEXT
028 ? ELEMENT_REPEAT_GROUP_CONTEXT
029 : context,
030 dad,
031 alpha);
032 if (context != LIST_CONTEXT
033 && !contentTypeGroupable(p1.getContentType(), p2.getContentType()))
034 throw new RestrictionViolationException("group_string");
035 }
036
037 void accept(PatternVisitor visitor) {
038 visitor.visitGroup(p1, p2);
039 }
040 Object apply(PatternFunction f) {
041 return f.caseGroup(this);
042 }
043 }