1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.sequencer.ddl;
25
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.LinkedList;
29 import java.util.List;
30 import net.jcip.annotations.Immutable;
31 import org.modeshape.common.text.ParsingException;
32 import org.modeshape.common.text.Position;
33 import org.modeshape.graph.JcrLexicon;
34 import org.modeshape.graph.JcrNtLexicon;
35 import org.modeshape.sequencer.ddl.dialect.derby.DerbyDdlParser;
36 import org.modeshape.sequencer.ddl.dialect.oracle.OracleDdlParser;
37 import org.modeshape.sequencer.ddl.dialect.postgres.PostgresDdlParser;
38 import org.modeshape.sequencer.ddl.node.AstNode;
39
40
41
42
43
44
45
46
47
48
49
50
51 @Immutable
52 public class DdlParsers {
53
54 public static final List<DdlParser> BUILTIN_PARSERS;
55
56 static {
57 List<DdlParser> parsers = new ArrayList<DdlParser>();
58 parsers.add(new StandardDdlParser());
59 parsers.add(new OracleDdlParser());
60 parsers.add(new DerbyDdlParser());
61 parsers.add(new PostgresDdlParser());
62 BUILTIN_PARSERS = Collections.unmodifiableList(parsers);
63 }
64
65 private List<DdlParser> parsers;
66
67
68
69
70 public DdlParsers() {
71 this.parsers = BUILTIN_PARSERS;
72 }
73
74
75
76
77
78
79 public DdlParsers( List<DdlParser> parsers ) {
80 this.parsers = (parsers != null && !parsers.isEmpty()) ? parsers : BUILTIN_PARSERS;
81 }
82
83
84
85
86
87
88
89
90
91 public AstNode parse( String ddl,
92 String fileName ) throws ParsingException {
93 assert ddl != null;
94
95
96 List<ScoredParser> scoredParsers = new LinkedList<ScoredParser>();
97 for (DdlParser parser : parsers) {
98 DdlParserScorer scorer = new DdlParserScorer();
99 try {
100 Object scoreResult = parser.score(ddl, fileName, scorer);
101 scoredParsers.add(new ScoredParser(parser, scorer, scoreResult));
102 } catch (Throwable t) {
103
104 }
105 }
106
107 if (!scoredParsers.isEmpty()) {
108 Collections.sort(scoredParsers);
109 } else {
110
111 for (DdlParser parser : parsers) {
112 scoredParsers.add(new ScoredParser(parser, new DdlParserScorer(), null));
113 }
114 }
115
116
117
118
119 AstNode astRoot = null;
120 RuntimeException firstException = null;
121 for (ScoredParser scoredParser : scoredParsers) {
122 try {
123 astRoot = new AstNode(StandardDdlLexicon.STATEMENTS_CONTAINER);
124 astRoot.setProperty(JcrLexicon.PRIMARY_TYPE, JcrNtLexicon.UNSTRUCTURED);
125 DdlParser parser = scoredParser.getParser();
126 parser.parse(ddl, astRoot, scoredParser.getScoringResult());
127 astRoot.setProperty(StandardDdlLexicon.PARSER_ID, parser.getId());
128
129 return astRoot;
130 } catch (ParsingException t) {
131 if (firstException == null) firstException = t;
132
133 } catch (RuntimeException t) {
134 if (firstException == null) firstException = t;
135
136 }
137 }
138 if (firstException != null) {
139 throw firstException;
140 }
141
142 throw new ParsingException(new Position(-1, 1, 0), DdlSequencerI18n.errorParsingDdlContent.text());
143 }
144
145 protected static class ScoredParser implements Comparable<ScoredParser> {
146 private final DdlParserScorer scorer;
147 private final DdlParser parser;
148 private final Object scoringResult;
149
150 protected ScoredParser( DdlParser parser,
151 DdlParserScorer scorer,
152 Object scoringResult ) {
153 this.parser = parser;
154 this.scorer = scorer;
155 this.scoringResult = scoringResult;
156 assert this.parser != null;
157 assert this.scorer != null;
158 }
159
160
161
162
163 public DdlParser getParser() {
164 return parser;
165 }
166
167
168
169
170 public DdlParserScorer getScorer() {
171 return scorer;
172 }
173
174
175
176
177 public Object getScoringResult() {
178 return scoringResult;
179 }
180
181
182
183
184
185
186 public int compareTo( ScoredParser that ) {
187 if (that == null) return 1;
188 return that.getScorer().getScore() - this.getScorer().getScore();
189 }
190 }
191 }