001    package com.thaiopensource.relaxng.impl;
002    
003    import java.util.Hashtable;
004    
005    public class FeasibleTransform {
006      private static class FeasiblePatternFunction extends AbstractPatternFunction {
007        private final SchemaPatternBuilder spb;
008        private final Hashtable elementTable = new Hashtable();
009    
010        FeasiblePatternFunction(SchemaPatternBuilder spb) {
011          this.spb = spb;
012        }
013    
014        public Object caseChoice(ChoicePattern p) {
015          return spb.makeChoice(p.getOperand1().applyForPattern(this), p.getOperand2().applyForPattern(this));
016        }
017    
018        public Object caseGroup(GroupPattern p) {
019          return spb.makeGroup(p.getOperand1().applyForPattern(this), p.getOperand2().applyForPattern(this));
020        }
021    
022        public Object caseInterleave(InterleavePattern p) {
023          return spb.makeInterleave(p.getOperand1().applyForPattern(this), p.getOperand2().applyForPattern(this));
024        }
025    
026        public Object caseOneOrMore(OneOrMorePattern p) {
027          return spb.makeOneOrMore(p.getOperand().applyForPattern(this));
028        }
029    
030        public Object caseElement(ElementPattern p) {
031          if (elementTable.get(p) == null) {
032            elementTable.put(p, p);
033            p.setContent(p.getContent().applyForPattern(this));
034          }
035          return spb.makeOptional(p);
036        }
037    
038        public Object caseOther(Pattern p) {
039          return spb.makeOptional(p);
040        }
041      }
042    
043      public static Pattern transform(SchemaPatternBuilder spb, Pattern p) {
044        return p.applyForPattern(new FeasiblePatternFunction(spb));
045      }
046    }