1 package com.thaiopensource.relaxng.impl; 2 3 import com.thaiopensource.xml.util.Name; 4 5 import java.util.Hashtable ; 6 7 class FindElementFunction extends AbstractPatternFunction { 8 private final ValidatorPatternBuilder builder; 9 private final Name name; 10 private final Hashtable processed = new Hashtable (); 11 private int specificity = NameClass.SPECIFICITY_NONE; 12 private Pattern pattern = null; 13 14 static public Pattern findElement(ValidatorPatternBuilder builder, Name name, Pattern start) { 15 FindElementFunction f = new FindElementFunction(builder, name); 16 start.apply(f); 17 if (f.pattern == null) 18 return builder.makeNotAllowed(); 19 return f.pattern; 20 } 21 22 private FindElementFunction(ValidatorPatternBuilder builder, Name name) { 23 this.builder = builder; 24 this.name = name; 25 } 26 27 private boolean haveProcessed(Pattern p) { 28 if (processed.get(p) != null) 29 return true; 30 processed.put(p, p); 31 return false; 32 } 33 34 private Object caseBinary(BinaryPattern p) { 35 if (!haveProcessed(p)) { 36 p.getOperand1().apply(this); 37 p.getOperand2().apply(this); 38 } 39 return null; 40 41 } 42 43 public Object caseGroup(GroupPattern p) { 44 return caseBinary(p); 45 } 46 47 public Object caseInterleave(InterleavePattern p) { 48 return caseBinary(p); 49 } 50 51 public Object caseChoice(ChoicePattern p) { 52 return caseBinary(p); 53 } 54 55 public Object caseOneOrMore(OneOrMorePattern p) { 56 if (!haveProcessed(p)) 57 p.getOperand().apply(this); 58 return null; 59 } 60 61 public Object caseElement(ElementPattern p) { 62 if (!haveProcessed(p)) { 63 int s = p.getNameClass().containsSpecificity(name); 64 if (s > specificity) { 65 specificity = s; 66 pattern = p.getContent(); 67 } 68 else if (s == specificity && s != NameClass.SPECIFICITY_NONE) 69 pattern = builder.makeChoice(pattern, p.getContent()); 70 p.getContent().apply(this); 71 } 72 return null; 73 } 74 75 public Object caseOther(Pattern p) { 76 return null; 77 } 78 } 79 | Popular Tags |