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.digested;
  47 
  48 import com.sun.xml.internal.rngom.ast.builder.BuildException;
  49 import com.sun.xml.internal.rngom.ast.builder.DataPatternBuilder;
  50 import com.sun.xml.internal.rngom.ast.builder.ElementAnnotationBuilder;
  51 import com.sun.xml.internal.rngom.ast.builder.Grammar;
  52 import com.sun.xml.internal.rngom.ast.builder.NameClassBuilder;
  53 import com.sun.xml.internal.rngom.ast.builder.SchemaBuilder;
  54 import com.sun.xml.internal.rngom.ast.builder.Scope;
  55 import com.sun.xml.internal.rngom.ast.om.ParsedPattern;
  56 import com.sun.xml.internal.rngom.ast.util.LocatorImpl;
  57 import com.sun.xml.internal.rngom.nc.NameClass;
  58 import com.sun.xml.internal.rngom.nc.NameClassBuilderImpl;
  59 import com.sun.xml.internal.rngom.parse.Context;
  60 import com.sun.xml.internal.rngom.parse.IllegalSchemaException;
  61 import com.sun.xml.internal.rngom.parse.Parseable;
  62 import org.w3c.dom.Document;
  63 
  64 import javax.xml.parsers.DocumentBuilderFactory;
  65 import javax.xml.parsers.ParserConfigurationException;
  66 import java.util.List;
  67 
  68 /**
  69  * Parses as {@link Parseable} into a {@link DPattern}.
  70  *
  71  * @author Kohsuke Kawaguchi (kk@kohsuke.org)
  72  */
  73 public class DSchemaBuilderImpl implements SchemaBuilder
  74     <NameClass,DPattern,ElementWrapper,LocatorImpl,Annotation,CommentListImpl> {
  75 
  76     private final NameClassBuilder ncb = new NameClassBuilderImpl();
  77 
  78     /**
  79      * Used to parse annotations.
  80      */
  81     private final Document dom;
  82 
  83     public DSchemaBuilderImpl() {
  84         try {
  85             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  86             dbf.setNamespaceAware(true);
  87             this.dom = dbf.newDocumentBuilder().newDocument();
  88         } catch (ParserConfigurationException e) {
  89             // impossible
  90             throw new InternalError(e.getMessage());
  91         }
  92     }
  93 
  94     public NameClassBuilder getNameClassBuilder() throws BuildException {
  95         return ncb;
  96     }
  97 
  98     static  DPattern wrap( DPattern p, LocatorImpl loc, Annotation anno ) {
  99         p.location = loc;
 100         if(anno!=null)
 101             p.annotation = anno.getResult();
 102         return p;
 103     }
 104 
 105     static DContainerPattern addAll( DContainerPattern parent, List<DPattern> children) {
 106         for (DPattern c : children)
 107             parent.add(c);
 108         return parent;
 109     }
 110 
 111     static DUnaryPattern addBody( DUnaryPattern parent, ParsedPattern _body, LocatorImpl loc ) {
 112         parent.setChild( (DPattern)_body );
 113         return parent;
 114     }
 115 
 116     public DPattern makeChoice(List<DPattern> patterns, LocatorImpl loc, Annotation anno) throws BuildException {
 117         return wrap(addAll(new DChoicePattern(),patterns),loc,anno);
 118     }
 119 
 120     public DPattern makeInterleave(List<DPattern> patterns, LocatorImpl loc, Annotation anno) throws BuildException {
 121         return wrap(addAll(new DInterleavePattern(),patterns),loc,anno);
 122     }
 123 
 124     public DPattern makeGroup(List<DPattern> patterns, LocatorImpl loc, Annotation anno) throws BuildException {
 125         return wrap(addAll(new DGroupPattern(),patterns),loc,anno);
 126     }
 127 
 128     public DPattern makeOneOrMore(DPattern p, LocatorImpl loc, Annotation anno) throws BuildException {
 129         return wrap(addBody(new DOneOrMorePattern(),p,loc),loc,anno);
 130     }
 131 
 132     public DPattern makeZeroOrMore(DPattern p, LocatorImpl loc, Annotation anno) throws BuildException {
 133         return wrap(addBody(new DZeroOrMorePattern(),p,loc),loc,anno);
 134     }
 135 
 136     public DPattern makeOptional(DPattern p, LocatorImpl loc, Annotation anno) throws BuildException {
 137         return wrap(addBody(new DOptionalPattern(),p,loc),loc,anno);
 138     }
 139 
 140     public DPattern makeList(DPattern p, LocatorImpl loc, Annotation anno) throws BuildException {
 141         return wrap(addBody(new DListPattern(),p,loc),loc,anno);
 142     }
 143 
 144     public DPattern makeMixed(DPattern p, LocatorImpl loc, Annotation anno) throws BuildException {
 145         return wrap(addBody(new DMixedPattern(),p,loc),loc,anno);
 146     }
 147 
 148     public DPattern makeEmpty(LocatorImpl loc, Annotation anno) {
 149         return wrap(new DEmptyPattern(),loc,anno);
 150     }
 151 
 152     public DPattern makeNotAllowed(LocatorImpl loc, Annotation anno) {
 153         return wrap(new DNotAllowedPattern(),loc,anno);
 154     }
 155 
 156     public DPattern makeText(LocatorImpl loc, Annotation anno) {
 157         return wrap(new DTextPattern(),loc,anno);
 158     }
 159 
 160     public DPattern makeAttribute(NameClass nc, DPattern p, LocatorImpl loc, Annotation anno) throws BuildException {
 161         return wrap(addBody(new DAttributePattern(nc),p,loc),loc,anno);
 162     }
 163 
 164     public DPattern makeElement(NameClass nc, DPattern p, LocatorImpl loc, Annotation anno) throws BuildException {
 165         return wrap(addBody(new DElementPattern(nc),p,loc),loc,anno);
 166     }
 167 
 168     public DataPatternBuilder makeDataPatternBuilder(String datatypeLibrary, String type, LocatorImpl loc) throws BuildException {
 169         return new DataPatternBuilderImpl(datatypeLibrary,type,loc);
 170     }
 171 
 172     public DPattern makeValue(String datatypeLibrary, String type, String value, Context c, String ns, LocatorImpl loc, Annotation anno) throws BuildException {
 173         return wrap(new DValuePattern(datatypeLibrary,type,value,c.copy(),ns),loc,anno);
 174     }
 175 
 176     public Grammar makeGrammar(Scope parent) {
 177         return new GrammarBuilderImpl(new DGrammarPattern(),parent,this);
 178     }
 179 
 180     public DPattern annotate(DPattern p, Annotation anno) throws BuildException {
 181         // TODO: not sure when this is used
 182         return p;
 183     }
 184 
 185     public DPattern annotateAfter(DPattern p, ElementWrapper e) throws BuildException {
 186         // TODO
 187         return p;
 188     }
 189 
 190     public DPattern commentAfter(DPattern p, CommentListImpl comments) throws BuildException {
 191         // TODO
 192         return p;
 193     }
 194 
 195     public DPattern makeExternalRef(Parseable current, String uri, String ns,
 196                                     Scope<DPattern, ElementWrapper, LocatorImpl, Annotation, CommentListImpl> scope, LocatorImpl loc, Annotation anno) throws BuildException, IllegalSchemaException {
 197         // TODO
 198         return null;
 199     }
 200 
 201     public LocatorImpl makeLocation(String systemId, int lineNumber, int columnNumber) {
 202         return new LocatorImpl(systemId,lineNumber,columnNumber);
 203     }
 204 
 205     public Annotation makeAnnotations(CommentListImpl comments, Context context) {
 206         return new Annotation();
 207     }
 208 
 209     public ElementAnnotationBuilder makeElementAnnotationBuilder(String ns, String localName, String prefix, LocatorImpl loc, CommentListImpl comments, Context context) {
 210         String qname;
 211         if(prefix==null)
 212             qname = localName;
 213         else
 214             qname = prefix+':'+localName;
 215         return new ElementAnnotationBuilderImpl(dom.createElementNS(ns,qname));
 216     }
 217 
 218     public CommentListImpl makeCommentList() {
 219         return null;
 220     }
 221 
 222     public DPattern makeErrorPattern() {
 223         return new DNotAllowedPattern();
 224     }
 225 
 226     public boolean usesComments() {
 227         return false;
 228     }
 229 
 230     public DPattern expandPattern(DPattern p) throws BuildException, IllegalSchemaException {
 231         return p;
 232     }
 233 }