001: package org.kohsuke.rngom.parse.host;
002:
003: import org.kohsuke.rngom.ast.builder.Annotations;
004: import org.kohsuke.rngom.ast.builder.BuildException;
005: import org.kohsuke.rngom.ast.builder.CommentList;
006: import org.kohsuke.rngom.ast.builder.DataPatternBuilder;
007: import org.kohsuke.rngom.ast.builder.ElementAnnotationBuilder;
008: import org.kohsuke.rngom.ast.builder.Grammar;
009: import org.kohsuke.rngom.ast.builder.NameClassBuilder;
010: import org.kohsuke.rngom.ast.builder.SchemaBuilder;
011: import org.kohsuke.rngom.ast.builder.Scope;
012: import org.kohsuke.rngom.ast.om.Location;
013: import org.kohsuke.rngom.ast.om.ParsedElementAnnotation;
014: import org.kohsuke.rngom.ast.om.ParsedNameClass;
015: import org.kohsuke.rngom.ast.om.ParsedPattern;
016: import org.kohsuke.rngom.parse.Context;
017: import org.kohsuke.rngom.parse.IllegalSchemaException;
018: import org.kohsuke.rngom.parse.Parseable;
019:
020: import java.util.List;
021: import java.util.ArrayList;
022:
023: /**
024: *
025: * @author
026: * Kohsuke Kawaguchi (kk@kohsuke.org)
027: */
028: public class SchemaBuilderHost extends Base implements SchemaBuilder {
029: final SchemaBuilder lhs;
030: final SchemaBuilder rhs;
031:
032: public SchemaBuilderHost(SchemaBuilder lhs, SchemaBuilder rhs) {
033: this .lhs = lhs;
034: this .rhs = rhs;
035: }
036:
037: public ParsedPattern annotate(ParsedPattern _p, Annotations _anno)
038: throws BuildException {
039:
040: ParsedPatternHost p = (ParsedPatternHost) _p;
041: AnnotationsHost a = cast(_anno);
042:
043: return new ParsedPatternHost(lhs.annotate(p.lhs, a.lhs), rhs
044: .annotate(p.lhs, a.lhs));
045: }
046:
047: public ParsedPattern annotateAfter(ParsedPattern _p,
048: ParsedElementAnnotation _e) throws BuildException {
049:
050: ParsedPatternHost p = (ParsedPatternHost) _p;
051: ParsedElementAnnotationHost e = (ParsedElementAnnotationHost) _e;
052: return new ParsedPatternHost(lhs.annotateAfter(p.lhs, e.lhs),
053: rhs.annotateAfter(p.rhs, e.rhs));
054: }
055:
056: public ParsedPattern commentAfter(ParsedPattern _p,
057: CommentList _comments) throws BuildException {
058:
059: ParsedPatternHost p = (ParsedPatternHost) _p;
060: CommentListHost comments = (CommentListHost) _comments;
061:
062: return new ParsedPatternHost(lhs.commentAfter(p.lhs,
063: comments == null ? null : comments.lhs), rhs
064: .commentAfter(p.rhs, comments == null ? null
065: : comments.rhs));
066: }
067:
068: public ParsedPattern expandPattern(ParsedPattern _p)
069: throws BuildException, IllegalSchemaException {
070: ParsedPatternHost p = (ParsedPatternHost) _p;
071: return new ParsedPatternHost(lhs.expandPattern(p.lhs), rhs
072: .expandPattern(p.rhs));
073: }
074:
075: public NameClassBuilder getNameClassBuilder() throws BuildException {
076: return new NameClassBuilderHost(lhs.getNameClassBuilder(), rhs
077: .getNameClassBuilder());
078: }
079:
080: public Annotations makeAnnotations(CommentList _comments,
081: Context context) {
082: CommentListHost comments = (CommentListHost) _comments;
083: Annotations l = lhs.makeAnnotations(
084: (comments != null) ? comments.lhs : null, context);
085: Annotations r = rhs.makeAnnotations(
086: (comments != null) ? comments.rhs : null, context);
087: if (l == null || r == null)
088: throw new IllegalArgumentException(
089: "annotations cannot be null");
090: return new AnnotationsHost(l, r);
091: }
092:
093: public ParsedPattern makeAttribute(ParsedNameClass _nc,
094: ParsedPattern _p, Location _loc, Annotations _anno)
095: throws BuildException {
096:
097: ParsedNameClassHost nc = (ParsedNameClassHost) _nc;
098: ParsedPatternHost p = (ParsedPatternHost) _p;
099: LocationHost loc = cast(_loc);
100: AnnotationsHost anno = cast(_anno);
101:
102: return new ParsedPatternHost(lhs.makeAttribute(nc.lhs, p.lhs,
103: loc.lhs, anno.lhs), rhs.makeAttribute(nc.rhs, p.rhs,
104: loc.rhs, anno.rhs));
105: }
106:
107: public ParsedPattern makeChoice(List patterns, Location _loc,
108: Annotations _anno) throws BuildException {
109:
110: List<ParsedPattern> lp = new ArrayList<ParsedPattern>();
111: List<ParsedPattern> rp = new ArrayList<ParsedPattern>();
112: for (int i = 0; i < patterns.size(); i++) {
113: lp.add(((ParsedPatternHost) patterns.get(i)).lhs);
114: rp.add(((ParsedPatternHost) patterns.get(i)).rhs);
115: }
116: LocationHost loc = cast(_loc);
117: AnnotationsHost anno = cast(_anno);
118:
119: return new ParsedPatternHost(lhs.makeChoice(lp, loc.lhs,
120: anno.lhs), rhs.makeChoice(rp, loc.rhs, anno.rhs));
121: }
122:
123: public CommentList makeCommentList() {
124: return new CommentListHost(lhs.makeCommentList(), rhs
125: .makeCommentList());
126: }
127:
128: public DataPatternBuilder makeDataPatternBuilder(
129: String datatypeLibrary, String type, Location _loc)
130: throws BuildException {
131: LocationHost loc = cast(_loc);
132:
133: return new DataPatternBuilderHost(lhs.makeDataPatternBuilder(
134: datatypeLibrary, type, loc.lhs), rhs
135: .makeDataPatternBuilder(datatypeLibrary, type, loc.rhs));
136: }
137:
138: public ParsedPattern makeElement(ParsedNameClass _nc,
139: ParsedPattern _p, Location _loc, Annotations _anno)
140: throws BuildException {
141:
142: ParsedNameClassHost nc = (ParsedNameClassHost) _nc;
143: ParsedPatternHost p = (ParsedPatternHost) _p;
144: LocationHost loc = cast(_loc);
145: AnnotationsHost anno = cast(_anno);
146:
147: return new ParsedPatternHost(lhs.makeElement(nc.lhs, p.lhs,
148: loc.lhs, anno.lhs), rhs.makeElement(nc.rhs, p.rhs,
149: loc.rhs, anno.rhs));
150: }
151:
152: public ElementAnnotationBuilder makeElementAnnotationBuilder(
153: String ns, String localName, String prefix, Location _loc,
154: CommentList _comments, Context context) {
155: LocationHost loc = cast(_loc);
156: CommentListHost comments = (CommentListHost) _comments;
157:
158: return new ElementAnnotationBuilderHost(
159: lhs
160: .makeElementAnnotationBuilder(ns, localName,
161: prefix, loc.lhs,
162: comments == null ? null : comments.lhs,
163: context), rhs
164: .makeElementAnnotationBuilder(ns, localName,
165: prefix, loc.rhs,
166: comments == null ? null : comments.rhs,
167: context));
168: }
169:
170: public ParsedPattern makeEmpty(Location _loc, Annotations _anno) {
171: LocationHost loc = cast(_loc);
172: AnnotationsHost anno = cast(_anno);
173:
174: return new ParsedPatternHost(lhs.makeEmpty(loc.lhs, anno.lhs),
175: rhs.makeEmpty(loc.rhs, anno.rhs));
176: }
177:
178: public ParsedPattern makeErrorPattern() {
179: return new ParsedPatternHost(lhs.makeErrorPattern(), rhs
180: .makeErrorPattern());
181: }
182:
183: public ParsedPattern makeExternalRef(Parseable current, String uri,
184: String ns, Scope _scope, Location _loc, Annotations _anno)
185: throws BuildException, IllegalSchemaException {
186:
187: ScopeHost scope = (ScopeHost) _scope;
188: LocationHost loc = cast(_loc);
189: AnnotationsHost anno = cast(_anno);
190:
191: return new ParsedPatternHost(lhs.makeExternalRef(current, uri,
192: ns, scope.lhs, loc.lhs, anno.lhs), rhs.makeExternalRef(
193: current, uri, ns, scope.rhs, loc.rhs, anno.rhs));
194: }
195:
196: public Grammar makeGrammar(Scope _parent) {
197: ScopeHost parent = (ScopeHost) _parent;
198:
199: return new GrammarHost(lhs
200: .makeGrammar((parent != null) ? parent.lhs : null), rhs
201: .makeGrammar((parent != null) ? parent.rhs : null));
202: }
203:
204: public ParsedPattern makeGroup(List patterns, Location _loc,
205: Annotations _anno) throws BuildException {
206:
207: List<ParsedPattern> lp = new ArrayList<ParsedPattern>();
208: List<ParsedPattern> rp = new ArrayList<ParsedPattern>();
209: for (int i = 0; i < patterns.size(); i++) {
210: lp.add(((ParsedPatternHost) patterns.get(i)).lhs);
211: rp.add(((ParsedPatternHost) patterns.get(i)).rhs);
212: }
213: LocationHost loc = cast(_loc);
214: AnnotationsHost anno = cast(_anno);
215:
216: return new ParsedPatternHost(lhs.makeGroup(lp, loc.lhs,
217: anno.lhs), rhs.makeGroup(rp, loc.rhs, anno.rhs));
218: }
219:
220: public ParsedPattern makeInterleave(List patterns, Location _loc,
221: Annotations _anno) throws BuildException {
222:
223: List<ParsedPattern> lp = new ArrayList<ParsedPattern>();
224: List<ParsedPattern> rp = new ArrayList<ParsedPattern>();
225: for (int i = 0; i < patterns.size(); i++) {
226: lp.add(((ParsedPatternHost) patterns.get(i)).lhs);
227: rp.add(((ParsedPatternHost) patterns.get(i)).rhs);
228: }
229: LocationHost loc = cast(_loc);
230: AnnotationsHost anno = cast(_anno);
231:
232: return new ParsedPatternHost(lhs.makeInterleave(lp, loc.lhs,
233: anno.lhs), rhs.makeInterleave(rp, loc.rhs, anno.rhs));
234: }
235:
236: public ParsedPattern makeList(ParsedPattern _p, Location _loc,
237: Annotations _anno) throws BuildException {
238:
239: ParsedPatternHost p = (ParsedPatternHost) _p;
240: LocationHost loc = cast(_loc);
241: AnnotationsHost anno = cast(_anno);
242:
243: return new ParsedPatternHost(lhs.makeList(p.lhs, loc.lhs,
244: anno.lhs), rhs.makeList(p.rhs, loc.rhs, anno.rhs));
245: }
246:
247: public Location makeLocation(String systemId, int lineNumber,
248: int columnNumber) {
249: return new LocationHost(lhs.makeLocation(systemId, lineNumber,
250: columnNumber), rhs.makeLocation(systemId, lineNumber,
251: columnNumber));
252: }
253:
254: public ParsedPattern makeMixed(ParsedPattern _p, Location _loc,
255: Annotations _anno) throws BuildException {
256:
257: ParsedPatternHost p = (ParsedPatternHost) _p;
258: LocationHost loc = cast(_loc);
259: AnnotationsHost anno = cast(_anno);
260:
261: return new ParsedPatternHost(lhs.makeMixed(p.lhs, loc.lhs,
262: anno.lhs), rhs.makeMixed(p.rhs, loc.rhs, anno.rhs));
263: }
264:
265: public ParsedPattern makeNotAllowed(Location _loc, Annotations _anno) {
266: LocationHost loc = cast(_loc);
267: AnnotationsHost anno = cast(_anno);
268:
269: return new ParsedPatternHost(lhs.makeNotAllowed(loc.lhs,
270: anno.lhs), rhs.makeNotAllowed(loc.rhs, anno.rhs));
271: }
272:
273: public ParsedPattern makeOneOrMore(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(lhs.makeOneOrMore(p.lhs, loc.lhs,
281: anno.lhs), rhs.makeOneOrMore(p.rhs, loc.rhs, anno.rhs));
282: }
283:
284: public ParsedPattern makeZeroOrMore(ParsedPattern _p,
285: Location _loc, Annotations _anno) throws BuildException {
286:
287: ParsedPatternHost p = (ParsedPatternHost) _p;
288: LocationHost loc = cast(_loc);
289: AnnotationsHost anno = cast(_anno);
290:
291: return new ParsedPatternHost(lhs.makeZeroOrMore(p.lhs, loc.lhs,
292: anno.lhs), rhs.makeZeroOrMore(p.rhs, loc.rhs, anno.rhs));
293: }
294:
295: public ParsedPattern makeOptional(ParsedPattern _p, Location _loc,
296: Annotations _anno) throws BuildException {
297:
298: ParsedPatternHost p = (ParsedPatternHost) _p;
299: LocationHost loc = cast(_loc);
300: AnnotationsHost anno = cast(_anno);
301:
302: return new ParsedPatternHost(lhs.makeOptional(p.lhs, loc.lhs,
303: anno.lhs), rhs.makeOptional(p.rhs, loc.rhs, anno.rhs));
304: }
305:
306: public ParsedPattern makeText(Location _loc, Annotations _anno) {
307: LocationHost loc = cast(_loc);
308: AnnotationsHost anno = cast(_anno);
309:
310: return new ParsedPatternHost(lhs.makeText(loc.lhs, anno.lhs),
311: rhs.makeText(loc.rhs, anno.rhs));
312: }
313:
314: public ParsedPattern makeValue(String datatypeLibrary, String type,
315: String value, Context c, String ns, Location _loc,
316: Annotations _anno) throws BuildException {
317: LocationHost loc = cast(_loc);
318: AnnotationsHost anno = cast(_anno);
319:
320: return new ParsedPatternHost(lhs.makeValue(datatypeLibrary,
321: type, value, c, ns, loc.lhs, anno.lhs), rhs.makeValue(
322: datatypeLibrary, type, value, c, ns, loc.rhs, anno.rhs));
323: }
324:
325: public boolean usesComments() {
326: return lhs.usesComments() || rhs.usesComments();
327: }
328: }
|