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

changeset 408
b0610cd08440
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
     1.1 --- a/src/share/jaxws_classes/com/sun/xml/internal/rngom/binary/SchemaBuilderImpl.java	Thu Sep 26 10:43:28 2013 -0700
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/rngom/binary/SchemaBuilderImpl.java	Fri Oct 04 16:21:34 2013 +0100
     1.3 @@ -23,7 +23,7 @@
     1.4   * questions.
     1.5   */
     1.6  /*
     1.7 - * Copyright (C) 2004-2011
     1.8 + * Copyright (C) 2004-2012
     1.9   *
    1.10   * Permission is hereby granted, free of charge, to any person obtaining a copy
    1.11   * of this software and associated documentation files (the "Software"), to deal
    1.12 @@ -88,703 +88,705 @@
    1.13  import org.xml.sax.SAXParseException;
    1.14  
    1.15  public class SchemaBuilderImpl implements SchemaBuilder, ElementAnnotationBuilder, CommentList {
    1.16 -  private final SchemaBuilderImpl parent;
    1.17 -  private boolean hadError = false;
    1.18 -  private final SchemaPatternBuilder pb;
    1.19 -  private final DatatypeLibraryFactory datatypeLibraryFactory;
    1.20 -  private final String inheritNs;
    1.21 -  private final ErrorHandler eh;
    1.22 -  private final OpenIncludes openIncludes;
    1.23 -  private final NameClassBuilder ncb =new NameClassBuilderImpl();
    1.24 -  static final Localizer localizer = new Localizer(SchemaBuilderImpl.class);
    1.25  
    1.26 -  static class OpenIncludes {
    1.27 -    final String uri;
    1.28 -    final OpenIncludes parent;
    1.29 +    private final SchemaBuilderImpl parent;
    1.30 +    private boolean hadError = false;
    1.31 +    private final SchemaPatternBuilder pb;
    1.32 +    private final DatatypeLibraryFactory datatypeLibraryFactory;
    1.33 +    private final String inheritNs;
    1.34 +    private final ErrorHandler eh;
    1.35 +    private final OpenIncludes openIncludes;
    1.36 +    private final NameClassBuilder ncb = new NameClassBuilderImpl();
    1.37 +    static final Localizer localizer = new Localizer(SchemaBuilderImpl.class);
    1.38  
    1.39 -    OpenIncludes(String uri, OpenIncludes parent) {
    1.40 -      this.uri = uri;
    1.41 -      this.parent = parent;
    1.42 +    static class OpenIncludes {
    1.43 +
    1.44 +        final String uri;
    1.45 +        final OpenIncludes parent;
    1.46 +
    1.47 +        OpenIncludes(String uri, OpenIncludes parent) {
    1.48 +            this.uri = uri;
    1.49 +            this.parent = parent;
    1.50 +        }
    1.51      }
    1.52 -  }
    1.53  
    1.54 -  public ParsedPattern expandPattern(ParsedPattern _pattern)
    1.55 -        throws BuildException, IllegalSchemaException {
    1.56 +    public ParsedPattern expandPattern(ParsedPattern _pattern)
    1.57 +            throws BuildException, IllegalSchemaException {
    1.58          Pattern pattern = (Pattern) _pattern;
    1.59          if (!hadError) {
    1.60              try {
    1.61                  pattern.checkRecursion(0);
    1.62                  pattern = pattern.expand(pb);
    1.63                  pattern.checkRestrictions(Pattern.START_CONTEXT, null, null);
    1.64 -                if (!hadError) return pattern;
    1.65 +                if (!hadError) {
    1.66 +                    return pattern;
    1.67 +                }
    1.68              } catch (SAXParseException e) {
    1.69                  error(e);
    1.70              } catch (SAXException e) {
    1.71                  throw new BuildException(e);
    1.72              } catch (RestrictionViolationException e) {
    1.73 -                if (e.getName() != null)
    1.74 +                if (e.getName() != null) {
    1.75                      error(e.getMessageId(), e.getName().toString(), e
    1.76 -                        .getLocator());
    1.77 -                else
    1.78 +                            .getLocator());
    1.79 +                } else {
    1.80                      error(e.getMessageId(), e.getLocator());
    1.81 +                }
    1.82              }
    1.83          }
    1.84          throw new IllegalSchemaException();
    1.85      }
    1.86  
    1.87 -  /**
    1.88 -   *
    1.89 -   * @param eh
    1.90 -   *        Error handler to receive errors while building the schema.
    1.91 -   */
    1.92 -  public SchemaBuilderImpl(ErrorHandler eh) {
    1.93 -      this(eh,
    1.94 -          new CascadingDatatypeLibraryFactory(new DatatypeLibraryLoader(),
    1.95 -            new BuiltinDatatypeLibraryFactory(new DatatypeLibraryLoader())),
    1.96 -          new SchemaPatternBuilder());
    1.97 -  }
    1.98 +    /**
    1.99 +     *
   1.100 +     * @param eh Error handler to receive errors while building the schema.
   1.101 +     */
   1.102 +    public SchemaBuilderImpl(ErrorHandler eh) {
   1.103 +        this(eh,
   1.104 +                new CascadingDatatypeLibraryFactory(new DatatypeLibraryLoader(),
   1.105 +                new BuiltinDatatypeLibraryFactory(new DatatypeLibraryLoader())),
   1.106 +                new SchemaPatternBuilder());
   1.107 +    }
   1.108  
   1.109 -  /**
   1.110 -   *
   1.111 -   * @param eh
   1.112 -   *        Error handler to receive errors while building the schema.
   1.113 -   * @param datatypeLibraryFactory
   1.114 -   *        This is consulted to locate datatype libraries.
   1.115 -   * @param pb
   1.116 -   *        Used to build patterns.
   1.117 -   */
   1.118 -  public SchemaBuilderImpl(ErrorHandler eh,
   1.119 -                            DatatypeLibraryFactory datatypeLibraryFactory,
   1.120 -                            SchemaPatternBuilder pb) {
   1.121 -    this.parent = null;
   1.122 -    this.eh = eh;
   1.123 -    this.datatypeLibraryFactory = datatypeLibraryFactory;
   1.124 -    this.pb = pb;
   1.125 -    this.inheritNs = "";
   1.126 -    this.openIncludes = null;
   1.127 -  }
   1.128 +    /**
   1.129 +     *
   1.130 +     * @param eh Error handler to receive errors while building the schema.
   1.131 +     * @param datatypeLibraryFactory This is consulted to locate datatype
   1.132 +     * libraries.
   1.133 +     * @param pb Used to build patterns.
   1.134 +     */
   1.135 +    public SchemaBuilderImpl(ErrorHandler eh,
   1.136 +            DatatypeLibraryFactory datatypeLibraryFactory,
   1.137 +            SchemaPatternBuilder pb) {
   1.138 +        this.parent = null;
   1.139 +        this.eh = eh;
   1.140 +        this.datatypeLibraryFactory = datatypeLibraryFactory;
   1.141 +        this.pb = pb;
   1.142 +        this.inheritNs = "";
   1.143 +        this.openIncludes = null;
   1.144 +    }
   1.145  
   1.146 -  private SchemaBuilderImpl(String inheritNs,
   1.147 -                            String uri,
   1.148 -                            SchemaBuilderImpl parent) {
   1.149 -    this.parent = parent;
   1.150 -    this.eh = parent.eh;
   1.151 -    this.datatypeLibraryFactory = parent.datatypeLibraryFactory;
   1.152 -    this.pb = parent.pb;
   1.153 -    this.inheritNs = inheritNs;
   1.154 -    this.openIncludes = new OpenIncludes(uri, parent.openIncludes);
   1.155 -  }
   1.156 +    private SchemaBuilderImpl(String inheritNs,
   1.157 +            String uri,
   1.158 +            SchemaBuilderImpl parent) {
   1.159 +        this.parent = parent;
   1.160 +        this.eh = parent.eh;
   1.161 +        this.datatypeLibraryFactory = parent.datatypeLibraryFactory;
   1.162 +        this.pb = parent.pb;
   1.163 +        this.inheritNs = inheritNs;
   1.164 +        this.openIncludes = new OpenIncludes(uri, parent.openIncludes);
   1.165 +    }
   1.166  
   1.167 -  public NameClassBuilder getNameClassBuilder() {
   1.168 -      return ncb;
   1.169 -  }
   1.170 +    public NameClassBuilder getNameClassBuilder() {
   1.171 +        return ncb;
   1.172 +    }
   1.173  
   1.174 -  public ParsedPattern makeChoice(List patterns, Location loc, Annotations anno)
   1.175 -          throws BuildException {
   1.176 -    if (patterns.isEmpty())
   1.177 -      throw new IllegalArgumentException();
   1.178 -    Pattern result = (Pattern)patterns.get(0);
   1.179 -    for (int i = 1; i < patterns.size(); i++)
   1.180 -      result = pb.makeChoice(result, (Pattern)patterns.get(i));
   1.181 -    return result;
   1.182 -  }
   1.183 +    public ParsedPattern makeChoice(List patterns, Location loc, Annotations anno)
   1.184 +            throws BuildException {
   1.185 +        if (patterns.isEmpty()) {
   1.186 +            throw new IllegalArgumentException();
   1.187 +        }
   1.188 +        Pattern result = (Pattern) patterns.get(0);
   1.189 +        for (int i = 1; i < patterns.size(); i++) {
   1.190 +            result = pb.makeChoice(result, (Pattern) patterns.get(i));
   1.191 +        }
   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 +    public ParsedPattern makeInterleave(List patterns, Location loc, Annotations anno)
   1.205 +            throws BuildException {
   1.206 +        if (patterns.isEmpty()) {
   1.207 +            throw new IllegalArgumentException();
   1.208 +        }
   1.209 +        Pattern result = (Pattern) patterns.get(0);
   1.210 +        for (int i = 1; i < patterns.size(); i++) {
   1.211 +            result = pb.makeInterleave(result, (Pattern) patterns.get(i));
   1.212 +        }
   1.213 +        return result;
   1.214 +    }
   1.215  
   1.216 -  public ParsedPattern makeGroup(List patterns, Location loc, Annotations anno)
   1.217 -          throws BuildException {
   1.218 -    if (patterns.isEmpty())
   1.219 -      throw new IllegalArgumentException();
   1.220 -    Pattern result = (Pattern)patterns.get(0);
   1.221 -    for (int i = 1; i < patterns.size(); i++)
   1.222 -      result = pb.makeGroup(result, (Pattern)patterns.get(i));
   1.223 -    return result;
   1.224 -  }
   1.225 +    public ParsedPattern makeGroup(List patterns, Location loc, Annotations anno)
   1.226 +            throws BuildException {
   1.227 +        if (patterns.isEmpty()) {
   1.228 +            throw new IllegalArgumentException();
   1.229 +        }
   1.230 +        Pattern result = (Pattern) patterns.get(0);
   1.231 +        for (int i = 1; i < patterns.size(); i++) {
   1.232 +            result = pb.makeGroup(result, (Pattern) patterns.get(i));
   1.233 +        }
   1.234 +        return result;
   1.235 +    }
   1.236  
   1.237 -  public ParsedPattern makeOneOrMore(ParsedPattern p, Location loc, Annotations anno)
   1.238 -          throws BuildException {
   1.239 -    return pb.makeOneOrMore((Pattern)p);
   1.240 -  }
   1.241 +    public ParsedPattern makeOneOrMore(ParsedPattern p, Location loc, Annotations anno)
   1.242 +            throws BuildException {
   1.243 +        return pb.makeOneOrMore((Pattern) p);
   1.244 +    }
   1.245  
   1.246 -  public ParsedPattern makeZeroOrMore(ParsedPattern p, Location loc, Annotations anno)
   1.247 -          throws BuildException {
   1.248 -    return pb.makeZeroOrMore((Pattern)p);
   1.249 -  }
   1.250 +    public ParsedPattern makeZeroOrMore(ParsedPattern p, Location loc, Annotations anno)
   1.251 +            throws BuildException {
   1.252 +        return pb.makeZeroOrMore((Pattern) p);
   1.253 +    }
   1.254  
   1.255 -  public ParsedPattern makeOptional(ParsedPattern p, Location loc, Annotations anno)
   1.256 -          throws BuildException {
   1.257 -    return pb.makeOptional((Pattern)p);
   1.258 -  }
   1.259 +    public ParsedPattern makeOptional(ParsedPattern p, Location loc, Annotations anno)
   1.260 +            throws BuildException {
   1.261 +        return pb.makeOptional((Pattern) p);
   1.262 +    }
   1.263  
   1.264 -  public ParsedPattern makeList(ParsedPattern p, Location loc, Annotations anno)
   1.265 -          throws BuildException {
   1.266 -    return pb.makeList((Pattern)p, (Locator)loc);
   1.267 -  }
   1.268 +    public ParsedPattern makeList(ParsedPattern p, Location loc, Annotations anno)
   1.269 +            throws BuildException {
   1.270 +        return pb.makeList((Pattern) p, (Locator) loc);
   1.271 +    }
   1.272  
   1.273 -  public ParsedPattern makeMixed(ParsedPattern p, Location loc, Annotations anno)
   1.274 -          throws BuildException {
   1.275 -    return pb.makeMixed((Pattern)p);
   1.276 -  }
   1.277 +    public ParsedPattern makeMixed(ParsedPattern p, Location loc, Annotations anno)
   1.278 +            throws BuildException {
   1.279 +        return pb.makeMixed((Pattern) p);
   1.280 +    }
   1.281  
   1.282 -  public ParsedPattern makeEmpty(Location loc, Annotations anno) {
   1.283 -    return pb.makeEmpty();
   1.284 -  }
   1.285 +    public ParsedPattern makeEmpty(Location loc, Annotations anno) {
   1.286 +        return pb.makeEmpty();
   1.287 +    }
   1.288  
   1.289 -  public ParsedPattern makeNotAllowed(Location loc, Annotations anno) {
   1.290 -    return pb.makeUnexpandedNotAllowed();
   1.291 -  }
   1.292 +    public ParsedPattern makeNotAllowed(Location loc, Annotations anno) {
   1.293 +        return pb.makeUnexpandedNotAllowed();
   1.294 +    }
   1.295  
   1.296 -  public ParsedPattern makeText(Location loc, Annotations anno) {
   1.297 -    return pb.makeText();
   1.298 -  }
   1.299 +    public ParsedPattern makeText(Location loc, Annotations anno) {
   1.300 +        return pb.makeText();
   1.301 +    }
   1.302  
   1.303 -  public ParsedPattern makeErrorPattern() {
   1.304 -    return pb.makeError();
   1.305 -  }
   1.306 +    public ParsedPattern makeErrorPattern() {
   1.307 +        return pb.makeError();
   1.308 +    }
   1.309  
   1.310  //  public ParsedNameClass makeErrorNameClass() {
   1.311  //    return new ErrorNameClass();
   1.312  //  }
   1.313 -
   1.314 -  public ParsedPattern makeAttribute(ParsedNameClass nc, ParsedPattern p, Location loc, Annotations anno)
   1.315 -          throws BuildException {
   1.316 -    return pb.makeAttribute((NameClass)nc, (Pattern)p, (Locator)loc);
   1.317 -  }
   1.318 -
   1.319 -  public ParsedPattern makeElement(ParsedNameClass nc, ParsedPattern p, Location loc, Annotations anno)
   1.320 -          throws BuildException {
   1.321 -    return pb.makeElement((NameClass)nc, (Pattern)p, (Locator)loc);
   1.322 -  }
   1.323 -
   1.324 -  private class DummyDataPatternBuilder implements DataPatternBuilder {
   1.325 -    public void addParam(String name, String value, Context context, String ns, Location loc, Annotations anno)
   1.326 +    public ParsedPattern makeAttribute(ParsedNameClass nc, ParsedPattern p, Location loc, Annotations anno)
   1.327              throws BuildException {
   1.328 +        return pb.makeAttribute((NameClass) nc, (Pattern) p, (Locator) loc);
   1.329      }
   1.330  
   1.331 -    public ParsedPattern makePattern(Location loc, Annotations anno)
   1.332 +    public ParsedPattern makeElement(ParsedNameClass nc, ParsedPattern p, Location loc, Annotations anno)
   1.333              throws BuildException {
   1.334 -      return pb.makeError();
   1.335 +        return pb.makeElement((NameClass) nc, (Pattern) p, (Locator) loc);
   1.336      }
   1.337  
   1.338 -    public ParsedPattern makePattern(ParsedPattern except, Location loc, Annotations anno)
   1.339 -            throws BuildException {
   1.340 -      return pb.makeError();
   1.341 +    private class DummyDataPatternBuilder implements DataPatternBuilder {
   1.342 +
   1.343 +        public void addParam(String name, String value, Context context, String ns, Location loc, Annotations anno)
   1.344 +                throws BuildException {
   1.345 +        }
   1.346 +
   1.347 +        public ParsedPattern makePattern(Location loc, Annotations anno)
   1.348 +                throws BuildException {
   1.349 +            return pb.makeError();
   1.350 +        }
   1.351 +
   1.352 +        public ParsedPattern makePattern(ParsedPattern except, Location loc, Annotations anno)
   1.353 +                throws BuildException {
   1.354 +            return pb.makeError();
   1.355 +        }
   1.356 +
   1.357 +        public void annotation(ParsedElementAnnotation ea) {
   1.358 +        }
   1.359      }
   1.360  
   1.361 -    public void annotation(ParsedElementAnnotation ea) {
   1.362 -    }
   1.363 -  }
   1.364 +    private static class ValidationContextImpl implements ValidationContext {
   1.365  
   1.366 -  private class ValidationContextImpl implements ValidationContext {
   1.367 -    private ValidationContext vc;
   1.368 -    private String ns;
   1.369 +        private ValidationContext vc;
   1.370 +        private String ns;
   1.371  
   1.372 -    ValidationContextImpl(ValidationContext vc, String ns) {
   1.373 -      this.vc = vc;
   1.374 -      this.ns = ns.length() == 0 ? null : ns;
   1.375 +        ValidationContextImpl(ValidationContext vc, String ns) {
   1.376 +            this.vc = vc;
   1.377 +            this.ns = ns.length() == 0 ? null : ns;
   1.378 +        }
   1.379 +
   1.380 +        public String resolveNamespacePrefix(String prefix) {
   1.381 +            return prefix.length() == 0 ? ns : vc.resolveNamespacePrefix(prefix);
   1.382 +        }
   1.383 +
   1.384 +        public String getBaseUri() {
   1.385 +            return vc.getBaseUri();
   1.386 +        }
   1.387 +
   1.388 +        public boolean isUnparsedEntity(String entityName) {
   1.389 +            return vc.isUnparsedEntity(entityName);
   1.390 +        }
   1.391 +
   1.392 +        public boolean isNotation(String notationName) {
   1.393 +            return vc.isNotation(notationName);
   1.394 +        }
   1.395      }
   1.396  
   1.397 -    public String resolveNamespacePrefix(String prefix) {
   1.398 -      return prefix.length() == 0 ? ns : vc.resolveNamespacePrefix(prefix);
   1.399 +    private class DataPatternBuilderImpl implements DataPatternBuilder {
   1.400 +
   1.401 +        private DatatypeBuilder dtb;
   1.402 +
   1.403 +        DataPatternBuilderImpl(DatatypeBuilder dtb) {
   1.404 +            this.dtb = dtb;
   1.405 +        }
   1.406 +
   1.407 +        public void addParam(String name, String value, Context context, String ns, Location loc, Annotations anno)
   1.408 +                throws BuildException {
   1.409 +            try {
   1.410 +                dtb.addParameter(name, value, new ValidationContextImpl(context, ns));
   1.411 +            } catch (DatatypeException e) {
   1.412 +                String detail = e.getMessage();
   1.413 +                int pos = e.getIndex();
   1.414 +                String displayedParam;
   1.415 +                if (pos == DatatypeException.UNKNOWN) {
   1.416 +                    displayedParam = null;
   1.417 +                } else {
   1.418 +                    displayedParam = displayParam(value, pos);
   1.419 +                }
   1.420 +                if (displayedParam != null) {
   1.421 +                    if (detail != null) {
   1.422 +                        error("invalid_param_detail_display", detail, displayedParam, (Locator) loc);
   1.423 +                    } else {
   1.424 +                        error("invalid_param_display", displayedParam, (Locator) loc);
   1.425 +                    }
   1.426 +                } else if (detail != null) {
   1.427 +                    error("invalid_param_detail", detail, (Locator) loc);
   1.428 +                } else {
   1.429 +                    error("invalid_param", (Locator) loc);
   1.430 +                }
   1.431 +            }
   1.432 +        }
   1.433 +
   1.434 +        String displayParam(String value, int pos) {
   1.435 +            if (pos < 0) {
   1.436 +                pos = 0;
   1.437 +            } else if (pos > value.length()) {
   1.438 +                pos = value.length();
   1.439 +            }
   1.440 +            return localizer.message("display_param", value.substring(0, pos), value.substring(pos));
   1.441 +        }
   1.442 +
   1.443 +        public ParsedPattern makePattern(Location loc, Annotations anno)
   1.444 +                throws BuildException {
   1.445 +            try {
   1.446 +                return pb.makeData(dtb.createDatatype());
   1.447 +            } catch (DatatypeException e) {
   1.448 +                String detail = e.getMessage();
   1.449 +                if (detail != null) {
   1.450 +                    error("invalid_params_detail", detail, (Locator) loc);
   1.451 +                } else {
   1.452 +                    error("invalid_params", (Locator) loc);
   1.453 +                }
   1.454 +                return pb.makeError();
   1.455 +            }
   1.456 +        }
   1.457 +
   1.458 +        public ParsedPattern makePattern(ParsedPattern except, Location loc, Annotations anno)
   1.459 +                throws BuildException {
   1.460 +            try {
   1.461 +                return pb.makeDataExcept(dtb.createDatatype(), (Pattern) except, (Locator) loc);
   1.462 +            } catch (DatatypeException e) {
   1.463 +                String detail = e.getMessage();
   1.464 +                if (detail != null) {
   1.465 +                    error("invalid_params_detail", detail, (Locator) loc);
   1.466 +                } else {
   1.467 +                    error("invalid_params", (Locator) loc);
   1.468 +                }
   1.469 +                return pb.makeError();
   1.470 +            }
   1.471 +        }
   1.472 +
   1.473 +        public void annotation(ParsedElementAnnotation ea) {
   1.474 +        }
   1.475      }
   1.476  
   1.477 -    public String getBaseUri() {
   1.478 -      return vc.getBaseUri();
   1.479 +    public DataPatternBuilder makeDataPatternBuilder(String datatypeLibrary, String type, Location loc)
   1.480 +            throws BuildException {
   1.481 +        DatatypeLibrary dl = datatypeLibraryFactory.createDatatypeLibrary(datatypeLibrary);
   1.482 +        if (dl == null) {
   1.483 +            error("unrecognized_datatype_library", datatypeLibrary, (Locator) loc);
   1.484 +        } else {
   1.485 +            try {
   1.486 +                return new DataPatternBuilderImpl(dl.createDatatypeBuilder(type));
   1.487 +            } catch (DatatypeException e) {
   1.488 +                String detail = e.getMessage();
   1.489 +                if (detail != null) {
   1.490 +                    error("unsupported_datatype_detail", datatypeLibrary, type, detail, (Locator) loc);
   1.491 +                } else {
   1.492 +                    error("unrecognized_datatype", datatypeLibrary, type, (Locator) loc);
   1.493 +                }
   1.494 +            }
   1.495 +        }
   1.496 +        return new DummyDataPatternBuilder();
   1.497      }
   1.498  
   1.499 -    public boolean isUnparsedEntity(String entityName) {
   1.500 -      return vc.isUnparsedEntity(entityName);
   1.501 +    public ParsedPattern makeValue(String datatypeLibrary, String type, String value, Context context, String ns,
   1.502 +            Location loc, Annotations anno) throws BuildException {
   1.503 +        DatatypeLibrary dl = datatypeLibraryFactory.createDatatypeLibrary(datatypeLibrary);
   1.504 +        if (dl == null) {
   1.505 +            error("unrecognized_datatype_library", datatypeLibrary, (Locator) loc);
   1.506 +        } else {
   1.507 +            try {
   1.508 +                DatatypeBuilder dtb = dl.createDatatypeBuilder(type);
   1.509 +                try {
   1.510 +                    Datatype dt = dtb.createDatatype();
   1.511 +                    Object obj = dt.createValue(value, new ValidationContextImpl(context, ns));
   1.512 +                    if (obj != null) {
   1.513 +                        return pb.makeValue(dt, obj);
   1.514 +                    }
   1.515 +                    error("invalid_value", value, (Locator) loc);
   1.516 +                } catch (DatatypeException e) {
   1.517 +                    String detail = e.getMessage();
   1.518 +                    if (detail != null) {
   1.519 +                        error("datatype_requires_param_detail", detail, (Locator) loc);
   1.520 +                    } else {
   1.521 +                        error("datatype_requires_param", (Locator) loc);
   1.522 +                    }
   1.523 +                }
   1.524 +            } catch (DatatypeException e) {
   1.525 +                error("unrecognized_datatype", datatypeLibrary, type, (Locator) loc);
   1.526 +            }
   1.527 +        }
   1.528 +        return pb.makeError();
   1.529      }
   1.530  
   1.531 -    public boolean isNotation(String notationName) {
   1.532 -      return vc.isNotation(notationName);
   1.533 -    }
   1.534 -  }
   1.535 +    static class GrammarImpl implements Grammar, Div, IncludedGrammar {
   1.536  
   1.537 -  private class DataPatternBuilderImpl implements DataPatternBuilder {
   1.538 -    private DatatypeBuilder dtb;
   1.539 -    DataPatternBuilderImpl(DatatypeBuilder dtb) {
   1.540 -      this.dtb = dtb;
   1.541 +        private final SchemaBuilderImpl sb;
   1.542 +        private final Hashtable defines;
   1.543 +        private final RefPattern startRef;
   1.544 +        private final Scope parent;
   1.545 +
   1.546 +        private GrammarImpl(SchemaBuilderImpl sb, Scope parent) {
   1.547 +            this.sb = sb;
   1.548 +            this.parent = parent;
   1.549 +            this.defines = new Hashtable();
   1.550 +            this.startRef = new RefPattern(null);
   1.551 +        }
   1.552 +
   1.553 +        protected GrammarImpl(SchemaBuilderImpl sb, GrammarImpl g) {
   1.554 +            this.sb = sb;
   1.555 +            parent = g.parent;
   1.556 +            startRef = g.startRef;
   1.557 +            defines = g.defines;
   1.558 +        }
   1.559 +
   1.560 +        public ParsedPattern endGrammar(Location loc, Annotations anno) throws BuildException {
   1.561 +            for (Enumeration e = defines.keys();
   1.562 +                    e.hasMoreElements();) {
   1.563 +                String name = (String) e.nextElement();
   1.564 +                RefPattern rp = (RefPattern) defines.get(name);
   1.565 +                if (rp.getPattern() == null) {
   1.566 +                    sb.error("reference_to_undefined", name, rp.getRefLocator());
   1.567 +                    rp.setPattern(sb.pb.makeError());
   1.568 +                }
   1.569 +            }
   1.570 +            Pattern start = startRef.getPattern();
   1.571 +            if (start == null) {
   1.572 +                sb.error("missing_start_element", (Locator) loc);
   1.573 +                start = sb.pb.makeError();
   1.574 +            }
   1.575 +            return start;
   1.576 +        }
   1.577 +
   1.578 +        public void endDiv(Location loc, Annotations anno) throws BuildException {
   1.579 +            // nothing to do
   1.580 +        }
   1.581 +
   1.582 +        public ParsedPattern endIncludedGrammar(Location loc, Annotations anno) throws BuildException {
   1.583 +            return null;
   1.584 +        }
   1.585 +
   1.586 +        public void define(String name, GrammarSection.Combine combine, ParsedPattern pattern, Location loc, Annotations anno)
   1.587 +                throws BuildException {
   1.588 +            define(lookup(name), combine, pattern, loc);
   1.589 +        }
   1.590 +
   1.591 +        private void define(RefPattern rp, GrammarSection.Combine combine, ParsedPattern pattern, Location loc)
   1.592 +                throws BuildException {
   1.593 +            switch (rp.getReplacementStatus()) {
   1.594 +                case RefPattern.REPLACEMENT_KEEP:
   1.595 +                    if (combine == null) {
   1.596 +                        if (rp.isCombineImplicit()) {
   1.597 +                            if (rp.getName() == null) {
   1.598 +                                sb.error("duplicate_start", (Locator) loc);
   1.599 +                            } else {
   1.600 +                                sb.error("duplicate_define", rp.getName(), (Locator) loc);
   1.601 +                            }
   1.602 +                        } else {
   1.603 +                            rp.setCombineImplicit();
   1.604 +                        }
   1.605 +                    } else {
   1.606 +                        byte combineType = (combine == COMBINE_CHOICE ? RefPattern.COMBINE_CHOICE : RefPattern.COMBINE_INTERLEAVE);
   1.607 +                        if (rp.getCombineType() != RefPattern.COMBINE_NONE
   1.608 +                                && rp.getCombineType() != combineType) {
   1.609 +                            if (rp.getName() == null) {
   1.610 +                                sb.error("conflict_combine_start", (Locator) loc);
   1.611 +                            } else {
   1.612 +                                sb.error("conflict_combine_define", rp.getName(), (Locator) loc);
   1.613 +                            }
   1.614 +                        }
   1.615 +                        rp.setCombineType(combineType);
   1.616 +                    }
   1.617 +                    Pattern p = (Pattern) pattern;
   1.618 +                    if (rp.getPattern() == null) {
   1.619 +                        rp.setPattern(p);
   1.620 +                    } else if (rp.getCombineType() == RefPattern.COMBINE_INTERLEAVE) {
   1.621 +                        rp.setPattern(sb.pb.makeInterleave(rp.getPattern(), p));
   1.622 +                    } else {
   1.623 +                        rp.setPattern(sb.pb.makeChoice(rp.getPattern(), p));
   1.624 +                    }
   1.625 +                    break;
   1.626 +                case RefPattern.REPLACEMENT_REQUIRE:
   1.627 +                    rp.setReplacementStatus(RefPattern.REPLACEMENT_IGNORE);
   1.628 +                    break;
   1.629 +                case RefPattern.REPLACEMENT_IGNORE:
   1.630 +                    break;
   1.631 +            }
   1.632 +        }
   1.633 +
   1.634 +        public void topLevelAnnotation(ParsedElementAnnotation ea) throws BuildException {
   1.635 +        }
   1.636 +
   1.637 +        public void topLevelComment(CommentList comments) throws BuildException {
   1.638 +        }
   1.639 +
   1.640 +        private RefPattern lookup(String name) {
   1.641 +            if (name == START) {
   1.642 +                return startRef;
   1.643 +            }
   1.644 +            return lookup1(name);
   1.645 +        }
   1.646 +
   1.647 +        private RefPattern lookup1(String name) {
   1.648 +            RefPattern p = (RefPattern) defines.get(name);
   1.649 +            if (p == null) {
   1.650 +                p = new RefPattern(name);
   1.651 +                defines.put(name, p);
   1.652 +            }
   1.653 +            return p;
   1.654 +        }
   1.655 +
   1.656 +        public ParsedPattern makeRef(String name, Location loc, Annotations anno) throws BuildException {
   1.657 +            RefPattern p = lookup1(name);
   1.658 +            if (p.getRefLocator() == null && loc != null) {
   1.659 +                p.setRefLocator((Locator) loc);
   1.660 +            }
   1.661 +            return p;
   1.662 +        }
   1.663 +
   1.664 +        public ParsedPattern makeParentRef(String name, Location loc, Annotations anno) throws BuildException {
   1.665 +            // TODO: do this check by the caller
   1.666 +            if (parent == null) {
   1.667 +                sb.error("parent_ref_outside_grammar", (Locator) loc);
   1.668 +                return sb.makeErrorPattern();
   1.669 +            }
   1.670 +            return parent.makeRef(name, loc, anno);
   1.671 +        }
   1.672 +
   1.673 +        public Div makeDiv() {
   1.674 +            return this;
   1.675 +        }
   1.676 +
   1.677 +        public Include makeInclude() {
   1.678 +            return new IncludeImpl(sb, this);
   1.679 +        }
   1.680      }
   1.681  
   1.682 -    public void addParam(String name, String value, Context context, String ns, Location loc, Annotations anno)
   1.683 -            throws BuildException {
   1.684 -      try {
   1.685 -        dtb.addParameter(name, value, new ValidationContextImpl(context, ns));
   1.686 -      }
   1.687 -      catch (DatatypeException e) {
   1.688 -        String detail = e.getMessage();
   1.689 -        int pos = e.getIndex();
   1.690 -        String displayedParam;
   1.691 -        if (pos == DatatypeException.UNKNOWN)
   1.692 -          displayedParam = null;
   1.693 -        else
   1.694 -          displayedParam = displayParam(value, pos);
   1.695 -        if (displayedParam != null) {
   1.696 -          if (detail != null)
   1.697 -            error("invalid_param_detail_display", detail, displayedParam, (Locator)loc);
   1.698 -          else
   1.699 -            error("invalid_param_display", displayedParam, (Locator)loc);
   1.700 +    static class Override {
   1.701 +
   1.702 +        Override(RefPattern prp, Override next) {
   1.703 +            this.prp = prp;
   1.704 +            this.next = next;
   1.705          }
   1.706 -        else if (detail != null)
   1.707 -          error("invalid_param_detail", detail, (Locator)loc);
   1.708 -        else
   1.709 -          error("invalid_param", (Locator)loc);
   1.710 -      }
   1.711 +        RefPattern prp;
   1.712 +        Override next;
   1.713 +        byte replacementStatus;
   1.714      }
   1.715  
   1.716 -    String displayParam(String value, int pos) {
   1.717 -      if (pos < 0)
   1.718 -        pos = 0;
   1.719 -      else if (pos > value.length())
   1.720 -        pos = value.length();
   1.721 -      return localizer.message("display_param", value.substring(0, pos), value.substring(pos));
   1.722 +    private static class IncludeImpl implements Include, Div {
   1.723 +
   1.724 +        private SchemaBuilderImpl sb;
   1.725 +        private Override overrides;
   1.726 +        private GrammarImpl grammar;
   1.727 +
   1.728 +        private IncludeImpl(SchemaBuilderImpl sb, GrammarImpl grammar) {
   1.729 +            this.sb = sb;
   1.730 +            this.grammar = grammar;
   1.731 +        }
   1.732 +
   1.733 +        public void define(String name, GrammarSection.Combine combine, ParsedPattern pattern, Location loc, Annotations anno)
   1.734 +                throws BuildException {
   1.735 +            RefPattern rp = grammar.lookup(name);
   1.736 +            overrides = new Override(rp, overrides);
   1.737 +            grammar.define(rp, combine, pattern, loc);
   1.738 +        }
   1.739 +
   1.740 +        public void endDiv(Location loc, Annotations anno) throws BuildException {
   1.741 +            // nothing to do
   1.742 +        }
   1.743 +
   1.744 +        public void topLevelAnnotation(ParsedElementAnnotation ea) throws BuildException {
   1.745 +            // nothing to do
   1.746 +        }
   1.747 +
   1.748 +        public void topLevelComment(CommentList comments) throws BuildException {
   1.749 +        }
   1.750 +
   1.751 +        public Div makeDiv() {
   1.752 +            return this;
   1.753 +        }
   1.754 +
   1.755 +        public void endInclude(Parseable current, String uri, String ns,
   1.756 +                Location loc, Annotations anno) throws BuildException {
   1.757 +            for (OpenIncludes inc = sb.openIncludes;
   1.758 +                    inc != null;
   1.759 +                    inc = inc.parent) {
   1.760 +                if (inc.uri.equals(uri)) {
   1.761 +                    sb.error("recursive_include", uri, (Locator) loc);
   1.762 +                    return;
   1.763 +                }
   1.764 +            }
   1.765 +
   1.766 +            for (Override o = overrides; o != null; o = o.next) {
   1.767 +                o.replacementStatus = o.prp.getReplacementStatus();
   1.768 +                o.prp.setReplacementStatus(RefPattern.REPLACEMENT_REQUIRE);
   1.769 +            }
   1.770 +            try {
   1.771 +                SchemaBuilderImpl isb = new SchemaBuilderImpl(ns, uri, sb);
   1.772 +                current.parseInclude(uri, isb, new GrammarImpl(isb, grammar), ns);
   1.773 +                for (Override o = overrides; o != null; o = o.next) {
   1.774 +                    if (o.prp.getReplacementStatus() == RefPattern.REPLACEMENT_REQUIRE) {
   1.775 +                        if (o.prp.getName() == null) {
   1.776 +                            sb.error("missing_start_replacement", (Locator) loc);
   1.777 +                        } else {
   1.778 +                            sb.error("missing_define_replacement", o.prp.getName(), (Locator) loc);
   1.779 +                        }
   1.780 +                    }
   1.781 +                }
   1.782 +            } catch (IllegalSchemaException e) {
   1.783 +                sb.noteError();
   1.784 +            } finally {
   1.785 +                for (Override o = overrides; o != null; o = o.next) {
   1.786 +                    o.prp.setReplacementStatus(o.replacementStatus);
   1.787 +                }
   1.788 +            }
   1.789 +        }
   1.790 +
   1.791 +        public Include makeInclude() {
   1.792 +            return null;
   1.793 +        }
   1.794      }
   1.795  
   1.796 -    public ParsedPattern makePattern(Location loc, Annotations anno)
   1.797 -            throws BuildException {
   1.798 -      try {
   1.799 -        return pb.makeData(dtb.createDatatype());
   1.800 -      }
   1.801 -      catch (DatatypeException e) {
   1.802 -        String detail = e.getMessage();
   1.803 -        if (detail != null)
   1.804 -          error("invalid_params_detail", detail, (Locator)loc);
   1.805 -        else
   1.806 -          error("invalid_params", (Locator)loc);
   1.807 -        return pb.makeError();
   1.808 -      }
   1.809 +    public Grammar makeGrammar(Scope parent) {
   1.810 +        return new GrammarImpl(this, parent);
   1.811      }
   1.812  
   1.813 -    public ParsedPattern makePattern(ParsedPattern except, Location loc, Annotations anno)
   1.814 -            throws BuildException {
   1.815 -      try {
   1.816 -        return pb.makeDataExcept(dtb.createDatatype(), (Pattern)except, (Locator)loc);
   1.817 -      }
   1.818 -      catch (DatatypeException e) {
   1.819 -        String detail = e.getMessage();
   1.820 -        if (detail != null)
   1.821 -          error("invalid_params_detail", detail, (Locator)loc);
   1.822 -        else
   1.823 -          error("invalid_params", (Locator)loc);
   1.824 -        return pb.makeError();
   1.825 -      }
   1.826 +    public ParsedPattern annotate(ParsedPattern p, Annotations anno) throws BuildException {
   1.827 +        return p;
   1.828      }
   1.829  
   1.830 -    public void annotation(ParsedElementAnnotation ea) {
   1.831 -    }
   1.832 -  }
   1.833 -
   1.834 -  public DataPatternBuilder makeDataPatternBuilder(String datatypeLibrary, String type, Location loc)
   1.835 -          throws BuildException {
   1.836 -    DatatypeLibrary dl = datatypeLibraryFactory.createDatatypeLibrary(datatypeLibrary);
   1.837 -    if (dl == null)
   1.838 -      error("unrecognized_datatype_library", datatypeLibrary, (Locator)loc);
   1.839 -    else {
   1.840 -      try {
   1.841 -        return new DataPatternBuilderImpl(dl.createDatatypeBuilder(type));
   1.842 -      }
   1.843 -      catch (DatatypeException e) {
   1.844 -        String detail = e.getMessage();
   1.845 -        if (detail != null)
   1.846 -          error("unsupported_datatype_detail", datatypeLibrary, type, detail, (Locator)loc);
   1.847 -        else
   1.848 -          error("unrecognized_datatype", datatypeLibrary, type, (Locator)loc);
   1.849 -      }
   1.850 -    }
   1.851 -    return new DummyDataPatternBuilder();
   1.852 -  }
   1.853 -
   1.854 -  public ParsedPattern makeValue(String datatypeLibrary, String type, String value, Context context, String ns,
   1.855 -                                 Location loc, Annotations anno) throws BuildException {
   1.856 -    DatatypeLibrary dl = datatypeLibraryFactory.createDatatypeLibrary(datatypeLibrary);
   1.857 -    if (dl == null)
   1.858 -      error("unrecognized_datatype_library", datatypeLibrary, (Locator)loc);
   1.859 -    else {
   1.860 -      try {
   1.861 -        DatatypeBuilder dtb = dl.createDatatypeBuilder(type);
   1.862 -        try {
   1.863 -          Datatype dt = dtb.createDatatype();
   1.864 -          Object obj = dt.createValue(value, new ValidationContextImpl(context, ns));
   1.865 -          if (obj != null)
   1.866 -            return pb.makeValue(dt, obj);
   1.867 -          error("invalid_value", value, (Locator)loc);
   1.868 -        }
   1.869 -        catch (DatatypeException e) {
   1.870 -          String detail = e.getMessage();
   1.871 -          if (detail != null)
   1.872 -            error("datatype_requires_param_detail", detail, (Locator)loc);
   1.873 -          else
   1.874 -            error("datatype_requires_param", (Locator)loc);
   1.875 -        }
   1.876 -      }
   1.877 -      catch (DatatypeException e) {
   1.878 -        error("unrecognized_datatype", datatypeLibrary, type, (Locator)loc);
   1.879 -      }
   1.880 -    }
   1.881 -    return pb.makeError();
   1.882 -  }
   1.883 -
   1.884 -  static class GrammarImpl implements Grammar, Div, IncludedGrammar {
   1.885 -    private final SchemaBuilderImpl sb;
   1.886 -    private final Hashtable defines;
   1.887 -    private final RefPattern startRef;
   1.888 -    private final Scope parent;
   1.889 -
   1.890 -    private GrammarImpl(SchemaBuilderImpl sb, Scope parent) {
   1.891 -      this.sb = sb;
   1.892 -      this.parent = parent;
   1.893 -      this.defines = new Hashtable();
   1.894 -      this.startRef = new RefPattern(null);
   1.895 +    public ParsedPattern annotateAfter(ParsedPattern p, ParsedElementAnnotation e) throws BuildException {
   1.896 +        return p;
   1.897      }
   1.898  
   1.899 -    protected GrammarImpl(SchemaBuilderImpl sb, GrammarImpl g) {
   1.900 -      this.sb = sb;
   1.901 -      parent = g.parent;
   1.902 -      startRef = g.startRef;
   1.903 -      defines = g.defines;
   1.904 +    public ParsedPattern commentAfter(ParsedPattern p, CommentList comments) throws BuildException {
   1.905 +        return p;
   1.906      }
   1.907  
   1.908 -    public ParsedPattern endGrammar(Location loc, Annotations anno) throws BuildException {
   1.909 -      for (Enumeration e = defines.keys();
   1.910 -           e.hasMoreElements();) {
   1.911 -        String name = (String)e.nextElement();
   1.912 -        RefPattern rp = (RefPattern)defines.get(name);
   1.913 -        if (rp.getPattern() == null) {
   1.914 -          sb.error("reference_to_undefined", name, rp.getRefLocator());
   1.915 -          rp.setPattern(sb.pb.makeError());
   1.916 +    public ParsedPattern makeExternalRef(Parseable current, String uri, String ns, Scope scope,
   1.917 +            Location loc, Annotations anno)
   1.918 +            throws BuildException {
   1.919 +        for (OpenIncludes inc = openIncludes;
   1.920 +                inc != null;
   1.921 +                inc = inc.parent) {
   1.922 +            if (inc.uri.equals(uri)) {
   1.923 +                error("recursive_include", uri, (Locator) loc);
   1.924 +                return pb.makeError();
   1.925 +            }
   1.926          }
   1.927 -      }
   1.928 -      Pattern start = startRef.getPattern();
   1.929 -      if (start == null) {
   1.930 -        sb.error("missing_start_element", (Locator)loc);
   1.931 -        start = sb.pb.makeError();
   1.932 -      }
   1.933 -      return start;
   1.934 +        try {
   1.935 +            return current.parseExternal(uri, new SchemaBuilderImpl(ns, uri, this), scope, ns);
   1.936 +        } catch (IllegalSchemaException e) {
   1.937 +            noteError();
   1.938 +            return pb.makeError();
   1.939 +        }
   1.940      }
   1.941  
   1.942 -    public void endDiv(Location loc, Annotations anno) throws BuildException {
   1.943 -      // nothing to do
   1.944 +    public Location makeLocation(String systemId, int lineNumber, int columnNumber) {
   1.945 +        return new LocatorImpl(systemId, lineNumber, columnNumber);
   1.946      }
   1.947  
   1.948 -    public ParsedPattern endIncludedGrammar(Location loc, Annotations anno) throws BuildException {
   1.949 +    public Annotations makeAnnotations(CommentList comments, Context context) {
   1.950 +        return this;
   1.951 +    }
   1.952 +
   1.953 +    public ElementAnnotationBuilder makeElementAnnotationBuilder(String ns, String localName, String prefix,
   1.954 +            Location loc, CommentList comments, Context context) {
   1.955 +        return this;
   1.956 +    }
   1.957 +
   1.958 +    public CommentList makeCommentList() {
   1.959 +        return this;
   1.960 +    }
   1.961 +
   1.962 +    public void addComment(String value, Location loc) throws BuildException {
   1.963 +    }
   1.964 +
   1.965 +    public void addAttribute(String ns, String localName, String prefix, String value, Location loc) {
   1.966 +        // nothing needed
   1.967 +    }
   1.968 +
   1.969 +    public void addElement(ParsedElementAnnotation ea) {
   1.970 +        // nothing needed
   1.971 +    }
   1.972 +
   1.973 +    public void addComment(CommentList comments) throws BuildException {
   1.974 +        // nothing needed
   1.975 +    }
   1.976 +
   1.977 +    public void addLeadingComment(CommentList comments) throws BuildException {
   1.978 +        // nothing needed
   1.979 +    }
   1.980 +
   1.981 +    public ParsedElementAnnotation makeElementAnnotation() {
   1.982          return null;
   1.983      }
   1.984  
   1.985 -    public void define(String name, GrammarSection.Combine combine, ParsedPattern pattern, Location loc, Annotations anno)
   1.986 -            throws BuildException {
   1.987 -      define(lookup(name), combine, pattern, loc);
   1.988 +    public void addText(String value, Location loc, CommentList comments) throws BuildException {
   1.989      }
   1.990  
   1.991 -    private void define(RefPattern rp, GrammarSection.Combine combine, ParsedPattern pattern, Location loc)
   1.992 -            throws BuildException {
   1.993 -      switch (rp.getReplacementStatus()) {
   1.994 -      case RefPattern.REPLACEMENT_KEEP:
   1.995 -        if (combine == null) {
   1.996 -          if (rp.isCombineImplicit()) {
   1.997 -            if (rp.getName() == null)
   1.998 -              sb.error("duplicate_start", (Locator)loc);
   1.999 -            else
  1.1000 -              sb.error("duplicate_define", rp.getName(), (Locator)loc);
  1.1001 -          }
  1.1002 -          else
  1.1003 -            rp.setCombineImplicit();
  1.1004 -        }
  1.1005 -        else {
  1.1006 -          byte combineType = (combine == COMBINE_CHOICE ? RefPattern.COMBINE_CHOICE : RefPattern.COMBINE_INTERLEAVE);
  1.1007 -          if (rp.getCombineType() != RefPattern.COMBINE_NONE
  1.1008 -              && rp.getCombineType() != combineType) {
  1.1009 -            if (rp.getName() == null)
  1.1010 -              sb.error("conflict_combine_start", (Locator)loc);
  1.1011 -            else
  1.1012 -              sb.error("conflict_combine_define", rp.getName(), (Locator)loc);
  1.1013 -          }
  1.1014 -          rp.setCombineType(combineType);
  1.1015 -        }
  1.1016 -        Pattern p = (Pattern)pattern;
  1.1017 -        if (rp.getPattern() == null)
  1.1018 -          rp.setPattern(p);
  1.1019 -        else if (rp.getCombineType() == RefPattern.COMBINE_INTERLEAVE)
  1.1020 -          rp.setPattern(sb.pb.makeInterleave(rp.getPattern(), p));
  1.1021 -        else
  1.1022 -          rp.setPattern(sb.pb.makeChoice(rp.getPattern(), p));
  1.1023 -        break;
  1.1024 -      case RefPattern.REPLACEMENT_REQUIRE:
  1.1025 -        rp.setReplacementStatus(RefPattern.REPLACEMENT_IGNORE);
  1.1026 -        break;
  1.1027 -      case RefPattern.REPLACEMENT_IGNORE:
  1.1028 -        break;
  1.1029 -      }
  1.1030 +    public boolean usesComments() {
  1.1031 +        return false;
  1.1032      }
  1.1033  
  1.1034 -    public void topLevelAnnotation(ParsedElementAnnotation ea) throws BuildException {
  1.1035 +    private void error(SAXParseException message) throws BuildException {
  1.1036 +        noteError();
  1.1037 +        try {
  1.1038 +            if (eh != null) {
  1.1039 +                eh.error(message);
  1.1040 +            }
  1.1041 +        } catch (SAXException e) {
  1.1042 +            throw new BuildException(e);
  1.1043 +        }
  1.1044      }
  1.1045  
  1.1046 -    public void topLevelComment(CommentList comments) throws BuildException {
  1.1047 +    private void error(String key, Locator loc) throws BuildException {
  1.1048 +        error(new SAXParseException(localizer.message(key), loc));
  1.1049      }
  1.1050  
  1.1051 -    private RefPattern lookup(String name) {
  1.1052 -      if (name == START)
  1.1053 -        return startRef;
  1.1054 -      return lookup1(name);
  1.1055 +    private void error(String key, String arg, Locator loc) throws BuildException {
  1.1056 +        error(new SAXParseException(localizer.message(key, arg), loc));
  1.1057      }
  1.1058  
  1.1059 -    private RefPattern lookup1(String name) {
  1.1060 -      RefPattern p = (RefPattern)defines.get(name);
  1.1061 -      if (p == null) {
  1.1062 -        p = new RefPattern(name);
  1.1063 -        defines.put(name, p);
  1.1064 -      }
  1.1065 -      return p;
  1.1066 +    private void error(String key, String arg1, String arg2, Locator loc) throws BuildException {
  1.1067 +        error(new SAXParseException(localizer.message(key, arg1, arg2), loc));
  1.1068      }
  1.1069  
  1.1070 -    public ParsedPattern makeRef(String name, Location loc, Annotations anno) throws BuildException {
  1.1071 -      RefPattern p = lookup1(name);
  1.1072 -      if (p.getRefLocator() == null && loc != null)
  1.1073 -        p.setRefLocator((Locator)loc);
  1.1074 -      return p;
  1.1075 +    private void error(String key, String arg1, String arg2, String arg3, Locator loc) throws BuildException {
  1.1076 +        error(new SAXParseException(localizer.message(key, new Object[]{arg1, arg2, arg3}), loc));
  1.1077      }
  1.1078  
  1.1079 -    public ParsedPattern makeParentRef(String name, Location loc, Annotations anno) throws BuildException {
  1.1080 -        // TODO: do this check by the caller
  1.1081 -      if (parent == null) {
  1.1082 -        sb.error("parent_ref_outside_grammar", (Locator)loc);
  1.1083 -        return sb.makeErrorPattern();
  1.1084 -      }
  1.1085 -      return parent.makeRef(name, loc, anno);
  1.1086 +    private void noteError() {
  1.1087 +        if (!hadError && parent != null) {
  1.1088 +            parent.noteError();
  1.1089 +        }
  1.1090 +        hadError = true;
  1.1091      }
  1.1092 -
  1.1093 -    public Div makeDiv() {
  1.1094 -      return this;
  1.1095 -    }
  1.1096 -
  1.1097 -    public Include makeInclude() {
  1.1098 -      return new IncludeImpl(sb, this);
  1.1099 -    }
  1.1100 -
  1.1101 -  }
  1.1102 -
  1.1103 -
  1.1104 -  static class Override {
  1.1105 -    Override(RefPattern prp, Override next) {
  1.1106 -      this.prp = prp;
  1.1107 -      this.next = next;
  1.1108 -    }
  1.1109 -
  1.1110 -    RefPattern prp;
  1.1111 -    Override next;
  1.1112 -    byte replacementStatus;
  1.1113 -  }
  1.1114 -
  1.1115 -
  1.1116 -  private static class IncludeImpl implements Include, Div {
  1.1117 -    private SchemaBuilderImpl sb;
  1.1118 -    private Override overrides;
  1.1119 -    private GrammarImpl grammar;
  1.1120 -
  1.1121 -    private IncludeImpl(SchemaBuilderImpl sb, GrammarImpl grammar) {
  1.1122 -      this.sb = sb;
  1.1123 -      this.grammar = grammar;
  1.1124 -    }
  1.1125 -
  1.1126 -    public void define(String name, GrammarSection.Combine combine, ParsedPattern pattern, Location loc, Annotations anno)
  1.1127 -            throws BuildException {
  1.1128 -      RefPattern rp = grammar.lookup(name);
  1.1129 -      overrides = new Override(rp, overrides);
  1.1130 -      grammar.define(rp, combine, pattern, loc);
  1.1131 -    }
  1.1132 -
  1.1133 -    public void endDiv(Location loc, Annotations anno) throws BuildException {
  1.1134 -      // nothing to do
  1.1135 -    }
  1.1136 -
  1.1137 -    public void topLevelAnnotation(ParsedElementAnnotation ea) throws BuildException {
  1.1138 -      // nothing to do
  1.1139 -    }
  1.1140 -
  1.1141 -    public void topLevelComment(CommentList comments) throws BuildException {
  1.1142 -    }
  1.1143 -
  1.1144 -    public Div makeDiv() {
  1.1145 -      return this;
  1.1146 -    }
  1.1147 -
  1.1148 -    public void endInclude(Parseable current,String uri, String ns,
  1.1149 -                           Location loc, Annotations anno) throws BuildException {
  1.1150 -      for (OpenIncludes inc = sb.openIncludes;
  1.1151 -           inc != null;
  1.1152 -           inc = inc.parent) {
  1.1153 -        if (inc.uri.equals(uri)) {
  1.1154 -          sb.error("recursive_include", uri, (Locator)loc);
  1.1155 -          return;
  1.1156 -        }
  1.1157 -      }
  1.1158 -
  1.1159 -      for (Override o = overrides; o != null; o = o.next) {
  1.1160 -        o.replacementStatus = o.prp.getReplacementStatus();
  1.1161 -        o.prp.setReplacementStatus(RefPattern.REPLACEMENT_REQUIRE);
  1.1162 -      }
  1.1163 -      try {
  1.1164 -        SchemaBuilderImpl isb = new SchemaBuilderImpl(ns, uri, sb);
  1.1165 -        current.parseInclude(uri, isb, new GrammarImpl(isb, grammar), ns);
  1.1166 -        for (Override o = overrides; o != null; o = o.next) {
  1.1167 -          if (o.prp.getReplacementStatus() == RefPattern.REPLACEMENT_REQUIRE) {
  1.1168 -            if (o.prp.getName() == null)
  1.1169 -              sb.error("missing_start_replacement", (Locator)loc);
  1.1170 -            else
  1.1171 -              sb.error("missing_define_replacement", o.prp.getName(), (Locator)loc);
  1.1172 -          }
  1.1173 -        }
  1.1174 -      }
  1.1175 -      catch (IllegalSchemaException e) {
  1.1176 -        sb.noteError();
  1.1177 -      }
  1.1178 -      finally {
  1.1179 -        for (Override o = overrides; o != null; o = o.next)
  1.1180 -          o.prp.setReplacementStatus(o.replacementStatus);
  1.1181 -      }
  1.1182 -    }
  1.1183 -
  1.1184 -    public Include makeInclude() {
  1.1185 -      return null;
  1.1186 -    }
  1.1187 -  }
  1.1188 -
  1.1189 -  public Grammar makeGrammar(Scope parent) {
  1.1190 -    return new GrammarImpl(this, parent);
  1.1191 -  }
  1.1192 -
  1.1193 -  public ParsedPattern annotate(ParsedPattern p, Annotations anno) throws BuildException {
  1.1194 -    return p;
  1.1195 -  }
  1.1196 -
  1.1197 -
  1.1198 -  public ParsedPattern annotateAfter(ParsedPattern p, ParsedElementAnnotation e) throws BuildException {
  1.1199 -    return p;
  1.1200 -  }
  1.1201 -
  1.1202 -  public ParsedPattern commentAfter(ParsedPattern p, CommentList comments) throws BuildException {
  1.1203 -    return p;
  1.1204 -  }
  1.1205 -
  1.1206 -
  1.1207 -  public ParsedPattern makeExternalRef(Parseable current, String uri, String ns, Scope scope,
  1.1208 -                                       Location loc, Annotations anno)
  1.1209 -          throws BuildException {
  1.1210 -    for (OpenIncludes inc = openIncludes;
  1.1211 -         inc != null;
  1.1212 -         inc = inc.parent) {
  1.1213 -      if (inc.uri.equals(uri)) {
  1.1214 -        error("recursive_include", uri, (Locator)loc);
  1.1215 -        return pb.makeError();
  1.1216 -      }
  1.1217 -    }
  1.1218 -    try {
  1.1219 -      return current.parseExternal(uri, new SchemaBuilderImpl(ns, uri, this), scope, ns );
  1.1220 -    }
  1.1221 -    catch (IllegalSchemaException e) {
  1.1222 -      noteError();
  1.1223 -      return pb.makeError();
  1.1224 -    }
  1.1225 -  }
  1.1226 -
  1.1227 -
  1.1228 -
  1.1229 -  public Location makeLocation(String systemId, int lineNumber, int columnNumber) {
  1.1230 -    return new LocatorImpl(systemId, lineNumber, columnNumber);
  1.1231 -  }
  1.1232 -
  1.1233 -  public Annotations makeAnnotations(CommentList comments, Context context) {
  1.1234 -    return this;
  1.1235 -  }
  1.1236 -
  1.1237 -  public ElementAnnotationBuilder makeElementAnnotationBuilder(String ns, String localName, String prefix,
  1.1238 -                                                               Location loc, CommentList comments, Context context) {
  1.1239 -    return this;
  1.1240 -  }
  1.1241 -
  1.1242 -  public CommentList makeCommentList() {
  1.1243 -    return this;
  1.1244 -  }
  1.1245 -
  1.1246 -  public void addComment(String value, Location loc) throws BuildException {
  1.1247 -  }
  1.1248 -
  1.1249 -  public void addAttribute(String ns, String localName, String prefix, String value, Location loc) {
  1.1250 -    // nothing needed
  1.1251 -  }
  1.1252 -
  1.1253 -  public void addElement(ParsedElementAnnotation ea) {
  1.1254 -    // nothing needed
  1.1255 -  }
  1.1256 -
  1.1257 -  public void addComment(CommentList comments) throws BuildException {
  1.1258 -    // nothing needed
  1.1259 -  }
  1.1260 -
  1.1261 -  public void addLeadingComment(CommentList comments) throws BuildException {
  1.1262 -    // nothing needed
  1.1263 -  }
  1.1264 -
  1.1265 -  public ParsedElementAnnotation makeElementAnnotation() {
  1.1266 -    return null;
  1.1267 -  }
  1.1268 -
  1.1269 -  public void addText(String value, Location loc, CommentList comments) throws BuildException {
  1.1270 -  }
  1.1271 -
  1.1272 -  public boolean usesComments() {
  1.1273 -    return false;
  1.1274 -  }
  1.1275 -
  1.1276 -  private void error(SAXParseException message) throws BuildException {
  1.1277 -    noteError();
  1.1278 -    try {
  1.1279 -      if (eh != null)
  1.1280 -        eh.error(message);
  1.1281 -    }
  1.1282 -    catch (SAXException e) {
  1.1283 -      throw new BuildException(e);
  1.1284 -    }
  1.1285 -  }
  1.1286 -
  1.1287 -  private void warning(SAXParseException message) throws BuildException {
  1.1288 -    try {
  1.1289 -      if (eh != null)
  1.1290 -        eh.warning(message);
  1.1291 -    }
  1.1292 -    catch (SAXException e) {
  1.1293 -      throw new BuildException(e);
  1.1294 -    }
  1.1295 -  }
  1.1296 -
  1.1297 -  private void error(String key, Locator loc) throws BuildException {
  1.1298 -    error(new SAXParseException(localizer.message(key), loc));
  1.1299 -  }
  1.1300 -
  1.1301 -  private void error(String key, String arg, Locator loc) throws BuildException {
  1.1302 -    error(new SAXParseException(localizer.message(key, arg), loc));
  1.1303 -  }
  1.1304 -
  1.1305 -  private void error(String key, String arg1, String arg2, Locator loc) throws BuildException {
  1.1306 -    error(new SAXParseException(localizer.message(key, arg1, arg2), loc));
  1.1307 -  }
  1.1308 -
  1.1309 -  private void error(String key, String arg1, String arg2, String arg3, Locator loc) throws BuildException {
  1.1310 -    error(new SAXParseException(localizer.message(key, new Object[]{arg1, arg2, arg3}), loc));
  1.1311 -  }
  1.1312 -  private void noteError() {
  1.1313 -    if (!hadError && parent != null)
  1.1314 -      parent.noteError();
  1.1315 -    hadError = true;
  1.1316 -  }
  1.1317 -
  1.1318  }

mercurial