1/*
2 * Copyright (c) 2005, 2012, 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 */
46package com.sun.xml.internal.rngom.parse.host;
47
48import com.sun.xml.internal.rngom.ast.builder.Annotations;
49import com.sun.xml.internal.rngom.ast.builder.BuildException;
50import com.sun.xml.internal.rngom.ast.builder.CommentList;
51import com.sun.xml.internal.rngom.ast.builder.DataPatternBuilder;
52import com.sun.xml.internal.rngom.ast.builder.ElementAnnotationBuilder;
53import com.sun.xml.internal.rngom.ast.builder.Grammar;
54import com.sun.xml.internal.rngom.ast.builder.NameClassBuilder;
55import com.sun.xml.internal.rngom.ast.builder.SchemaBuilder;
56import com.sun.xml.internal.rngom.ast.builder.Scope;
57import com.sun.xml.internal.rngom.ast.om.Location;
58import com.sun.xml.internal.rngom.ast.om.ParsedElementAnnotation;
59import com.sun.xml.internal.rngom.ast.om.ParsedNameClass;
60import com.sun.xml.internal.rngom.ast.om.ParsedPattern;
61import com.sun.xml.internal.rngom.parse.Context;
62import com.sun.xml.internal.rngom.parse.IllegalSchemaException;
63import com.sun.xml.internal.rngom.parse.Parseable;
64
65import java.util.List;
66import java.util.ArrayList;
67
68/**
69 *
70 * @author
71 *      Kohsuke Kawaguchi (kk@kohsuke.org)
72 */
73public class SchemaBuilderHost extends Base implements SchemaBuilder {
74    final SchemaBuilder lhs;
75    final SchemaBuilder rhs;
76
77    public SchemaBuilderHost( SchemaBuilder lhs, SchemaBuilder rhs ) {
78        this.lhs = lhs;
79        this.rhs = rhs;
80    }
81
82    public ParsedPattern annotate(ParsedPattern _p, Annotations _anno)
83        throws BuildException {
84
85        ParsedPatternHost p = (ParsedPatternHost) _p;
86        AnnotationsHost a = cast(_anno);
87
88        return new ParsedPatternHost(
89            lhs.annotate(p.lhs, a.lhs),
90            rhs.annotate(p.lhs, a.lhs) );
91    }
92
93    public ParsedPattern annotateAfter(ParsedPattern _p,
94        ParsedElementAnnotation _e) throws BuildException {
95
96        ParsedPatternHost p = (ParsedPatternHost) _p;
97        ParsedElementAnnotationHost e = (ParsedElementAnnotationHost) _e;
98        return new ParsedPatternHost(
99            lhs.annotateAfter(p.lhs, e.lhs),
100            rhs.annotateAfter(p.rhs, e.rhs));
101    }
102
103    public ParsedPattern commentAfter(ParsedPattern _p, CommentList _comments)
104        throws BuildException {
105
106        ParsedPatternHost p = (ParsedPatternHost) _p;
107        CommentListHost comments = (CommentListHost) _comments;
108
109        return new ParsedPatternHost(
110            lhs.commentAfter(p.lhs, comments==null?null:comments.lhs),
111            rhs.commentAfter(p.rhs, comments==null?null:comments.rhs));
112    }
113
114    public ParsedPattern expandPattern(ParsedPattern _p) throws BuildException, IllegalSchemaException {
115        ParsedPatternHost p = (ParsedPatternHost) _p;
116        return new ParsedPatternHost(
117            lhs.expandPattern(p.lhs),
118            rhs.expandPattern(p.rhs));
119    }
120
121    public NameClassBuilder getNameClassBuilder() throws BuildException {
122        return new NameClassBuilderHost( lhs.getNameClassBuilder(), rhs.getNameClassBuilder() );
123    }
124
125    public Annotations makeAnnotations(CommentList _comments, Context context) {
126        CommentListHost comments = (CommentListHost) _comments;
127        Annotations l = lhs.makeAnnotations((comments!=null)?comments.lhs:null, context);
128        Annotations r = rhs.makeAnnotations((comments!=null)?comments.rhs:null, context);
129        if(l==null || r==null)
130            throw new IllegalArgumentException("annotations cannot be null");
131        return new AnnotationsHost(l,r);
132    }
133
134    public ParsedPattern makeAttribute(ParsedNameClass _nc, ParsedPattern _p,
135        Location _loc, Annotations _anno) throws BuildException {
136
137        ParsedNameClassHost nc = (ParsedNameClassHost) _nc;
138        ParsedPatternHost p = (ParsedPatternHost) _p;
139        LocationHost loc = cast(_loc);
140        AnnotationsHost anno = cast(_anno);
141
142        return new ParsedPatternHost(
143            lhs.makeAttribute(nc.lhs, p.lhs, loc.lhs, anno.lhs),
144            rhs.makeAttribute(nc.rhs, p.rhs, loc.rhs, anno.rhs));
145    }
146
147    public ParsedPattern makeChoice(List patterns,
148        Location _loc, Annotations _anno) throws BuildException {
149
150        List<ParsedPattern> lp = new ArrayList<ParsedPattern>();
151        List<ParsedPattern> rp = new ArrayList<ParsedPattern>();
152        for( int i=0; i<patterns.size(); i++ ) {
153            lp.add( ((ParsedPatternHost)patterns.get(i)).lhs);
154            rp.add( ((ParsedPatternHost)patterns.get(i)).rhs);
155        }
156        LocationHost loc = cast(_loc);
157        AnnotationsHost anno = cast(_anno);
158
159        return new ParsedPatternHost(
160            lhs.makeChoice(lp, loc.lhs, anno.lhs),
161            rhs.makeChoice(rp, loc.rhs, anno.rhs));
162    }
163
164    public CommentList makeCommentList() {
165        return new CommentListHost(
166            lhs.makeCommentList(),
167            rhs.makeCommentList() );
168    }
169
170    public DataPatternBuilder makeDataPatternBuilder(String datatypeLibrary,
171        String type, Location _loc) throws BuildException {
172        LocationHost loc = cast(_loc);
173
174        return new DataPatternBuilderHost(
175            lhs.makeDataPatternBuilder(datatypeLibrary, type, loc.lhs),
176            rhs.makeDataPatternBuilder(datatypeLibrary, type, loc.rhs) );
177    }
178
179    public ParsedPattern makeElement(ParsedNameClass _nc, ParsedPattern _p,
180        Location _loc, Annotations _anno) throws BuildException {
181
182        ParsedNameClassHost nc = (ParsedNameClassHost) _nc;
183        ParsedPatternHost p = (ParsedPatternHost) _p;
184        LocationHost loc = cast(_loc);
185        AnnotationsHost anno = cast(_anno);
186
187        return new ParsedPatternHost(
188            lhs.makeElement(nc.lhs, p.lhs, loc.lhs, anno.lhs),
189            rhs.makeElement(nc.rhs, p.rhs, loc.rhs, anno.rhs));
190    }
191
192    public ElementAnnotationBuilder makeElementAnnotationBuilder(String ns,
193        String localName, String prefix, Location _loc, CommentList _comments,
194        Context context) {
195        LocationHost loc = cast(_loc);
196        CommentListHost comments = (CommentListHost) _comments;
197
198        return new ElementAnnotationBuilderHost(
199            lhs.makeElementAnnotationBuilder(ns, localName, prefix, loc.lhs, comments==null?null:comments.lhs, context),
200            rhs.makeElementAnnotationBuilder(ns, localName, prefix, loc.rhs, comments==null?null:comments.rhs, context) );
201    }
202
203    public ParsedPattern makeEmpty(Location _loc, Annotations _anno) {
204        LocationHost loc = cast(_loc);
205        AnnotationsHost anno = cast(_anno);
206
207        return new ParsedPatternHost(
208            lhs.makeEmpty(loc.lhs, anno.lhs),
209            rhs.makeEmpty(loc.rhs, anno.rhs));
210    }
211
212    public ParsedPattern makeErrorPattern() {
213        return new ParsedPatternHost(
214            lhs.makeErrorPattern(),
215            rhs.makeErrorPattern() );
216    }
217
218    public ParsedPattern makeExternalRef(Parseable current, String uri,
219        String ns, Scope _scope, Location _loc, Annotations _anno)
220        throws BuildException, IllegalSchemaException {
221
222        ScopeHost scope = (ScopeHost) _scope;
223        LocationHost loc = cast(_loc);
224        AnnotationsHost anno = cast(_anno);
225
226        return new ParsedPatternHost(
227            lhs.makeExternalRef(current, uri, ns, scope.lhs, loc.lhs, anno.lhs),
228            rhs.makeExternalRef(current, uri, ns, scope.rhs, loc.rhs, anno.rhs) );
229    }
230
231    public Grammar makeGrammar(Scope _parent) {
232        ScopeHost parent = (ScopeHost) _parent;
233
234        return new GrammarHost(
235            lhs.makeGrammar((parent!=null)?parent.lhs:null),
236            rhs.makeGrammar((parent!=null)?parent.rhs:null) );
237    }
238
239    public ParsedPattern makeGroup(List patterns,
240        Location _loc, Annotations _anno) throws BuildException {
241
242        List<ParsedPattern> lp = new ArrayList<ParsedPattern>();
243        List<ParsedPattern> rp = new ArrayList<ParsedPattern>();
244        for( int i=0; i<patterns.size(); i++ ) {
245            lp.add( ((ParsedPatternHost)patterns.get(i)).lhs);
246            rp.add( ((ParsedPatternHost)patterns.get(i)).rhs);
247        }
248        LocationHost loc = cast(_loc);
249        AnnotationsHost anno = cast(_anno);
250
251        return new ParsedPatternHost(
252            lhs.makeGroup(lp, loc.lhs, anno.lhs),
253            rhs.makeGroup(rp, loc.rhs, anno.rhs));
254    }
255
256    public ParsedPattern makeInterleave(List patterns,
257        Location _loc, Annotations _anno) throws BuildException {
258
259        List<ParsedPattern> lp = new ArrayList<ParsedPattern>();
260        List<ParsedPattern> rp = new ArrayList<ParsedPattern>();
261        for( int i=0; i<patterns.size(); i++ ) {
262            lp.add( ((ParsedPatternHost)patterns.get(i)).lhs);
263            rp.add( ((ParsedPatternHost)patterns.get(i)).rhs);
264        }
265        LocationHost loc = cast(_loc);
266        AnnotationsHost anno = cast(_anno);
267
268        return new ParsedPatternHost(
269            lhs.makeInterleave(lp, loc.lhs, anno.lhs),
270            rhs.makeInterleave(rp, loc.rhs, anno.rhs));
271    }
272
273    public ParsedPattern makeList(ParsedPattern _p, Location _loc,
274        Annotations _anno) throws BuildException {
275
276        ParsedPatternHost p = (ParsedPatternHost) _p;
277        LocationHost loc = cast(_loc);
278        AnnotationsHost anno = cast(_anno);
279
280        return new ParsedPatternHost(
281            lhs.makeList(p.lhs, loc.lhs, anno.lhs),
282            rhs.makeList(p.rhs, loc.rhs, anno.rhs));
283    }
284
285    public Location makeLocation(String systemId, int lineNumber,
286        int columnNumber) {
287        return new LocationHost(
288            lhs.makeLocation(systemId, lineNumber, columnNumber),
289            rhs.makeLocation(systemId, lineNumber, columnNumber));
290    }
291
292    public ParsedPattern makeMixed(ParsedPattern _p, Location _loc,
293        Annotations _anno) throws BuildException {
294
295        ParsedPatternHost p = (ParsedPatternHost) _p;
296        LocationHost loc = cast(_loc);
297        AnnotationsHost anno = cast(_anno);
298
299        return new ParsedPatternHost(
300            lhs.makeMixed(p.lhs, loc.lhs, anno.lhs),
301            rhs.makeMixed(p.rhs, loc.rhs, anno.rhs));
302    }
303
304    public ParsedPattern makeNotAllowed(Location _loc, Annotations _anno) {
305        LocationHost loc = cast(_loc);
306        AnnotationsHost anno = cast(_anno);
307
308        return new ParsedPatternHost(
309            lhs.makeNotAllowed(loc.lhs, anno.lhs),
310            rhs.makeNotAllowed(loc.rhs, anno.rhs));
311    }
312
313    public ParsedPattern makeOneOrMore(ParsedPattern _p, Location _loc,
314        Annotations _anno) throws BuildException {
315
316        ParsedPatternHost p = (ParsedPatternHost) _p;
317        LocationHost loc = cast(_loc);
318        AnnotationsHost anno = cast(_anno);
319
320        return new ParsedPatternHost(
321            lhs.makeOneOrMore(p.lhs, loc.lhs, anno.lhs),
322            rhs.makeOneOrMore(p.rhs, loc.rhs, anno.rhs));
323    }
324
325    public ParsedPattern makeZeroOrMore(ParsedPattern _p, Location _loc,
326        Annotations _anno) throws BuildException {
327
328        ParsedPatternHost p = (ParsedPatternHost) _p;
329        LocationHost loc = cast(_loc);
330        AnnotationsHost anno = cast(_anno);
331
332        return new ParsedPatternHost(
333            lhs.makeZeroOrMore(p.lhs, loc.lhs, anno.lhs),
334            rhs.makeZeroOrMore(p.rhs, loc.rhs, anno.rhs));
335    }
336
337    public ParsedPattern makeOptional(ParsedPattern _p, Location _loc,
338        Annotations _anno) throws BuildException {
339
340        ParsedPatternHost p = (ParsedPatternHost) _p;
341        LocationHost loc = cast(_loc);
342        AnnotationsHost anno = cast(_anno);
343
344        return new ParsedPatternHost(
345            lhs.makeOptional(p.lhs, loc.lhs, anno.lhs),
346            rhs.makeOptional(p.rhs, loc.rhs, anno.rhs));
347    }
348
349    public ParsedPattern makeText(Location _loc, Annotations _anno) {
350        LocationHost loc = cast(_loc);
351        AnnotationsHost anno = cast(_anno);
352
353        return new ParsedPatternHost(
354            lhs.makeText(loc.lhs, anno.lhs),
355            rhs.makeText(loc.rhs, anno.rhs));
356    }
357
358    public ParsedPattern makeValue(String datatypeLibrary, String type,
359        String value, Context c, String ns, Location _loc, Annotations _anno)
360        throws BuildException {
361        LocationHost loc = cast(_loc);
362        AnnotationsHost anno = cast(_anno);
363
364        return new ParsedPatternHost(
365            lhs.makeValue(datatypeLibrary,type,value,c,ns,loc.lhs,anno.lhs),
366            rhs.makeValue(datatypeLibrary,type,value,c,ns,loc.rhs,anno.rhs));
367    }
368
369    public boolean usesComments() {
370        return lhs.usesComments() || rhs.usesComments();
371    }
372}
373