1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/rngom/binary/SchemaBuilderImpl.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,790 @@ 1.4 +/* 1.5 + * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 +/* 1.29 + * Copyright (C) 2004-2011 1.30 + * 1.31 + * Permission is hereby granted, free of charge, to any person obtaining a copy 1.32 + * of this software and associated documentation files (the "Software"), to deal 1.33 + * in the Software without restriction, including without limitation the rights 1.34 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1.35 + * copies of the Software, and to permit persons to whom the Software is 1.36 + * furnished to do so, subject to the following conditions: 1.37 + * 1.38 + * The above copyright notice and this permission notice shall be included in 1.39 + * all copies or substantial portions of the Software. 1.40 + * 1.41 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1.42 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1.43 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1.44 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1.45 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 1.46 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 1.47 + * THE SOFTWARE. 1.48 + */ 1.49 +package com.sun.xml.internal.rngom.binary; 1.50 + 1.51 +import java.util.Enumeration; 1.52 +import java.util.Hashtable; 1.53 +import java.util.List; 1.54 + 1.55 +import com.sun.xml.internal.rngom.ast.builder.Annotations; 1.56 +import com.sun.xml.internal.rngom.ast.builder.BuildException; 1.57 +import com.sun.xml.internal.rngom.ast.builder.CommentList; 1.58 +import com.sun.xml.internal.rngom.ast.builder.DataPatternBuilder; 1.59 +import com.sun.xml.internal.rngom.ast.builder.Div; 1.60 +import com.sun.xml.internal.rngom.ast.builder.ElementAnnotationBuilder; 1.61 +import com.sun.xml.internal.rngom.ast.builder.Grammar; 1.62 +import com.sun.xml.internal.rngom.ast.builder.GrammarSection; 1.63 +import com.sun.xml.internal.rngom.ast.builder.Include; 1.64 +import com.sun.xml.internal.rngom.ast.builder.IncludedGrammar; 1.65 +import com.sun.xml.internal.rngom.ast.builder.NameClassBuilder; 1.66 +import com.sun.xml.internal.rngom.ast.builder.SchemaBuilder; 1.67 +import com.sun.xml.internal.rngom.ast.builder.Scope; 1.68 +import com.sun.xml.internal.rngom.ast.om.Location; 1.69 +import com.sun.xml.internal.rngom.ast.om.ParsedElementAnnotation; 1.70 +import com.sun.xml.internal.rngom.ast.om.ParsedNameClass; 1.71 +import com.sun.xml.internal.rngom.ast.om.ParsedPattern; 1.72 +import com.sun.xml.internal.rngom.ast.util.LocatorImpl; 1.73 +import com.sun.xml.internal.rngom.dt.builtin.BuiltinDatatypeLibraryFactory; 1.74 +import com.sun.xml.internal.rngom.dt.CascadingDatatypeLibraryFactory; 1.75 +import com.sun.xml.internal.rngom.nc.NameClass; 1.76 +import com.sun.xml.internal.rngom.nc.NameClassBuilderImpl; 1.77 +import com.sun.xml.internal.rngom.parse.Context; 1.78 +import com.sun.xml.internal.rngom.parse.IllegalSchemaException; 1.79 +import com.sun.xml.internal.rngom.parse.Parseable; 1.80 +import com.sun.xml.internal.rngom.util.Localizer; 1.81 +import org.relaxng.datatype.Datatype; 1.82 +import org.relaxng.datatype.DatatypeBuilder; 1.83 +import org.relaxng.datatype.DatatypeException; 1.84 +import org.relaxng.datatype.DatatypeLibrary; 1.85 +import org.relaxng.datatype.DatatypeLibraryFactory; 1.86 +import org.relaxng.datatype.ValidationContext; 1.87 +import org.relaxng.datatype.helpers.DatatypeLibraryLoader; 1.88 +import org.xml.sax.ErrorHandler; 1.89 +import org.xml.sax.Locator; 1.90 +import org.xml.sax.SAXException; 1.91 +import org.xml.sax.SAXParseException; 1.92 + 1.93 +public class SchemaBuilderImpl implements SchemaBuilder, ElementAnnotationBuilder, CommentList { 1.94 + private final SchemaBuilderImpl parent; 1.95 + private boolean hadError = false; 1.96 + private final SchemaPatternBuilder pb; 1.97 + private final DatatypeLibraryFactory datatypeLibraryFactory; 1.98 + private final String inheritNs; 1.99 + private final ErrorHandler eh; 1.100 + private final OpenIncludes openIncludes; 1.101 + private final NameClassBuilder ncb =new NameClassBuilderImpl(); 1.102 + static final Localizer localizer = new Localizer(SchemaBuilderImpl.class); 1.103 + 1.104 + static class OpenIncludes { 1.105 + final String uri; 1.106 + final OpenIncludes parent; 1.107 + 1.108 + OpenIncludes(String uri, OpenIncludes parent) { 1.109 + this.uri = uri; 1.110 + this.parent = parent; 1.111 + } 1.112 + } 1.113 + 1.114 + public ParsedPattern expandPattern(ParsedPattern _pattern) 1.115 + throws BuildException, IllegalSchemaException { 1.116 + Pattern pattern = (Pattern) _pattern; 1.117 + if (!hadError) { 1.118 + try { 1.119 + pattern.checkRecursion(0); 1.120 + pattern = pattern.expand(pb); 1.121 + pattern.checkRestrictions(Pattern.START_CONTEXT, null, null); 1.122 + if (!hadError) return pattern; 1.123 + } catch (SAXParseException e) { 1.124 + error(e); 1.125 + } catch (SAXException e) { 1.126 + throw new BuildException(e); 1.127 + } catch (RestrictionViolationException e) { 1.128 + if (e.getName() != null) 1.129 + error(e.getMessageId(), e.getName().toString(), e 1.130 + .getLocator()); 1.131 + else 1.132 + error(e.getMessageId(), e.getLocator()); 1.133 + } 1.134 + } 1.135 + throw new IllegalSchemaException(); 1.136 + } 1.137 + 1.138 + /** 1.139 + * 1.140 + * @param eh 1.141 + * Error handler to receive errors while building the schema. 1.142 + */ 1.143 + public SchemaBuilderImpl(ErrorHandler eh) { 1.144 + this(eh, 1.145 + new CascadingDatatypeLibraryFactory(new DatatypeLibraryLoader(), 1.146 + new BuiltinDatatypeLibraryFactory(new DatatypeLibraryLoader())), 1.147 + new SchemaPatternBuilder()); 1.148 + } 1.149 + 1.150 + /** 1.151 + * 1.152 + * @param eh 1.153 + * Error handler to receive errors while building the schema. 1.154 + * @param datatypeLibraryFactory 1.155 + * This is consulted to locate datatype libraries. 1.156 + * @param pb 1.157 + * Used to build patterns. 1.158 + */ 1.159 + public SchemaBuilderImpl(ErrorHandler eh, 1.160 + DatatypeLibraryFactory datatypeLibraryFactory, 1.161 + SchemaPatternBuilder pb) { 1.162 + this.parent = null; 1.163 + this.eh = eh; 1.164 + this.datatypeLibraryFactory = datatypeLibraryFactory; 1.165 + this.pb = pb; 1.166 + this.inheritNs = ""; 1.167 + this.openIncludes = null; 1.168 + } 1.169 + 1.170 + private SchemaBuilderImpl(String inheritNs, 1.171 + String uri, 1.172 + SchemaBuilderImpl parent) { 1.173 + this.parent = parent; 1.174 + this.eh = parent.eh; 1.175 + this.datatypeLibraryFactory = parent.datatypeLibraryFactory; 1.176 + this.pb = parent.pb; 1.177 + this.inheritNs = inheritNs; 1.178 + this.openIncludes = new OpenIncludes(uri, parent.openIncludes); 1.179 + } 1.180 + 1.181 + public NameClassBuilder getNameClassBuilder() { 1.182 + return ncb; 1.183 + } 1.184 + 1.185 + public ParsedPattern makeChoice(List patterns, Location loc, Annotations anno) 1.186 + throws BuildException { 1.187 + if (patterns.isEmpty()) 1.188 + throw new IllegalArgumentException(); 1.189 + Pattern result = (Pattern)patterns.get(0); 1.190 + for (int i = 1; i < patterns.size(); i++) 1.191 + result = pb.makeChoice(result, (Pattern)patterns.get(i)); 1.192 + return result; 1.193 + } 1.194 + 1.195 + public ParsedPattern makeInterleave(List patterns, Location loc, Annotations anno) 1.196 + throws BuildException { 1.197 + if (patterns.isEmpty()) 1.198 + throw new IllegalArgumentException(); 1.199 + Pattern result = (Pattern)patterns.get(0); 1.200 + for (int i = 1; i < patterns.size(); i++) 1.201 + result = pb.makeInterleave(result, (Pattern)patterns.get(i)); 1.202 + return result; 1.203 + } 1.204 + 1.205 + public ParsedPattern makeGroup(List patterns, Location loc, Annotations anno) 1.206 + throws BuildException { 1.207 + if (patterns.isEmpty()) 1.208 + throw new IllegalArgumentException(); 1.209 + Pattern result = (Pattern)patterns.get(0); 1.210 + for (int i = 1; i < patterns.size(); i++) 1.211 + result = pb.makeGroup(result, (Pattern)patterns.get(i)); 1.212 + return result; 1.213 + } 1.214 + 1.215 + public ParsedPattern makeOneOrMore(ParsedPattern p, Location loc, Annotations anno) 1.216 + throws BuildException { 1.217 + return pb.makeOneOrMore((Pattern)p); 1.218 + } 1.219 + 1.220 + public ParsedPattern makeZeroOrMore(ParsedPattern p, Location loc, Annotations anno) 1.221 + throws BuildException { 1.222 + return pb.makeZeroOrMore((Pattern)p); 1.223 + } 1.224 + 1.225 + public ParsedPattern makeOptional(ParsedPattern p, Location loc, Annotations anno) 1.226 + throws BuildException { 1.227 + return pb.makeOptional((Pattern)p); 1.228 + } 1.229 + 1.230 + public ParsedPattern makeList(ParsedPattern p, Location loc, Annotations anno) 1.231 + throws BuildException { 1.232 + return pb.makeList((Pattern)p, (Locator)loc); 1.233 + } 1.234 + 1.235 + public ParsedPattern makeMixed(ParsedPattern p, Location loc, Annotations anno) 1.236 + throws BuildException { 1.237 + return pb.makeMixed((Pattern)p); 1.238 + } 1.239 + 1.240 + public ParsedPattern makeEmpty(Location loc, Annotations anno) { 1.241 + return pb.makeEmpty(); 1.242 + } 1.243 + 1.244 + public ParsedPattern makeNotAllowed(Location loc, Annotations anno) { 1.245 + return pb.makeUnexpandedNotAllowed(); 1.246 + } 1.247 + 1.248 + public ParsedPattern makeText(Location loc, Annotations anno) { 1.249 + return pb.makeText(); 1.250 + } 1.251 + 1.252 + public ParsedPattern makeErrorPattern() { 1.253 + return pb.makeError(); 1.254 + } 1.255 + 1.256 +// public ParsedNameClass makeErrorNameClass() { 1.257 +// return new ErrorNameClass(); 1.258 +// } 1.259 + 1.260 + public ParsedPattern makeAttribute(ParsedNameClass nc, ParsedPattern p, Location loc, Annotations anno) 1.261 + throws BuildException { 1.262 + return pb.makeAttribute((NameClass)nc, (Pattern)p, (Locator)loc); 1.263 + } 1.264 + 1.265 + public ParsedPattern makeElement(ParsedNameClass nc, ParsedPattern p, Location loc, Annotations anno) 1.266 + throws BuildException { 1.267 + return pb.makeElement((NameClass)nc, (Pattern)p, (Locator)loc); 1.268 + } 1.269 + 1.270 + private class DummyDataPatternBuilder implements DataPatternBuilder { 1.271 + public void addParam(String name, String value, Context context, String ns, Location loc, Annotations anno) 1.272 + throws BuildException { 1.273 + } 1.274 + 1.275 + public ParsedPattern makePattern(Location loc, Annotations anno) 1.276 + throws BuildException { 1.277 + return pb.makeError(); 1.278 + } 1.279 + 1.280 + public ParsedPattern makePattern(ParsedPattern except, Location loc, Annotations anno) 1.281 + throws BuildException { 1.282 + return pb.makeError(); 1.283 + } 1.284 + 1.285 + public void annotation(ParsedElementAnnotation ea) { 1.286 + } 1.287 + } 1.288 + 1.289 + private class ValidationContextImpl implements ValidationContext { 1.290 + private ValidationContext vc; 1.291 + private String ns; 1.292 + 1.293 + ValidationContextImpl(ValidationContext vc, String ns) { 1.294 + this.vc = vc; 1.295 + this.ns = ns.length() == 0 ? null : ns; 1.296 + } 1.297 + 1.298 + public String resolveNamespacePrefix(String prefix) { 1.299 + return prefix.length() == 0 ? ns : vc.resolveNamespacePrefix(prefix); 1.300 + } 1.301 + 1.302 + public String getBaseUri() { 1.303 + return vc.getBaseUri(); 1.304 + } 1.305 + 1.306 + public boolean isUnparsedEntity(String entityName) { 1.307 + return vc.isUnparsedEntity(entityName); 1.308 + } 1.309 + 1.310 + public boolean isNotation(String notationName) { 1.311 + return vc.isNotation(notationName); 1.312 + } 1.313 + } 1.314 + 1.315 + private class DataPatternBuilderImpl implements DataPatternBuilder { 1.316 + private DatatypeBuilder dtb; 1.317 + DataPatternBuilderImpl(DatatypeBuilder dtb) { 1.318 + this.dtb = dtb; 1.319 + } 1.320 + 1.321 + public void addParam(String name, String value, Context context, String ns, Location loc, Annotations anno) 1.322 + throws BuildException { 1.323 + try { 1.324 + dtb.addParameter(name, value, new ValidationContextImpl(context, ns)); 1.325 + } 1.326 + catch (DatatypeException e) { 1.327 + String detail = e.getMessage(); 1.328 + int pos = e.getIndex(); 1.329 + String displayedParam; 1.330 + if (pos == DatatypeException.UNKNOWN) 1.331 + displayedParam = null; 1.332 + else 1.333 + displayedParam = displayParam(value, pos); 1.334 + if (displayedParam != null) { 1.335 + if (detail != null) 1.336 + error("invalid_param_detail_display", detail, displayedParam, (Locator)loc); 1.337 + else 1.338 + error("invalid_param_display", displayedParam, (Locator)loc); 1.339 + } 1.340 + else if (detail != null) 1.341 + error("invalid_param_detail", detail, (Locator)loc); 1.342 + else 1.343 + error("invalid_param", (Locator)loc); 1.344 + } 1.345 + } 1.346 + 1.347 + String displayParam(String value, int pos) { 1.348 + if (pos < 0) 1.349 + pos = 0; 1.350 + else if (pos > value.length()) 1.351 + pos = value.length(); 1.352 + return localizer.message("display_param", value.substring(0, pos), value.substring(pos)); 1.353 + } 1.354 + 1.355 + public ParsedPattern makePattern(Location loc, Annotations anno) 1.356 + throws BuildException { 1.357 + try { 1.358 + return pb.makeData(dtb.createDatatype()); 1.359 + } 1.360 + catch (DatatypeException e) { 1.361 + String detail = e.getMessage(); 1.362 + if (detail != null) 1.363 + error("invalid_params_detail", detail, (Locator)loc); 1.364 + else 1.365 + error("invalid_params", (Locator)loc); 1.366 + return pb.makeError(); 1.367 + } 1.368 + } 1.369 + 1.370 + public ParsedPattern makePattern(ParsedPattern except, Location loc, Annotations anno) 1.371 + throws BuildException { 1.372 + try { 1.373 + return pb.makeDataExcept(dtb.createDatatype(), (Pattern)except, (Locator)loc); 1.374 + } 1.375 + catch (DatatypeException e) { 1.376 + String detail = e.getMessage(); 1.377 + if (detail != null) 1.378 + error("invalid_params_detail", detail, (Locator)loc); 1.379 + else 1.380 + error("invalid_params", (Locator)loc); 1.381 + return pb.makeError(); 1.382 + } 1.383 + } 1.384 + 1.385 + public void annotation(ParsedElementAnnotation ea) { 1.386 + } 1.387 + } 1.388 + 1.389 + public DataPatternBuilder makeDataPatternBuilder(String datatypeLibrary, String type, Location loc) 1.390 + throws BuildException { 1.391 + DatatypeLibrary dl = datatypeLibraryFactory.createDatatypeLibrary(datatypeLibrary); 1.392 + if (dl == null) 1.393 + error("unrecognized_datatype_library", datatypeLibrary, (Locator)loc); 1.394 + else { 1.395 + try { 1.396 + return new DataPatternBuilderImpl(dl.createDatatypeBuilder(type)); 1.397 + } 1.398 + catch (DatatypeException e) { 1.399 + String detail = e.getMessage(); 1.400 + if (detail != null) 1.401 + error("unsupported_datatype_detail", datatypeLibrary, type, detail, (Locator)loc); 1.402 + else 1.403 + error("unrecognized_datatype", datatypeLibrary, type, (Locator)loc); 1.404 + } 1.405 + } 1.406 + return new DummyDataPatternBuilder(); 1.407 + } 1.408 + 1.409 + public ParsedPattern makeValue(String datatypeLibrary, String type, String value, Context context, String ns, 1.410 + Location loc, Annotations anno) throws BuildException { 1.411 + DatatypeLibrary dl = datatypeLibraryFactory.createDatatypeLibrary(datatypeLibrary); 1.412 + if (dl == null) 1.413 + error("unrecognized_datatype_library", datatypeLibrary, (Locator)loc); 1.414 + else { 1.415 + try { 1.416 + DatatypeBuilder dtb = dl.createDatatypeBuilder(type); 1.417 + try { 1.418 + Datatype dt = dtb.createDatatype(); 1.419 + Object obj = dt.createValue(value, new ValidationContextImpl(context, ns)); 1.420 + if (obj != null) 1.421 + return pb.makeValue(dt, obj); 1.422 + error("invalid_value", value, (Locator)loc); 1.423 + } 1.424 + catch (DatatypeException e) { 1.425 + String detail = e.getMessage(); 1.426 + if (detail != null) 1.427 + error("datatype_requires_param_detail", detail, (Locator)loc); 1.428 + else 1.429 + error("datatype_requires_param", (Locator)loc); 1.430 + } 1.431 + } 1.432 + catch (DatatypeException e) { 1.433 + error("unrecognized_datatype", datatypeLibrary, type, (Locator)loc); 1.434 + } 1.435 + } 1.436 + return pb.makeError(); 1.437 + } 1.438 + 1.439 + static class GrammarImpl implements Grammar, Div, IncludedGrammar { 1.440 + private final SchemaBuilderImpl sb; 1.441 + private final Hashtable defines; 1.442 + private final RefPattern startRef; 1.443 + private final Scope parent; 1.444 + 1.445 + private GrammarImpl(SchemaBuilderImpl sb, Scope parent) { 1.446 + this.sb = sb; 1.447 + this.parent = parent; 1.448 + this.defines = new Hashtable(); 1.449 + this.startRef = new RefPattern(null); 1.450 + } 1.451 + 1.452 + protected GrammarImpl(SchemaBuilderImpl sb, GrammarImpl g) { 1.453 + this.sb = sb; 1.454 + parent = g.parent; 1.455 + startRef = g.startRef; 1.456 + defines = g.defines; 1.457 + } 1.458 + 1.459 + public ParsedPattern endGrammar(Location loc, Annotations anno) throws BuildException { 1.460 + for (Enumeration e = defines.keys(); 1.461 + e.hasMoreElements();) { 1.462 + String name = (String)e.nextElement(); 1.463 + RefPattern rp = (RefPattern)defines.get(name); 1.464 + if (rp.getPattern() == null) { 1.465 + sb.error("reference_to_undefined", name, rp.getRefLocator()); 1.466 + rp.setPattern(sb.pb.makeError()); 1.467 + } 1.468 + } 1.469 + Pattern start = startRef.getPattern(); 1.470 + if (start == null) { 1.471 + sb.error("missing_start_element", (Locator)loc); 1.472 + start = sb.pb.makeError(); 1.473 + } 1.474 + return start; 1.475 + } 1.476 + 1.477 + public void endDiv(Location loc, Annotations anno) throws BuildException { 1.478 + // nothing to do 1.479 + } 1.480 + 1.481 + public ParsedPattern endIncludedGrammar(Location loc, Annotations anno) throws BuildException { 1.482 + return null; 1.483 + } 1.484 + 1.485 + public void define(String name, GrammarSection.Combine combine, ParsedPattern pattern, Location loc, Annotations anno) 1.486 + throws BuildException { 1.487 + define(lookup(name), combine, pattern, loc); 1.488 + } 1.489 + 1.490 + private void define(RefPattern rp, GrammarSection.Combine combine, ParsedPattern pattern, Location loc) 1.491 + throws BuildException { 1.492 + switch (rp.getReplacementStatus()) { 1.493 + case RefPattern.REPLACEMENT_KEEP: 1.494 + if (combine == null) { 1.495 + if (rp.isCombineImplicit()) { 1.496 + if (rp.getName() == null) 1.497 + sb.error("duplicate_start", (Locator)loc); 1.498 + else 1.499 + sb.error("duplicate_define", rp.getName(), (Locator)loc); 1.500 + } 1.501 + else 1.502 + rp.setCombineImplicit(); 1.503 + } 1.504 + else { 1.505 + byte combineType = (combine == COMBINE_CHOICE ? RefPattern.COMBINE_CHOICE : RefPattern.COMBINE_INTERLEAVE); 1.506 + if (rp.getCombineType() != RefPattern.COMBINE_NONE 1.507 + && rp.getCombineType() != combineType) { 1.508 + if (rp.getName() == null) 1.509 + sb.error("conflict_combine_start", (Locator)loc); 1.510 + else 1.511 + sb.error("conflict_combine_define", rp.getName(), (Locator)loc); 1.512 + } 1.513 + rp.setCombineType(combineType); 1.514 + } 1.515 + Pattern p = (Pattern)pattern; 1.516 + if (rp.getPattern() == null) 1.517 + rp.setPattern(p); 1.518 + else if (rp.getCombineType() == RefPattern.COMBINE_INTERLEAVE) 1.519 + rp.setPattern(sb.pb.makeInterleave(rp.getPattern(), p)); 1.520 + else 1.521 + rp.setPattern(sb.pb.makeChoice(rp.getPattern(), p)); 1.522 + break; 1.523 + case RefPattern.REPLACEMENT_REQUIRE: 1.524 + rp.setReplacementStatus(RefPattern.REPLACEMENT_IGNORE); 1.525 + break; 1.526 + case RefPattern.REPLACEMENT_IGNORE: 1.527 + break; 1.528 + } 1.529 + } 1.530 + 1.531 + public void topLevelAnnotation(ParsedElementAnnotation ea) throws BuildException { 1.532 + } 1.533 + 1.534 + public void topLevelComment(CommentList comments) throws BuildException { 1.535 + } 1.536 + 1.537 + private RefPattern lookup(String name) { 1.538 + if (name == START) 1.539 + return startRef; 1.540 + return lookup1(name); 1.541 + } 1.542 + 1.543 + private RefPattern lookup1(String name) { 1.544 + RefPattern p = (RefPattern)defines.get(name); 1.545 + if (p == null) { 1.546 + p = new RefPattern(name); 1.547 + defines.put(name, p); 1.548 + } 1.549 + return p; 1.550 + } 1.551 + 1.552 + public ParsedPattern makeRef(String name, Location loc, Annotations anno) throws BuildException { 1.553 + RefPattern p = lookup1(name); 1.554 + if (p.getRefLocator() == null && loc != null) 1.555 + p.setRefLocator((Locator)loc); 1.556 + return p; 1.557 + } 1.558 + 1.559 + public ParsedPattern makeParentRef(String name, Location loc, Annotations anno) throws BuildException { 1.560 + // TODO: do this check by the caller 1.561 + if (parent == null) { 1.562 + sb.error("parent_ref_outside_grammar", (Locator)loc); 1.563 + return sb.makeErrorPattern(); 1.564 + } 1.565 + return parent.makeRef(name, loc, anno); 1.566 + } 1.567 + 1.568 + public Div makeDiv() { 1.569 + return this; 1.570 + } 1.571 + 1.572 + public Include makeInclude() { 1.573 + return new IncludeImpl(sb, this); 1.574 + } 1.575 + 1.576 + } 1.577 + 1.578 + 1.579 + static class Override { 1.580 + Override(RefPattern prp, Override next) { 1.581 + this.prp = prp; 1.582 + this.next = next; 1.583 + } 1.584 + 1.585 + RefPattern prp; 1.586 + Override next; 1.587 + byte replacementStatus; 1.588 + } 1.589 + 1.590 + 1.591 + private static class IncludeImpl implements Include, Div { 1.592 + private SchemaBuilderImpl sb; 1.593 + private Override overrides; 1.594 + private GrammarImpl grammar; 1.595 + 1.596 + private IncludeImpl(SchemaBuilderImpl sb, GrammarImpl grammar) { 1.597 + this.sb = sb; 1.598 + this.grammar = grammar; 1.599 + } 1.600 + 1.601 + public void define(String name, GrammarSection.Combine combine, ParsedPattern pattern, Location loc, Annotations anno) 1.602 + throws BuildException { 1.603 + RefPattern rp = grammar.lookup(name); 1.604 + overrides = new Override(rp, overrides); 1.605 + grammar.define(rp, combine, pattern, loc); 1.606 + } 1.607 + 1.608 + public void endDiv(Location loc, Annotations anno) throws BuildException { 1.609 + // nothing to do 1.610 + } 1.611 + 1.612 + public void topLevelAnnotation(ParsedElementAnnotation ea) throws BuildException { 1.613 + // nothing to do 1.614 + } 1.615 + 1.616 + public void topLevelComment(CommentList comments) throws BuildException { 1.617 + } 1.618 + 1.619 + public Div makeDiv() { 1.620 + return this; 1.621 + } 1.622 + 1.623 + public void endInclude(Parseable current,String uri, String ns, 1.624 + Location loc, Annotations anno) throws BuildException { 1.625 + for (OpenIncludes inc = sb.openIncludes; 1.626 + inc != null; 1.627 + inc = inc.parent) { 1.628 + if (inc.uri.equals(uri)) { 1.629 + sb.error("recursive_include", uri, (Locator)loc); 1.630 + return; 1.631 + } 1.632 + } 1.633 + 1.634 + for (Override o = overrides; o != null; o = o.next) { 1.635 + o.replacementStatus = o.prp.getReplacementStatus(); 1.636 + o.prp.setReplacementStatus(RefPattern.REPLACEMENT_REQUIRE); 1.637 + } 1.638 + try { 1.639 + SchemaBuilderImpl isb = new SchemaBuilderImpl(ns, uri, sb); 1.640 + current.parseInclude(uri, isb, new GrammarImpl(isb, grammar), ns); 1.641 + for (Override o = overrides; o != null; o = o.next) { 1.642 + if (o.prp.getReplacementStatus() == RefPattern.REPLACEMENT_REQUIRE) { 1.643 + if (o.prp.getName() == null) 1.644 + sb.error("missing_start_replacement", (Locator)loc); 1.645 + else 1.646 + sb.error("missing_define_replacement", o.prp.getName(), (Locator)loc); 1.647 + } 1.648 + } 1.649 + } 1.650 + catch (IllegalSchemaException e) { 1.651 + sb.noteError(); 1.652 + } 1.653 + finally { 1.654 + for (Override o = overrides; o != null; o = o.next) 1.655 + o.prp.setReplacementStatus(o.replacementStatus); 1.656 + } 1.657 + } 1.658 + 1.659 + public Include makeInclude() { 1.660 + return null; 1.661 + } 1.662 + } 1.663 + 1.664 + public Grammar makeGrammar(Scope parent) { 1.665 + return new GrammarImpl(this, parent); 1.666 + } 1.667 + 1.668 + public ParsedPattern annotate(ParsedPattern p, Annotations anno) throws BuildException { 1.669 + return p; 1.670 + } 1.671 + 1.672 + 1.673 + public ParsedPattern annotateAfter(ParsedPattern p, ParsedElementAnnotation e) throws BuildException { 1.674 + return p; 1.675 + } 1.676 + 1.677 + public ParsedPattern commentAfter(ParsedPattern p, CommentList comments) throws BuildException { 1.678 + return p; 1.679 + } 1.680 + 1.681 + 1.682 + public ParsedPattern makeExternalRef(Parseable current, String uri, String ns, Scope scope, 1.683 + Location loc, Annotations anno) 1.684 + throws BuildException { 1.685 + for (OpenIncludes inc = openIncludes; 1.686 + inc != null; 1.687 + inc = inc.parent) { 1.688 + if (inc.uri.equals(uri)) { 1.689 + error("recursive_include", uri, (Locator)loc); 1.690 + return pb.makeError(); 1.691 + } 1.692 + } 1.693 + try { 1.694 + return current.parseExternal(uri, new SchemaBuilderImpl(ns, uri, this), scope, ns ); 1.695 + } 1.696 + catch (IllegalSchemaException e) { 1.697 + noteError(); 1.698 + return pb.makeError(); 1.699 + } 1.700 + } 1.701 + 1.702 + 1.703 + 1.704 + public Location makeLocation(String systemId, int lineNumber, int columnNumber) { 1.705 + return new LocatorImpl(systemId, lineNumber, columnNumber); 1.706 + } 1.707 + 1.708 + public Annotations makeAnnotations(CommentList comments, Context context) { 1.709 + return this; 1.710 + } 1.711 + 1.712 + public ElementAnnotationBuilder makeElementAnnotationBuilder(String ns, String localName, String prefix, 1.713 + Location loc, CommentList comments, Context context) { 1.714 + return this; 1.715 + } 1.716 + 1.717 + public CommentList makeCommentList() { 1.718 + return this; 1.719 + } 1.720 + 1.721 + public void addComment(String value, Location loc) throws BuildException { 1.722 + } 1.723 + 1.724 + public void addAttribute(String ns, String localName, String prefix, String value, Location loc) { 1.725 + // nothing needed 1.726 + } 1.727 + 1.728 + public void addElement(ParsedElementAnnotation ea) { 1.729 + // nothing needed 1.730 + } 1.731 + 1.732 + public void addComment(CommentList comments) throws BuildException { 1.733 + // nothing needed 1.734 + } 1.735 + 1.736 + public void addLeadingComment(CommentList comments) throws BuildException { 1.737 + // nothing needed 1.738 + } 1.739 + 1.740 + public ParsedElementAnnotation makeElementAnnotation() { 1.741 + return null; 1.742 + } 1.743 + 1.744 + public void addText(String value, Location loc, CommentList comments) throws BuildException { 1.745 + } 1.746 + 1.747 + public boolean usesComments() { 1.748 + return false; 1.749 + } 1.750 + 1.751 + private void error(SAXParseException message) throws BuildException { 1.752 + noteError(); 1.753 + try { 1.754 + if (eh != null) 1.755 + eh.error(message); 1.756 + } 1.757 + catch (SAXException e) { 1.758 + throw new BuildException(e); 1.759 + } 1.760 + } 1.761 + 1.762 + private void warning(SAXParseException message) throws BuildException { 1.763 + try { 1.764 + if (eh != null) 1.765 + eh.warning(message); 1.766 + } 1.767 + catch (SAXException e) { 1.768 + throw new BuildException(e); 1.769 + } 1.770 + } 1.771 + 1.772 + private void error(String key, Locator loc) throws BuildException { 1.773 + error(new SAXParseException(localizer.message(key), loc)); 1.774 + } 1.775 + 1.776 + private void error(String key, String arg, Locator loc) throws BuildException { 1.777 + error(new SAXParseException(localizer.message(key, arg), loc)); 1.778 + } 1.779 + 1.780 + private void error(String key, String arg1, String arg2, Locator loc) throws BuildException { 1.781 + error(new SAXParseException(localizer.message(key, arg1, arg2), loc)); 1.782 + } 1.783 + 1.784 + private void error(String key, String arg1, String arg2, String arg3, Locator loc) throws BuildException { 1.785 + error(new SAXParseException(localizer.message(key, new Object[]{arg1, arg2, arg3}), loc)); 1.786 + } 1.787 + private void noteError() { 1.788 + if (!hadError && parent != null) 1.789 + parent.noteError(); 1.790 + hadError = true; 1.791 + } 1.792 + 1.793 +}