001: package org.kohsuke.rngom.digested;
002:
003: import org.kohsuke.rngom.ast.builder.BuildException;
004: import org.kohsuke.rngom.ast.builder.DataPatternBuilder;
005: import org.kohsuke.rngom.ast.builder.ElementAnnotationBuilder;
006: import org.kohsuke.rngom.ast.builder.Grammar;
007: import org.kohsuke.rngom.ast.builder.NameClassBuilder;
008: import org.kohsuke.rngom.ast.builder.SchemaBuilder;
009: import org.kohsuke.rngom.ast.builder.Scope;
010: import org.kohsuke.rngom.ast.om.ParsedPattern;
011: import org.kohsuke.rngom.ast.util.LocatorImpl;
012: import org.kohsuke.rngom.nc.NameClass;
013: import org.kohsuke.rngom.nc.NameClassBuilderImpl;
014: import org.kohsuke.rngom.parse.Context;
015: import org.kohsuke.rngom.parse.IllegalSchemaException;
016: import org.kohsuke.rngom.parse.Parseable;
017: import org.w3c.dom.Document;
018:
019: import javax.xml.parsers.DocumentBuilderFactory;
020: import javax.xml.parsers.ParserConfigurationException;
021: import java.util.List;
022:
023: /**
024: * Parses as {@link Parseable} into a {@link DPattern}.
025: *
026: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
027: */
028: public class DSchemaBuilderImpl
029: implements
030: SchemaBuilder<NameClass, DPattern, ElementWrapper, LocatorImpl, Annotation, CommentListImpl> {
031:
032: private final NameClassBuilder ncb = new NameClassBuilderImpl();
033:
034: /**
035: * Used to parse annotations.
036: */
037: private final Document dom;
038:
039: public DSchemaBuilderImpl() {
040: try {
041: DocumentBuilderFactory dbf = DocumentBuilderFactory
042: .newInstance();
043: dbf.setNamespaceAware(true);
044: this .dom = dbf.newDocumentBuilder().newDocument();
045: } catch (ParserConfigurationException e) {
046: // impossible
047: throw new InternalError(e.getMessage());
048: }
049: }
050:
051: public NameClassBuilder getNameClassBuilder() throws BuildException {
052: return ncb;
053: }
054:
055: static DPattern wrap(DPattern p, LocatorImpl loc, Annotation anno) {
056: p.location = loc;
057: if (anno != null)
058: p.annotation = anno.getResult();
059: return p;
060: }
061:
062: static DContainerPattern addAll(DContainerPattern parent,
063: List<DPattern> children) {
064: for (DPattern c : children)
065: parent.add(c);
066: return parent;
067: }
068:
069: static DUnaryPattern addBody(DUnaryPattern parent,
070: ParsedPattern _body, LocatorImpl loc) {
071: parent.setChild((DPattern) _body);
072: return parent;
073: }
074:
075: public DPattern makeChoice(List<DPattern> patterns,
076: LocatorImpl loc, Annotation anno) throws BuildException {
077: return wrap(addAll(new DChoicePattern(), patterns), loc, anno);
078: }
079:
080: public DPattern makeInterleave(List<DPattern> patterns,
081: LocatorImpl loc, Annotation anno) throws BuildException {
082: return wrap(addAll(new DInterleavePattern(), patterns), loc,
083: anno);
084: }
085:
086: public DPattern makeGroup(List<DPattern> patterns, LocatorImpl loc,
087: Annotation anno) throws BuildException {
088: return wrap(addAll(new DGroupPattern(), patterns), loc, anno);
089: }
090:
091: public DPattern makeOneOrMore(DPattern p, LocatorImpl loc,
092: Annotation anno) throws BuildException {
093: return wrap(addBody(new DOneOrMorePattern(), p, loc), loc, anno);
094: }
095:
096: public DPattern makeZeroOrMore(DPattern p, LocatorImpl loc,
097: Annotation anno) throws BuildException {
098: return wrap(addBody(new DZeroOrMorePattern(), p, loc), loc,
099: anno);
100: }
101:
102: public DPattern makeOptional(DPattern p, LocatorImpl loc,
103: Annotation anno) throws BuildException {
104: return wrap(addBody(new DOptionalPattern(), p, loc), loc, anno);
105: }
106:
107: public DPattern makeList(DPattern p, LocatorImpl loc,
108: Annotation anno) throws BuildException {
109: return wrap(addBody(new DListPattern(), p, loc), loc, anno);
110: }
111:
112: public DPattern makeMixed(DPattern p, LocatorImpl loc,
113: Annotation anno) throws BuildException {
114: return wrap(addBody(new DMixedPattern(), p, loc), loc, anno);
115: }
116:
117: public DPattern makeEmpty(LocatorImpl loc, Annotation anno) {
118: return wrap(new DEmptyPattern(), loc, anno);
119: }
120:
121: public DPattern makeNotAllowed(LocatorImpl loc, Annotation anno) {
122: return wrap(new DNotAllowedPattern(), loc, anno);
123: }
124:
125: public DPattern makeText(LocatorImpl loc, Annotation anno) {
126: return wrap(new DTextPattern(), loc, anno);
127: }
128:
129: public DPattern makeAttribute(NameClass nc, DPattern p,
130: LocatorImpl loc, Annotation anno) throws BuildException {
131: return wrap(addBody(new DAttributePattern(nc), p, loc), loc,
132: anno);
133: }
134:
135: public DPattern makeElement(NameClass nc, DPattern p,
136: LocatorImpl loc, Annotation anno) throws BuildException {
137: return wrap(addBody(new DElementPattern(nc), p, loc), loc, anno);
138: }
139:
140: public DataPatternBuilder makeDataPatternBuilder(
141: String datatypeLibrary, String type, LocatorImpl loc)
142: throws BuildException {
143: return new DataPatternBuilderImpl(datatypeLibrary, type, loc);
144: }
145:
146: public DPattern makeValue(String datatypeLibrary, String type,
147: String value, Context c, String ns, LocatorImpl loc,
148: Annotation anno) throws BuildException {
149: return wrap(new DValuePattern(datatypeLibrary, type, value, c
150: .copy(), ns), loc, anno);
151: }
152:
153: public Grammar makeGrammar(Scope parent) {
154: return new GrammarBuilderImpl(new DGrammarPattern(), parent,
155: this );
156: }
157:
158: public DPattern annotate(DPattern p, Annotation anno)
159: throws BuildException {
160: // TODO: not sure when this is used
161: return p;
162: }
163:
164: public DPattern annotateAfter(DPattern p, ElementWrapper e)
165: throws BuildException {
166: // TODO
167: return p;
168: }
169:
170: public DPattern commentAfter(DPattern p, CommentListImpl comments)
171: throws BuildException {
172: // TODO
173: return p;
174: }
175:
176: public DPattern makeExternalRef(
177: Parseable current,
178: String uri,
179: String ns,
180: Scope<DPattern, ElementWrapper, LocatorImpl, Annotation, CommentListImpl> scope,
181: LocatorImpl loc, Annotation anno) throws BuildException,
182: IllegalSchemaException {
183: // TODO
184: return null;
185: }
186:
187: public LocatorImpl makeLocation(String systemId, int lineNumber,
188: int columnNumber) {
189: return new LocatorImpl(systemId, lineNumber, columnNumber);
190: }
191:
192: public Annotation makeAnnotations(CommentListImpl comments,
193: Context context) {
194: return new Annotation();
195: }
196:
197: public ElementAnnotationBuilder makeElementAnnotationBuilder(
198: String ns, String localName, String prefix,
199: LocatorImpl loc, CommentListImpl comments, Context context) {
200: String qname;
201: if (prefix == null)
202: qname = localName;
203: else
204: qname = prefix + ':' + localName;
205: return new ElementAnnotationBuilderImpl(dom.createElementNS(ns,
206: qname));
207: }
208:
209: public CommentListImpl makeCommentList() {
210: return null;
211: }
212:
213: public DPattern makeErrorPattern() {
214: return new DNotAllowedPattern();
215: }
216:
217: public boolean usesComments() {
218: return false;
219: }
220:
221: public DPattern expandPattern(DPattern p) throws BuildException,
222: IllegalSchemaException {
223: return p;
224: }
225: }
|