src/share/jaxws_classes/com/sun/xml/internal/rngom/binary/SchemaBuilderImpl.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 408
b0610cd08440
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25 /*
aoqi@0 26 * Copyright (C) 2004-2012
aoqi@0 27 *
aoqi@0 28 * Permission is hereby granted, free of charge, to any person obtaining a copy
aoqi@0 29 * of this software and associated documentation files (the "Software"), to deal
aoqi@0 30 * in the Software without restriction, including without limitation the rights
aoqi@0 31 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
aoqi@0 32 * copies of the Software, and to permit persons to whom the Software is
aoqi@0 33 * furnished to do so, subject to the following conditions:
aoqi@0 34 *
aoqi@0 35 * The above copyright notice and this permission notice shall be included in
aoqi@0 36 * all copies or substantial portions of the Software.
aoqi@0 37 *
aoqi@0 38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
aoqi@0 39 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
aoqi@0 40 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
aoqi@0 41 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
aoqi@0 42 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
aoqi@0 43 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
aoqi@0 44 * THE SOFTWARE.
aoqi@0 45 */
aoqi@0 46 package com.sun.xml.internal.rngom.binary;
aoqi@0 47
aoqi@0 48 import java.util.Enumeration;
aoqi@0 49 import java.util.Hashtable;
aoqi@0 50 import java.util.List;
aoqi@0 51
aoqi@0 52 import com.sun.xml.internal.rngom.ast.builder.Annotations;
aoqi@0 53 import com.sun.xml.internal.rngom.ast.builder.BuildException;
aoqi@0 54 import com.sun.xml.internal.rngom.ast.builder.CommentList;
aoqi@0 55 import com.sun.xml.internal.rngom.ast.builder.DataPatternBuilder;
aoqi@0 56 import com.sun.xml.internal.rngom.ast.builder.Div;
aoqi@0 57 import com.sun.xml.internal.rngom.ast.builder.ElementAnnotationBuilder;
aoqi@0 58 import com.sun.xml.internal.rngom.ast.builder.Grammar;
aoqi@0 59 import com.sun.xml.internal.rngom.ast.builder.GrammarSection;
aoqi@0 60 import com.sun.xml.internal.rngom.ast.builder.Include;
aoqi@0 61 import com.sun.xml.internal.rngom.ast.builder.IncludedGrammar;
aoqi@0 62 import com.sun.xml.internal.rngom.ast.builder.NameClassBuilder;
aoqi@0 63 import com.sun.xml.internal.rngom.ast.builder.SchemaBuilder;
aoqi@0 64 import com.sun.xml.internal.rngom.ast.builder.Scope;
aoqi@0 65 import com.sun.xml.internal.rngom.ast.om.Location;
aoqi@0 66 import com.sun.xml.internal.rngom.ast.om.ParsedElementAnnotation;
aoqi@0 67 import com.sun.xml.internal.rngom.ast.om.ParsedNameClass;
aoqi@0 68 import com.sun.xml.internal.rngom.ast.om.ParsedPattern;
aoqi@0 69 import com.sun.xml.internal.rngom.ast.util.LocatorImpl;
aoqi@0 70 import com.sun.xml.internal.rngom.dt.builtin.BuiltinDatatypeLibraryFactory;
aoqi@0 71 import com.sun.xml.internal.rngom.dt.CascadingDatatypeLibraryFactory;
aoqi@0 72 import com.sun.xml.internal.rngom.nc.NameClass;
aoqi@0 73 import com.sun.xml.internal.rngom.nc.NameClassBuilderImpl;
aoqi@0 74 import com.sun.xml.internal.rngom.parse.Context;
aoqi@0 75 import com.sun.xml.internal.rngom.parse.IllegalSchemaException;
aoqi@0 76 import com.sun.xml.internal.rngom.parse.Parseable;
aoqi@0 77 import com.sun.xml.internal.rngom.util.Localizer;
aoqi@0 78 import org.relaxng.datatype.Datatype;
aoqi@0 79 import org.relaxng.datatype.DatatypeBuilder;
aoqi@0 80 import org.relaxng.datatype.DatatypeException;
aoqi@0 81 import org.relaxng.datatype.DatatypeLibrary;
aoqi@0 82 import org.relaxng.datatype.DatatypeLibraryFactory;
aoqi@0 83 import org.relaxng.datatype.ValidationContext;
aoqi@0 84 import org.relaxng.datatype.helpers.DatatypeLibraryLoader;
aoqi@0 85 import org.xml.sax.ErrorHandler;
aoqi@0 86 import org.xml.sax.Locator;
aoqi@0 87 import org.xml.sax.SAXException;
aoqi@0 88 import org.xml.sax.SAXParseException;
aoqi@0 89
aoqi@0 90 public class SchemaBuilderImpl implements SchemaBuilder, ElementAnnotationBuilder, CommentList {
aoqi@0 91
aoqi@0 92 private final SchemaBuilderImpl parent;
aoqi@0 93 private boolean hadError = false;
aoqi@0 94 private final SchemaPatternBuilder pb;
aoqi@0 95 private final DatatypeLibraryFactory datatypeLibraryFactory;
aoqi@0 96 private final String inheritNs;
aoqi@0 97 private final ErrorHandler eh;
aoqi@0 98 private final OpenIncludes openIncludes;
aoqi@0 99 private final NameClassBuilder ncb = new NameClassBuilderImpl();
aoqi@0 100 static final Localizer localizer = new Localizer(SchemaBuilderImpl.class);
aoqi@0 101
aoqi@0 102 static class OpenIncludes {
aoqi@0 103
aoqi@0 104 final String uri;
aoqi@0 105 final OpenIncludes parent;
aoqi@0 106
aoqi@0 107 OpenIncludes(String uri, OpenIncludes parent) {
aoqi@0 108 this.uri = uri;
aoqi@0 109 this.parent = parent;
aoqi@0 110 }
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 public ParsedPattern expandPattern(ParsedPattern _pattern)
aoqi@0 114 throws BuildException, IllegalSchemaException {
aoqi@0 115 Pattern pattern = (Pattern) _pattern;
aoqi@0 116 if (!hadError) {
aoqi@0 117 try {
aoqi@0 118 pattern.checkRecursion(0);
aoqi@0 119 pattern = pattern.expand(pb);
aoqi@0 120 pattern.checkRestrictions(Pattern.START_CONTEXT, null, null);
aoqi@0 121 if (!hadError) {
aoqi@0 122 return pattern;
aoqi@0 123 }
aoqi@0 124 } catch (SAXParseException e) {
aoqi@0 125 error(e);
aoqi@0 126 } catch (SAXException e) {
aoqi@0 127 throw new BuildException(e);
aoqi@0 128 } catch (RestrictionViolationException e) {
aoqi@0 129 if (e.getName() != null) {
aoqi@0 130 error(e.getMessageId(), e.getName().toString(), e
aoqi@0 131 .getLocator());
aoqi@0 132 } else {
aoqi@0 133 error(e.getMessageId(), e.getLocator());
aoqi@0 134 }
aoqi@0 135 }
aoqi@0 136 }
aoqi@0 137 throw new IllegalSchemaException();
aoqi@0 138 }
aoqi@0 139
aoqi@0 140 /**
aoqi@0 141 *
aoqi@0 142 * @param eh Error handler to receive errors while building the schema.
aoqi@0 143 */
aoqi@0 144 public SchemaBuilderImpl(ErrorHandler eh) {
aoqi@0 145 this(eh,
aoqi@0 146 new CascadingDatatypeLibraryFactory(new DatatypeLibraryLoader(),
aoqi@0 147 new BuiltinDatatypeLibraryFactory(new DatatypeLibraryLoader())),
aoqi@0 148 new SchemaPatternBuilder());
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 /**
aoqi@0 152 *
aoqi@0 153 * @param eh Error handler to receive errors while building the schema.
aoqi@0 154 * @param datatypeLibraryFactory This is consulted to locate datatype
aoqi@0 155 * libraries.
aoqi@0 156 * @param pb Used to build patterns.
aoqi@0 157 */
aoqi@0 158 public SchemaBuilderImpl(ErrorHandler eh,
aoqi@0 159 DatatypeLibraryFactory datatypeLibraryFactory,
aoqi@0 160 SchemaPatternBuilder pb) {
aoqi@0 161 this.parent = null;
aoqi@0 162 this.eh = eh;
aoqi@0 163 this.datatypeLibraryFactory = datatypeLibraryFactory;
aoqi@0 164 this.pb = pb;
aoqi@0 165 this.inheritNs = "";
aoqi@0 166 this.openIncludes = null;
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 private SchemaBuilderImpl(String inheritNs,
aoqi@0 170 String uri,
aoqi@0 171 SchemaBuilderImpl parent) {
aoqi@0 172 this.parent = parent;
aoqi@0 173 this.eh = parent.eh;
aoqi@0 174 this.datatypeLibraryFactory = parent.datatypeLibraryFactory;
aoqi@0 175 this.pb = parent.pb;
aoqi@0 176 this.inheritNs = inheritNs;
aoqi@0 177 this.openIncludes = new OpenIncludes(uri, parent.openIncludes);
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 public NameClassBuilder getNameClassBuilder() {
aoqi@0 181 return ncb;
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 public ParsedPattern makeChoice(List patterns, Location loc, Annotations anno)
aoqi@0 185 throws BuildException {
aoqi@0 186 if (patterns.isEmpty()) {
aoqi@0 187 throw new IllegalArgumentException();
aoqi@0 188 }
aoqi@0 189 Pattern result = (Pattern) patterns.get(0);
aoqi@0 190 for (int i = 1; i < patterns.size(); i++) {
aoqi@0 191 result = pb.makeChoice(result, (Pattern) patterns.get(i));
aoqi@0 192 }
aoqi@0 193 return result;
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 public ParsedPattern makeInterleave(List patterns, Location loc, Annotations anno)
aoqi@0 197 throws BuildException {
aoqi@0 198 if (patterns.isEmpty()) {
aoqi@0 199 throw new IllegalArgumentException();
aoqi@0 200 }
aoqi@0 201 Pattern result = (Pattern) patterns.get(0);
aoqi@0 202 for (int i = 1; i < patterns.size(); i++) {
aoqi@0 203 result = pb.makeInterleave(result, (Pattern) patterns.get(i));
aoqi@0 204 }
aoqi@0 205 return result;
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 public ParsedPattern makeGroup(List patterns, Location loc, Annotations anno)
aoqi@0 209 throws BuildException {
aoqi@0 210 if (patterns.isEmpty()) {
aoqi@0 211 throw new IllegalArgumentException();
aoqi@0 212 }
aoqi@0 213 Pattern result = (Pattern) patterns.get(0);
aoqi@0 214 for (int i = 1; i < patterns.size(); i++) {
aoqi@0 215 result = pb.makeGroup(result, (Pattern) patterns.get(i));
aoqi@0 216 }
aoqi@0 217 return result;
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 public ParsedPattern makeOneOrMore(ParsedPattern p, Location loc, Annotations anno)
aoqi@0 221 throws BuildException {
aoqi@0 222 return pb.makeOneOrMore((Pattern) p);
aoqi@0 223 }
aoqi@0 224
aoqi@0 225 public ParsedPattern makeZeroOrMore(ParsedPattern p, Location loc, Annotations anno)
aoqi@0 226 throws BuildException {
aoqi@0 227 return pb.makeZeroOrMore((Pattern) p);
aoqi@0 228 }
aoqi@0 229
aoqi@0 230 public ParsedPattern makeOptional(ParsedPattern p, Location loc, Annotations anno)
aoqi@0 231 throws BuildException {
aoqi@0 232 return pb.makeOptional((Pattern) p);
aoqi@0 233 }
aoqi@0 234
aoqi@0 235 public ParsedPattern makeList(ParsedPattern p, Location loc, Annotations anno)
aoqi@0 236 throws BuildException {
aoqi@0 237 return pb.makeList((Pattern) p, (Locator) loc);
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 public ParsedPattern makeMixed(ParsedPattern p, Location loc, Annotations anno)
aoqi@0 241 throws BuildException {
aoqi@0 242 return pb.makeMixed((Pattern) p);
aoqi@0 243 }
aoqi@0 244
aoqi@0 245 public ParsedPattern makeEmpty(Location loc, Annotations anno) {
aoqi@0 246 return pb.makeEmpty();
aoqi@0 247 }
aoqi@0 248
aoqi@0 249 public ParsedPattern makeNotAllowed(Location loc, Annotations anno) {
aoqi@0 250 return pb.makeUnexpandedNotAllowed();
aoqi@0 251 }
aoqi@0 252
aoqi@0 253 public ParsedPattern makeText(Location loc, Annotations anno) {
aoqi@0 254 return pb.makeText();
aoqi@0 255 }
aoqi@0 256
aoqi@0 257 public ParsedPattern makeErrorPattern() {
aoqi@0 258 return pb.makeError();
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 // public ParsedNameClass makeErrorNameClass() {
aoqi@0 262 // return new ErrorNameClass();
aoqi@0 263 // }
aoqi@0 264 public ParsedPattern makeAttribute(ParsedNameClass nc, ParsedPattern p, Location loc, Annotations anno)
aoqi@0 265 throws BuildException {
aoqi@0 266 return pb.makeAttribute((NameClass) nc, (Pattern) p, (Locator) loc);
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 public ParsedPattern makeElement(ParsedNameClass nc, ParsedPattern p, Location loc, Annotations anno)
aoqi@0 270 throws BuildException {
aoqi@0 271 return pb.makeElement((NameClass) nc, (Pattern) p, (Locator) loc);
aoqi@0 272 }
aoqi@0 273
aoqi@0 274 private class DummyDataPatternBuilder implements DataPatternBuilder {
aoqi@0 275
aoqi@0 276 public void addParam(String name, String value, Context context, String ns, Location loc, Annotations anno)
aoqi@0 277 throws BuildException {
aoqi@0 278 }
aoqi@0 279
aoqi@0 280 public ParsedPattern makePattern(Location loc, Annotations anno)
aoqi@0 281 throws BuildException {
aoqi@0 282 return pb.makeError();
aoqi@0 283 }
aoqi@0 284
aoqi@0 285 public ParsedPattern makePattern(ParsedPattern except, Location loc, Annotations anno)
aoqi@0 286 throws BuildException {
aoqi@0 287 return pb.makeError();
aoqi@0 288 }
aoqi@0 289
aoqi@0 290 public void annotation(ParsedElementAnnotation ea) {
aoqi@0 291 }
aoqi@0 292 }
aoqi@0 293
aoqi@0 294 private static class ValidationContextImpl implements ValidationContext {
aoqi@0 295
aoqi@0 296 private ValidationContext vc;
aoqi@0 297 private String ns;
aoqi@0 298
aoqi@0 299 ValidationContextImpl(ValidationContext vc, String ns) {
aoqi@0 300 this.vc = vc;
aoqi@0 301 this.ns = ns.length() == 0 ? null : ns;
aoqi@0 302 }
aoqi@0 303
aoqi@0 304 public String resolveNamespacePrefix(String prefix) {
aoqi@0 305 return prefix.length() == 0 ? ns : vc.resolveNamespacePrefix(prefix);
aoqi@0 306 }
aoqi@0 307
aoqi@0 308 public String getBaseUri() {
aoqi@0 309 return vc.getBaseUri();
aoqi@0 310 }
aoqi@0 311
aoqi@0 312 public boolean isUnparsedEntity(String entityName) {
aoqi@0 313 return vc.isUnparsedEntity(entityName);
aoqi@0 314 }
aoqi@0 315
aoqi@0 316 public boolean isNotation(String notationName) {
aoqi@0 317 return vc.isNotation(notationName);
aoqi@0 318 }
aoqi@0 319 }
aoqi@0 320
aoqi@0 321 private class DataPatternBuilderImpl implements DataPatternBuilder {
aoqi@0 322
aoqi@0 323 private DatatypeBuilder dtb;
aoqi@0 324
aoqi@0 325 DataPatternBuilderImpl(DatatypeBuilder dtb) {
aoqi@0 326 this.dtb = dtb;
aoqi@0 327 }
aoqi@0 328
aoqi@0 329 public void addParam(String name, String value, Context context, String ns, Location loc, Annotations anno)
aoqi@0 330 throws BuildException {
aoqi@0 331 try {
aoqi@0 332 dtb.addParameter(name, value, new ValidationContextImpl(context, ns));
aoqi@0 333 } catch (DatatypeException e) {
aoqi@0 334 String detail = e.getMessage();
aoqi@0 335 int pos = e.getIndex();
aoqi@0 336 String displayedParam;
aoqi@0 337 if (pos == DatatypeException.UNKNOWN) {
aoqi@0 338 displayedParam = null;
aoqi@0 339 } else {
aoqi@0 340 displayedParam = displayParam(value, pos);
aoqi@0 341 }
aoqi@0 342 if (displayedParam != null) {
aoqi@0 343 if (detail != null) {
aoqi@0 344 error("invalid_param_detail_display", detail, displayedParam, (Locator) loc);
aoqi@0 345 } else {
aoqi@0 346 error("invalid_param_display", displayedParam, (Locator) loc);
aoqi@0 347 }
aoqi@0 348 } else if (detail != null) {
aoqi@0 349 error("invalid_param_detail", detail, (Locator) loc);
aoqi@0 350 } else {
aoqi@0 351 error("invalid_param", (Locator) loc);
aoqi@0 352 }
aoqi@0 353 }
aoqi@0 354 }
aoqi@0 355
aoqi@0 356 String displayParam(String value, int pos) {
aoqi@0 357 if (pos < 0) {
aoqi@0 358 pos = 0;
aoqi@0 359 } else if (pos > value.length()) {
aoqi@0 360 pos = value.length();
aoqi@0 361 }
aoqi@0 362 return localizer.message("display_param", value.substring(0, pos), value.substring(pos));
aoqi@0 363 }
aoqi@0 364
aoqi@0 365 public ParsedPattern makePattern(Location loc, Annotations anno)
aoqi@0 366 throws BuildException {
aoqi@0 367 try {
aoqi@0 368 return pb.makeData(dtb.createDatatype());
aoqi@0 369 } catch (DatatypeException e) {
aoqi@0 370 String detail = e.getMessage();
aoqi@0 371 if (detail != null) {
aoqi@0 372 error("invalid_params_detail", detail, (Locator) loc);
aoqi@0 373 } else {
aoqi@0 374 error("invalid_params", (Locator) loc);
aoqi@0 375 }
aoqi@0 376 return pb.makeError();
aoqi@0 377 }
aoqi@0 378 }
aoqi@0 379
aoqi@0 380 public ParsedPattern makePattern(ParsedPattern except, Location loc, Annotations anno)
aoqi@0 381 throws BuildException {
aoqi@0 382 try {
aoqi@0 383 return pb.makeDataExcept(dtb.createDatatype(), (Pattern) except, (Locator) loc);
aoqi@0 384 } catch (DatatypeException e) {
aoqi@0 385 String detail = e.getMessage();
aoqi@0 386 if (detail != null) {
aoqi@0 387 error("invalid_params_detail", detail, (Locator) loc);
aoqi@0 388 } else {
aoqi@0 389 error("invalid_params", (Locator) loc);
aoqi@0 390 }
aoqi@0 391 return pb.makeError();
aoqi@0 392 }
aoqi@0 393 }
aoqi@0 394
aoqi@0 395 public void annotation(ParsedElementAnnotation ea) {
aoqi@0 396 }
aoqi@0 397 }
aoqi@0 398
aoqi@0 399 public DataPatternBuilder makeDataPatternBuilder(String datatypeLibrary, String type, Location loc)
aoqi@0 400 throws BuildException {
aoqi@0 401 DatatypeLibrary dl = datatypeLibraryFactory.createDatatypeLibrary(datatypeLibrary);
aoqi@0 402 if (dl == null) {
aoqi@0 403 error("unrecognized_datatype_library", datatypeLibrary, (Locator) loc);
aoqi@0 404 } else {
aoqi@0 405 try {
aoqi@0 406 return new DataPatternBuilderImpl(dl.createDatatypeBuilder(type));
aoqi@0 407 } catch (DatatypeException e) {
aoqi@0 408 String detail = e.getMessage();
aoqi@0 409 if (detail != null) {
aoqi@0 410 error("unsupported_datatype_detail", datatypeLibrary, type, detail, (Locator) loc);
aoqi@0 411 } else {
aoqi@0 412 error("unrecognized_datatype", datatypeLibrary, type, (Locator) loc);
aoqi@0 413 }
aoqi@0 414 }
aoqi@0 415 }
aoqi@0 416 return new DummyDataPatternBuilder();
aoqi@0 417 }
aoqi@0 418
aoqi@0 419 public ParsedPattern makeValue(String datatypeLibrary, String type, String value, Context context, String ns,
aoqi@0 420 Location loc, Annotations anno) throws BuildException {
aoqi@0 421 DatatypeLibrary dl = datatypeLibraryFactory.createDatatypeLibrary(datatypeLibrary);
aoqi@0 422 if (dl == null) {
aoqi@0 423 error("unrecognized_datatype_library", datatypeLibrary, (Locator) loc);
aoqi@0 424 } else {
aoqi@0 425 try {
aoqi@0 426 DatatypeBuilder dtb = dl.createDatatypeBuilder(type);
aoqi@0 427 try {
aoqi@0 428 Datatype dt = dtb.createDatatype();
aoqi@0 429 Object obj = dt.createValue(value, new ValidationContextImpl(context, ns));
aoqi@0 430 if (obj != null) {
aoqi@0 431 return pb.makeValue(dt, obj);
aoqi@0 432 }
aoqi@0 433 error("invalid_value", value, (Locator) loc);
aoqi@0 434 } catch (DatatypeException e) {
aoqi@0 435 String detail = e.getMessage();
aoqi@0 436 if (detail != null) {
aoqi@0 437 error("datatype_requires_param_detail", detail, (Locator) loc);
aoqi@0 438 } else {
aoqi@0 439 error("datatype_requires_param", (Locator) loc);
aoqi@0 440 }
aoqi@0 441 }
aoqi@0 442 } catch (DatatypeException e) {
aoqi@0 443 error("unrecognized_datatype", datatypeLibrary, type, (Locator) loc);
aoqi@0 444 }
aoqi@0 445 }
aoqi@0 446 return pb.makeError();
aoqi@0 447 }
aoqi@0 448
aoqi@0 449 static class GrammarImpl implements Grammar, Div, IncludedGrammar {
aoqi@0 450
aoqi@0 451 private final SchemaBuilderImpl sb;
aoqi@0 452 private final Hashtable defines;
aoqi@0 453 private final RefPattern startRef;
aoqi@0 454 private final Scope parent;
aoqi@0 455
aoqi@0 456 private GrammarImpl(SchemaBuilderImpl sb, Scope parent) {
aoqi@0 457 this.sb = sb;
aoqi@0 458 this.parent = parent;
aoqi@0 459 this.defines = new Hashtable();
aoqi@0 460 this.startRef = new RefPattern(null);
aoqi@0 461 }
aoqi@0 462
aoqi@0 463 protected GrammarImpl(SchemaBuilderImpl sb, GrammarImpl g) {
aoqi@0 464 this.sb = sb;
aoqi@0 465 parent = g.parent;
aoqi@0 466 startRef = g.startRef;
aoqi@0 467 defines = g.defines;
aoqi@0 468 }
aoqi@0 469
aoqi@0 470 public ParsedPattern endGrammar(Location loc, Annotations anno) throws BuildException {
aoqi@0 471 for (Enumeration e = defines.keys();
aoqi@0 472 e.hasMoreElements();) {
aoqi@0 473 String name = (String) e.nextElement();
aoqi@0 474 RefPattern rp = (RefPattern) defines.get(name);
aoqi@0 475 if (rp.getPattern() == null) {
aoqi@0 476 sb.error("reference_to_undefined", name, rp.getRefLocator());
aoqi@0 477 rp.setPattern(sb.pb.makeError());
aoqi@0 478 }
aoqi@0 479 }
aoqi@0 480 Pattern start = startRef.getPattern();
aoqi@0 481 if (start == null) {
aoqi@0 482 sb.error("missing_start_element", (Locator) loc);
aoqi@0 483 start = sb.pb.makeError();
aoqi@0 484 }
aoqi@0 485 return start;
aoqi@0 486 }
aoqi@0 487
aoqi@0 488 public void endDiv(Location loc, Annotations anno) throws BuildException {
aoqi@0 489 // nothing to do
aoqi@0 490 }
aoqi@0 491
aoqi@0 492 public ParsedPattern endIncludedGrammar(Location loc, Annotations anno) throws BuildException {
aoqi@0 493 return null;
aoqi@0 494 }
aoqi@0 495
aoqi@0 496 public void define(String name, GrammarSection.Combine combine, ParsedPattern pattern, Location loc, Annotations anno)
aoqi@0 497 throws BuildException {
aoqi@0 498 define(lookup(name), combine, pattern, loc);
aoqi@0 499 }
aoqi@0 500
aoqi@0 501 private void define(RefPattern rp, GrammarSection.Combine combine, ParsedPattern pattern, Location loc)
aoqi@0 502 throws BuildException {
aoqi@0 503 switch (rp.getReplacementStatus()) {
aoqi@0 504 case RefPattern.REPLACEMENT_KEEP:
aoqi@0 505 if (combine == null) {
aoqi@0 506 if (rp.isCombineImplicit()) {
aoqi@0 507 if (rp.getName() == null) {
aoqi@0 508 sb.error("duplicate_start", (Locator) loc);
aoqi@0 509 } else {
aoqi@0 510 sb.error("duplicate_define", rp.getName(), (Locator) loc);
aoqi@0 511 }
aoqi@0 512 } else {
aoqi@0 513 rp.setCombineImplicit();
aoqi@0 514 }
aoqi@0 515 } else {
aoqi@0 516 byte combineType = (combine == COMBINE_CHOICE ? RefPattern.COMBINE_CHOICE : RefPattern.COMBINE_INTERLEAVE);
aoqi@0 517 if (rp.getCombineType() != RefPattern.COMBINE_NONE
aoqi@0 518 && rp.getCombineType() != combineType) {
aoqi@0 519 if (rp.getName() == null) {
aoqi@0 520 sb.error("conflict_combine_start", (Locator) loc);
aoqi@0 521 } else {
aoqi@0 522 sb.error("conflict_combine_define", rp.getName(), (Locator) loc);
aoqi@0 523 }
aoqi@0 524 }
aoqi@0 525 rp.setCombineType(combineType);
aoqi@0 526 }
aoqi@0 527 Pattern p = (Pattern) pattern;
aoqi@0 528 if (rp.getPattern() == null) {
aoqi@0 529 rp.setPattern(p);
aoqi@0 530 } else if (rp.getCombineType() == RefPattern.COMBINE_INTERLEAVE) {
aoqi@0 531 rp.setPattern(sb.pb.makeInterleave(rp.getPattern(), p));
aoqi@0 532 } else {
aoqi@0 533 rp.setPattern(sb.pb.makeChoice(rp.getPattern(), p));
aoqi@0 534 }
aoqi@0 535 break;
aoqi@0 536 case RefPattern.REPLACEMENT_REQUIRE:
aoqi@0 537 rp.setReplacementStatus(RefPattern.REPLACEMENT_IGNORE);
aoqi@0 538 break;
aoqi@0 539 case RefPattern.REPLACEMENT_IGNORE:
aoqi@0 540 break;
aoqi@0 541 }
aoqi@0 542 }
aoqi@0 543
aoqi@0 544 public void topLevelAnnotation(ParsedElementAnnotation ea) throws BuildException {
aoqi@0 545 }
aoqi@0 546
aoqi@0 547 public void topLevelComment(CommentList comments) throws BuildException {
aoqi@0 548 }
aoqi@0 549
aoqi@0 550 private RefPattern lookup(String name) {
aoqi@0 551 if (name == START) {
aoqi@0 552 return startRef;
aoqi@0 553 }
aoqi@0 554 return lookup1(name);
aoqi@0 555 }
aoqi@0 556
aoqi@0 557 private RefPattern lookup1(String name) {
aoqi@0 558 RefPattern p = (RefPattern) defines.get(name);
aoqi@0 559 if (p == null) {
aoqi@0 560 p = new RefPattern(name);
aoqi@0 561 defines.put(name, p);
aoqi@0 562 }
aoqi@0 563 return p;
aoqi@0 564 }
aoqi@0 565
aoqi@0 566 public ParsedPattern makeRef(String name, Location loc, Annotations anno) throws BuildException {
aoqi@0 567 RefPattern p = lookup1(name);
aoqi@0 568 if (p.getRefLocator() == null && loc != null) {
aoqi@0 569 p.setRefLocator((Locator) loc);
aoqi@0 570 }
aoqi@0 571 return p;
aoqi@0 572 }
aoqi@0 573
aoqi@0 574 public ParsedPattern makeParentRef(String name, Location loc, Annotations anno) throws BuildException {
aoqi@0 575 // TODO: do this check by the caller
aoqi@0 576 if (parent == null) {
aoqi@0 577 sb.error("parent_ref_outside_grammar", (Locator) loc);
aoqi@0 578 return sb.makeErrorPattern();
aoqi@0 579 }
aoqi@0 580 return parent.makeRef(name, loc, anno);
aoqi@0 581 }
aoqi@0 582
aoqi@0 583 public Div makeDiv() {
aoqi@0 584 return this;
aoqi@0 585 }
aoqi@0 586
aoqi@0 587 public Include makeInclude() {
aoqi@0 588 return new IncludeImpl(sb, this);
aoqi@0 589 }
aoqi@0 590 }
aoqi@0 591
aoqi@0 592 static class Override {
aoqi@0 593
aoqi@0 594 Override(RefPattern prp, Override next) {
aoqi@0 595 this.prp = prp;
aoqi@0 596 this.next = next;
aoqi@0 597 }
aoqi@0 598 RefPattern prp;
aoqi@0 599 Override next;
aoqi@0 600 byte replacementStatus;
aoqi@0 601 }
aoqi@0 602
aoqi@0 603 private static class IncludeImpl implements Include, Div {
aoqi@0 604
aoqi@0 605 private SchemaBuilderImpl sb;
aoqi@0 606 private Override overrides;
aoqi@0 607 private GrammarImpl grammar;
aoqi@0 608
aoqi@0 609 private IncludeImpl(SchemaBuilderImpl sb, GrammarImpl grammar) {
aoqi@0 610 this.sb = sb;
aoqi@0 611 this.grammar = grammar;
aoqi@0 612 }
aoqi@0 613
aoqi@0 614 public void define(String name, GrammarSection.Combine combine, ParsedPattern pattern, Location loc, Annotations anno)
aoqi@0 615 throws BuildException {
aoqi@0 616 RefPattern rp = grammar.lookup(name);
aoqi@0 617 overrides = new Override(rp, overrides);
aoqi@0 618 grammar.define(rp, combine, pattern, loc);
aoqi@0 619 }
aoqi@0 620
aoqi@0 621 public void endDiv(Location loc, Annotations anno) throws BuildException {
aoqi@0 622 // nothing to do
aoqi@0 623 }
aoqi@0 624
aoqi@0 625 public void topLevelAnnotation(ParsedElementAnnotation ea) throws BuildException {
aoqi@0 626 // nothing to do
aoqi@0 627 }
aoqi@0 628
aoqi@0 629 public void topLevelComment(CommentList comments) throws BuildException {
aoqi@0 630 }
aoqi@0 631
aoqi@0 632 public Div makeDiv() {
aoqi@0 633 return this;
aoqi@0 634 }
aoqi@0 635
aoqi@0 636 public void endInclude(Parseable current, String uri, String ns,
aoqi@0 637 Location loc, Annotations anno) throws BuildException {
aoqi@0 638 for (OpenIncludes inc = sb.openIncludes;
aoqi@0 639 inc != null;
aoqi@0 640 inc = inc.parent) {
aoqi@0 641 if (inc.uri.equals(uri)) {
aoqi@0 642 sb.error("recursive_include", uri, (Locator) loc);
aoqi@0 643 return;
aoqi@0 644 }
aoqi@0 645 }
aoqi@0 646
aoqi@0 647 for (Override o = overrides; o != null; o = o.next) {
aoqi@0 648 o.replacementStatus = o.prp.getReplacementStatus();
aoqi@0 649 o.prp.setReplacementStatus(RefPattern.REPLACEMENT_REQUIRE);
aoqi@0 650 }
aoqi@0 651 try {
aoqi@0 652 SchemaBuilderImpl isb = new SchemaBuilderImpl(ns, uri, sb);
aoqi@0 653 current.parseInclude(uri, isb, new GrammarImpl(isb, grammar), ns);
aoqi@0 654 for (Override o = overrides; o != null; o = o.next) {
aoqi@0 655 if (o.prp.getReplacementStatus() == RefPattern.REPLACEMENT_REQUIRE) {
aoqi@0 656 if (o.prp.getName() == null) {
aoqi@0 657 sb.error("missing_start_replacement", (Locator) loc);
aoqi@0 658 } else {
aoqi@0 659 sb.error("missing_define_replacement", o.prp.getName(), (Locator) loc);
aoqi@0 660 }
aoqi@0 661 }
aoqi@0 662 }
aoqi@0 663 } catch (IllegalSchemaException e) {
aoqi@0 664 sb.noteError();
aoqi@0 665 } finally {
aoqi@0 666 for (Override o = overrides; o != null; o = o.next) {
aoqi@0 667 o.prp.setReplacementStatus(o.replacementStatus);
aoqi@0 668 }
aoqi@0 669 }
aoqi@0 670 }
aoqi@0 671
aoqi@0 672 public Include makeInclude() {
aoqi@0 673 return null;
aoqi@0 674 }
aoqi@0 675 }
aoqi@0 676
aoqi@0 677 public Grammar makeGrammar(Scope parent) {
aoqi@0 678 return new GrammarImpl(this, parent);
aoqi@0 679 }
aoqi@0 680
aoqi@0 681 public ParsedPattern annotate(ParsedPattern p, Annotations anno) throws BuildException {
aoqi@0 682 return p;
aoqi@0 683 }
aoqi@0 684
aoqi@0 685 public ParsedPattern annotateAfter(ParsedPattern p, ParsedElementAnnotation e) throws BuildException {
aoqi@0 686 return p;
aoqi@0 687 }
aoqi@0 688
aoqi@0 689 public ParsedPattern commentAfter(ParsedPattern p, CommentList comments) throws BuildException {
aoqi@0 690 return p;
aoqi@0 691 }
aoqi@0 692
aoqi@0 693 public ParsedPattern makeExternalRef(Parseable current, String uri, String ns, Scope scope,
aoqi@0 694 Location loc, Annotations anno)
aoqi@0 695 throws BuildException {
aoqi@0 696 for (OpenIncludes inc = openIncludes;
aoqi@0 697 inc != null;
aoqi@0 698 inc = inc.parent) {
aoqi@0 699 if (inc.uri.equals(uri)) {
aoqi@0 700 error("recursive_include", uri, (Locator) loc);
aoqi@0 701 return pb.makeError();
aoqi@0 702 }
aoqi@0 703 }
aoqi@0 704 try {
aoqi@0 705 return current.parseExternal(uri, new SchemaBuilderImpl(ns, uri, this), scope, ns);
aoqi@0 706 } catch (IllegalSchemaException e) {
aoqi@0 707 noteError();
aoqi@0 708 return pb.makeError();
aoqi@0 709 }
aoqi@0 710 }
aoqi@0 711
aoqi@0 712 public Location makeLocation(String systemId, int lineNumber, int columnNumber) {
aoqi@0 713 return new LocatorImpl(systemId, lineNumber, columnNumber);
aoqi@0 714 }
aoqi@0 715
aoqi@0 716 public Annotations makeAnnotations(CommentList comments, Context context) {
aoqi@0 717 return this;
aoqi@0 718 }
aoqi@0 719
aoqi@0 720 public ElementAnnotationBuilder makeElementAnnotationBuilder(String ns, String localName, String prefix,
aoqi@0 721 Location loc, CommentList comments, Context context) {
aoqi@0 722 return this;
aoqi@0 723 }
aoqi@0 724
aoqi@0 725 public CommentList makeCommentList() {
aoqi@0 726 return this;
aoqi@0 727 }
aoqi@0 728
aoqi@0 729 public void addComment(String value, Location loc) throws BuildException {
aoqi@0 730 }
aoqi@0 731
aoqi@0 732 public void addAttribute(String ns, String localName, String prefix, String value, Location loc) {
aoqi@0 733 // nothing needed
aoqi@0 734 }
aoqi@0 735
aoqi@0 736 public void addElement(ParsedElementAnnotation ea) {
aoqi@0 737 // nothing needed
aoqi@0 738 }
aoqi@0 739
aoqi@0 740 public void addComment(CommentList comments) throws BuildException {
aoqi@0 741 // nothing needed
aoqi@0 742 }
aoqi@0 743
aoqi@0 744 public void addLeadingComment(CommentList comments) throws BuildException {
aoqi@0 745 // nothing needed
aoqi@0 746 }
aoqi@0 747
aoqi@0 748 public ParsedElementAnnotation makeElementAnnotation() {
aoqi@0 749 return null;
aoqi@0 750 }
aoqi@0 751
aoqi@0 752 public void addText(String value, Location loc, CommentList comments) throws BuildException {
aoqi@0 753 }
aoqi@0 754
aoqi@0 755 public boolean usesComments() {
aoqi@0 756 return false;
aoqi@0 757 }
aoqi@0 758
aoqi@0 759 private void error(SAXParseException message) throws BuildException {
aoqi@0 760 noteError();
aoqi@0 761 try {
aoqi@0 762 if (eh != null) {
aoqi@0 763 eh.error(message);
aoqi@0 764 }
aoqi@0 765 } catch (SAXException e) {
aoqi@0 766 throw new BuildException(e);
aoqi@0 767 }
aoqi@0 768 }
aoqi@0 769
aoqi@0 770 private void error(String key, Locator loc) throws BuildException {
aoqi@0 771 error(new SAXParseException(localizer.message(key), loc));
aoqi@0 772 }
aoqi@0 773
aoqi@0 774 private void error(String key, String arg, Locator loc) throws BuildException {
aoqi@0 775 error(new SAXParseException(localizer.message(key, arg), loc));
aoqi@0 776 }
aoqi@0 777
aoqi@0 778 private void error(String key, String arg1, String arg2, Locator loc) throws BuildException {
aoqi@0 779 error(new SAXParseException(localizer.message(key, arg1, arg2), loc));
aoqi@0 780 }
aoqi@0 781
aoqi@0 782 private void error(String key, String arg1, String arg2, String arg3, Locator loc) throws BuildException {
aoqi@0 783 error(new SAXParseException(localizer.message(key, new Object[]{arg1, arg2, arg3}), loc));
aoqi@0 784 }
aoqi@0 785
aoqi@0 786 private void noteError() {
aoqi@0 787 if (!hadError && parent != null) {
aoqi@0 788 parent.noteError();
aoqi@0 789 }
aoqi@0 790 hadError = true;
aoqi@0 791 }
aoqi@0 792 }

mercurial