001: package org.kohsuke.rngom.digested;
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.Div;
007: import org.kohsuke.rngom.ast.builder.Grammar;
008: import org.kohsuke.rngom.ast.builder.Include;
009: import org.kohsuke.rngom.ast.builder.Scope;
010: import org.kohsuke.rngom.ast.om.Location;
011: import org.kohsuke.rngom.ast.om.ParsedElementAnnotation;
012: import org.kohsuke.rngom.ast.om.ParsedPattern;
013: import org.kohsuke.rngom.ast.util.LocatorImpl;
014: import org.w3c.dom.Element;
015:
016: import java.util.ArrayList;
017: import java.util.List;
018:
019: /**
020: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
021: */
022: class GrammarBuilderImpl implements Grammar, Div {
023:
024: protected final DGrammarPattern grammar;
025:
026: protected final Scope parent;
027:
028: protected final DSchemaBuilderImpl sb;
029:
030: /**
031: * Additional top-level element annotations.
032: * Can be null.
033: */
034: private List<Element> additionalElementAnnotations;
035:
036: public GrammarBuilderImpl(DGrammarPattern p, Scope parent,
037: DSchemaBuilderImpl sb) {
038: this .grammar = p;
039: this .parent = parent;
040: this .sb = sb;
041: }
042:
043: public ParsedPattern endGrammar(Location loc, Annotations anno)
044: throws BuildException {
045: if (anno != null)
046: grammar.annotation = ((Annotation) anno).getResult();
047: if (additionalElementAnnotations != null) {
048: if (grammar.annotation == null)
049: grammar.annotation = new DAnnotation();
050: grammar.annotation.contents
051: .addAll(additionalElementAnnotations);
052: }
053: return grammar;
054: }
055:
056: public void endDiv(Location loc, Annotations anno)
057: throws BuildException {
058: }
059:
060: public void define(String name, Combine combine,
061: ParsedPattern pattern, Location loc, Annotations anno)
062: throws BuildException {
063: if (name == START)
064: grammar.start = (DPattern) pattern;
065: else {
066: // TODO: handle combine
067: DDefine d = grammar.getOrAdd(name);
068: d.setPattern((DPattern) pattern);
069: if (anno != null)
070: d.annotation = ((Annotation) anno).getResult();
071: }
072: }
073:
074: public void topLevelAnnotation(ParsedElementAnnotation ea)
075: throws BuildException {
076: if (additionalElementAnnotations == null)
077: additionalElementAnnotations = new ArrayList<Element>();
078: additionalElementAnnotations.add(((ElementWrapper) ea).element);
079: }
080:
081: public void topLevelComment(CommentList comments)
082: throws BuildException {
083: }
084:
085: public Div makeDiv() {
086: return this ;
087: }
088:
089: public Include makeInclude() {
090: return new IncludeImpl(grammar, parent, sb);
091: }
092:
093: public ParsedPattern makeParentRef(String name, Location loc,
094: Annotations anno) throws BuildException {
095: return parent.makeRef(name, loc, anno);
096: }
097:
098: public ParsedPattern makeRef(String name, Location loc,
099: Annotations anno) throws BuildException {
100: return DSchemaBuilderImpl.wrap(new DRefPattern(grammar
101: .getOrAdd(name)), (LocatorImpl) loc, (Annotation) anno);
102: }
103: }
|