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.IncludedGrammar;
006: import org.kohsuke.rngom.ast.builder.SchemaBuilder;
007: import org.kohsuke.rngom.ast.builder.Scope;
008: import org.kohsuke.rngom.ast.om.Location;
009: import org.kohsuke.rngom.ast.om.ParsedNameClass;
010: import org.kohsuke.rngom.ast.om.ParsedPattern;
011: import org.kohsuke.rngom.nc.NameClass;
012: import org.kohsuke.rngom.parse.IllegalSchemaException;
013: import org.kohsuke.rngom.parse.Parseable;
014: import org.xml.sax.Locator;
015:
016: import java.util.List;
017: import java.util.ArrayList;
018:
019: /**
020: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
021: */
022: final class PatternParseable implements Parseable {
023: private final DPattern pattern;
024:
025: public PatternParseable(DPattern p) {
026: this .pattern = p;
027: }
028:
029: public ParsedPattern parse(SchemaBuilder sb) throws BuildException {
030: return (ParsedPattern) pattern.accept(new Parser(sb));
031: }
032:
033: public ParsedPattern parseInclude(String uri, SchemaBuilder f,
034: IncludedGrammar g, String inheritedNs)
035: throws BuildException {
036: throw new UnsupportedOperationException();
037: }
038:
039: public ParsedPattern parseExternal(String uri, SchemaBuilder f,
040: Scope s, String inheritedNs) throws BuildException {
041: throw new UnsupportedOperationException();
042: }
043:
044: private static class Parser implements
045: DPatternVisitor<ParsedPattern> {
046: private final SchemaBuilder sb;
047:
048: public Parser(SchemaBuilder sb) {
049: this .sb = sb;
050: }
051:
052: private Annotations parseAnnotation(DPattern p) {
053: // TODO
054: return null;
055: }
056:
057: private Location parseLocation(DPattern p) {
058: Locator l = p.getLocation();
059: return sb.makeLocation(l.getSystemId(), l.getLineNumber(),
060: l.getColumnNumber());
061: }
062:
063: private ParsedNameClass parseNameClass(NameClass name) {
064: // TODO: reparse the name class
065: return name;
066: }
067:
068: public ParsedPattern onAttribute(DAttributePattern p) {
069: return sb.makeAttribute(parseNameClass(p.getName()),
070: (ParsedPattern) p.getChild().accept(this ),
071: parseLocation(p), parseAnnotation(p));
072: }
073:
074: public ParsedPattern onChoice(DChoicePattern p) {
075: List<ParsedPattern> kids = new ArrayList<ParsedPattern>();
076: for (DPattern c = p.firstChild(); c != null; c = c.next)
077: kids.add((ParsedPattern) c.accept(this ));
078: return sb.makeChoice(kids, parseLocation(p), null);
079: }
080:
081: public ParsedPattern onData(DDataPattern p) {
082: // TODO
083: return null;
084: }
085:
086: public ParsedPattern onElement(DElementPattern p) {
087: return sb.makeElement(parseNameClass(p.getName()),
088: (ParsedPattern) p.getChild().accept(this ),
089: parseLocation(p), parseAnnotation(p));
090: }
091:
092: public ParsedPattern onEmpty(DEmptyPattern p) {
093: return sb.makeEmpty(parseLocation(p), parseAnnotation(p));
094: }
095:
096: public ParsedPattern onGrammar(DGrammarPattern p) {
097: // TODO
098: return null;
099: }
100:
101: public ParsedPattern onGroup(DGroupPattern p) {
102: List<ParsedPattern> kids = new ArrayList<ParsedPattern>();
103: for (DPattern c = p.firstChild(); c != null; c = c.next)
104: kids.add((ParsedPattern) c.accept(this ));
105: return sb.makeGroup(kids, parseLocation(p), null);
106: }
107:
108: public ParsedPattern onInterleave(DInterleavePattern p) {
109: List<ParsedPattern> kids = new ArrayList<ParsedPattern>();
110: for (DPattern c = p.firstChild(); c != null; c = c.next)
111: kids.add((ParsedPattern) c.accept(this ));
112: return sb.makeInterleave(kids, parseLocation(p), null);
113: }
114:
115: public ParsedPattern onList(DListPattern p) {
116: return sb
117: .makeList(
118: (ParsedPattern) p.getChild().accept(this ),
119: parseLocation(p), parseAnnotation(p));
120: }
121:
122: public ParsedPattern onMixed(DMixedPattern p) {
123: return sb.makeMixed((ParsedPattern) p.getChild().accept(
124: this ), parseLocation(p), parseAnnotation(p));
125: }
126:
127: public ParsedPattern onNotAllowed(DNotAllowedPattern p) {
128: return sb.makeNotAllowed(parseLocation(p),
129: parseAnnotation(p));
130: }
131:
132: public ParsedPattern onOneOrMore(DOneOrMorePattern p) {
133: return sb
134: .makeOneOrMore((ParsedPattern) p.getChild().accept(
135: this ), parseLocation(p), parseAnnotation(p));
136: }
137:
138: public ParsedPattern onOptional(DOptionalPattern p) {
139: return sb.makeOptional((ParsedPattern) p.getChild().accept(
140: this ), parseLocation(p), parseAnnotation(p));
141: }
142:
143: public ParsedPattern onRef(DRefPattern p) {
144: // TODO
145: return null;
146: }
147:
148: public ParsedPattern onText(DTextPattern p) {
149: return sb.makeText(parseLocation(p), parseAnnotation(p));
150: }
151:
152: public ParsedPattern onValue(DValuePattern p) {
153: return sb.makeValue(p.getDatatypeLibrary(), p.getType(), p
154: .getValue(), p.getContext(), p.getNs(),
155: parseLocation(p), parseAnnotation(p));
156: }
157:
158: public ParsedPattern onZeroOrMore(DZeroOrMorePattern p) {
159: return sb
160: .makeZeroOrMore((ParsedPattern) p.getChild()
161: .accept(this), parseLocation(p),
162: parseAnnotation(p));
163: }
164: }
165: }
|