1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 /*
  26  * Copyright (C) 2004-2011
  27  *
  28  * Permission is hereby granted, free of charge, to any person obtaining a copy
  29  * of this software and associated documentation files (the "Software"), to deal
  30  * in the Software without restriction, including without limitation the rights
  31  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  32  * copies of the Software, and to permit persons to whom the Software is
  33  * furnished to do so, subject to the following conditions:
  34  *
  35  * The above copyright notice and this permission notice shall be included in
  36  * all copies or substantial portions of the Software.
  37  *
  38  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  39  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  40  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  41  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  42  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  43  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  44  * THE SOFTWARE.
  45  */
  46 package com.sun.xml.internal.rngom.binary;
  47 
  48 public class PatternBuilder {
  49   private final EmptyPattern empty;
  50   protected final NotAllowedPattern notAllowed;
  51   protected final PatternInterner interner;
  52 
  53   public PatternBuilder() {
  54     empty = new EmptyPattern();
  55     notAllowed = new NotAllowedPattern();
  56     interner = new PatternInterner();
  57   }
  58 
  59   public PatternBuilder(PatternBuilder parent) {
  60     empty = parent.empty;
  61     notAllowed = parent.notAllowed;
  62     interner = new PatternInterner(parent.interner);
  63   }
  64 
  65   Pattern makeEmpty() {
  66     return empty;
  67   }
  68 
  69   Pattern makeNotAllowed() {
  70     return notAllowed;
  71   }
  72 
  73   Pattern makeGroup(Pattern p1, Pattern p2) {
  74     if (p1 == empty)
  75       return p2;
  76     if (p2 == empty)
  77       return p1;
  78     if (p1 == notAllowed || p2 == notAllowed)
  79       return notAllowed;
  80     if (false && p1 instanceof GroupPattern) {
  81       GroupPattern sp = (GroupPattern)p1;
  82       return makeGroup(sp.p1, makeGroup(sp.p2, p2));
  83     }
  84     Pattern p = new GroupPattern(p1, p2);
  85     return interner.intern(p);
  86   }
  87 
  88   Pattern makeInterleave(Pattern p1, Pattern p2) {
  89     if (p1 == empty)
  90       return p2;
  91     if (p2 == empty)
  92       return p1;
  93     if (p1 == notAllowed || p2 == notAllowed)
  94       return notAllowed;
  95     if (false && p1 instanceof InterleavePattern) {
  96       InterleavePattern ip = (InterleavePattern)p1;
  97       return makeInterleave(ip.p1, makeInterleave(ip.p2, p2));
  98     }
  99     if (false) {
 100     if (p2 instanceof InterleavePattern) {
 101       InterleavePattern ip = (InterleavePattern)p2;
 102       if (p1.hashCode() > ip.p1.hashCode())
 103         return makeInterleave(ip.p1, makeInterleave(p1, ip.p2));
 104     }
 105     else if (p1.hashCode() > p2.hashCode())
 106       return makeInterleave(p2, p1);
 107     }
 108     Pattern p = new InterleavePattern(p1, p2);
 109     return interner.intern(p);
 110   }
 111 
 112   Pattern makeChoice(Pattern p1, Pattern p2) {
 113     if (p1 == empty && p2.isNullable())
 114       return p2;
 115     if (p2 == empty && p1.isNullable())
 116       return p1;
 117     Pattern p = new ChoicePattern(p1, p2);
 118     return interner.intern(p);
 119   }
 120 
 121   Pattern makeOneOrMore(Pattern p) {
 122     if (p == empty
 123         || p == notAllowed
 124         || p instanceof OneOrMorePattern)
 125       return p;
 126     Pattern p1 = new OneOrMorePattern(p);
 127     return interner.intern(p1);
 128   }
 129 
 130   Pattern makeOptional(Pattern p) {
 131     return makeChoice(p, empty);
 132   }
 133 
 134   Pattern makeZeroOrMore(Pattern p) {
 135     return makeOptional(makeOneOrMore(p));
 136   }
 137 }