1 /*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
419 public ValidationContext createValidationContext() {
420 return currentContext;
421 }
422
423 public XmlString createXmlString(String value) {
424 if(value==null) return null;
425 else return new XmlString(value,createValidationContext());
426 }
427
428 @Override
429 public void startPrefixMapping( String prefix, String uri ) throws SAXException {
430 super.startPrefixMapping(prefix,uri);
431 currentContext = new Context(prefix,uri,currentContext);
432 }
433 @Override
434 public void endPrefixMapping( String prefix ) throws SAXException {
435 super.endPrefixMapping(prefix);
436 currentContext = currentContext.previous;
437 }
438
439
440
441
442
443 //
444 //
445 // Utility functions
446 //
447 //
448
449
450 /** Parses UName under the given context. */
451 public UName parseUName( String qname ) throws SAXException {
452 int idx = qname.indexOf(':');
453 if(idx<0) {
454 String uri = resolveNamespacePrefix("");
455
456 // chamelon behavior. ugly...
457 if( uri.equals("") && chameleonMode )
458 uri = currentSchema.getTargetNamespace();
459
460 // this is guaranteed to resolve
461 return new UName(uri,qname,qname);
462 } else {
463 String prefix = qname.substring(0,idx);
464 String uri = currentContext.resolveNamespacePrefix(prefix);
465 if(uri==null) {
466 // prefix failed to resolve.
467 reportError(Messages.format(
468 Messages.ERR_UNDEFINED_PREFIX,prefix));
469 uri="undefined"; // replace with a dummy
470 }
471 return new UName( uri, qname.substring(idx+1), qname );
472 }
473 }
474
475 public boolean parseBoolean(String v) {
476 if(v==null) return false;
477 v=v.trim();
478 return v.equals("true") || v.equals("1");
479 }
480
481
482 @Override
483 protected void unexpectedX(String token) throws SAXException {
484 SAXParseException e = new SAXParseException(MessageFormat.format(
485 "Unexpected {0} appears at line {1} column {2}",
486 token,
487 getLocator().getLineNumber(),
488 getLocator().getColumnNumber()),
489 getLocator());
490
491 parser.errorHandler.fatalError(e);
492 throw e; // we will abort anyway
|
1 /*
2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
419 public ValidationContext createValidationContext() {
420 return currentContext;
421 }
422
423 public XmlString createXmlString(String value) {
424 if(value==null) return null;
425 else return new XmlString(value,createValidationContext());
426 }
427
428 @Override
429 public void startPrefixMapping( String prefix, String uri ) throws SAXException {
430 super.startPrefixMapping(prefix,uri);
431 currentContext = new Context(prefix,uri,currentContext);
432 }
433 @Override
434 public void endPrefixMapping( String prefix ) throws SAXException {
435 super.endPrefixMapping(prefix);
436 currentContext = currentContext.previous;
437 }
438
439 //
440 //
441 // Utility functions
442 //
443 //
444
445 /**
446 * Parses UName under the given context.
447 * @param qname Attribute name.
448 * @return New {@link UName} instance based on attribute name.
449 */
450 public UName parseUName(final String qname ) throws SAXException {
451 int idx = qname.indexOf(':');
452 if(idx<0) {
453 String uri = resolveNamespacePrefix("");
454
455 // chamelon behavior. ugly...
456 if( uri.equals("") && chameleonMode )
457 uri = currentSchema.getTargetNamespace();
458
459 // this is guaranteed to resolve
460 return new UName(uri,qname,qname);
461 } else {
462 String prefix = qname.substring(0,idx);
463 String uri = currentContext.resolveNamespacePrefix(prefix);
464 if(uri==null) {
465 // prefix failed to resolve.
466 reportError(Messages.format(
467 Messages.ERR_UNDEFINED_PREFIX,prefix));
468 uri="undefined"; // replace with a dummy
469 }
470 return new UName( uri, qname.substring(idx+1), qname );
471 }
472 }
473
474 /**
475 * Utility function for collapsing the namespaces inside qname declarations
476 * and 'name' attribute values that should contain the qname values
477 *
478 * @param text String where whitespaces should be collapsed
479 * @return String with whitespaces collapsed
480 */
481 public String collapse(String text) {
482 return collapse((CharSequence) text).toString();
483 }
484
485 /**
486 * returns true if the specified char is a white space character.
487 */
488 private final boolean isWhiteSpace(char ch) {
489 // most of the characters are non-control characters.
490 // so check that first to quickly return false for most of the cases.
491 if (ch > 0x20) {
492 return false;
493 }
494
495 // other than we have to do four comparisons.
496 return ch == 0x9 || ch == 0xA || ch == 0xD || ch == 0x20;
497 }
498
499 /**
500 * This is usually the biggest processing bottleneck.
501 *
502 */
503 private CharSequence collapse(CharSequence text) {
504 int len = text.length();
505
506 // most of the texts are already in the collapsed form.
507 // so look for the first whitespace in the hope that we will
508 // never see it.
509 int s = 0;
510 while (s < len) {
511 if (isWhiteSpace(text.charAt(s))) {
512 break;
513 }
514 s++;
515 }
516 if (s == len) // the input happens to be already collapsed.
517 {
518 return text;
519 }
520
521 // we now know that the input contains spaces.
522 // let's sit down and do the collapsing normally.
523 StringBuilder result = new StringBuilder(len /*allocate enough size to avoid re-allocation*/);
524
525 if (s != 0) {
526 for (int i = 0; i < s; i++) {
527 result.append(text.charAt(i));
528 }
529 result.append(' ');
530 }
531
532 boolean inStripMode = true;
533 for (int i = s + 1; i < len; i++) {
534 char ch = text.charAt(i);
535 boolean b = isWhiteSpace(ch);
536 if (inStripMode && b) {
537 continue; // skip this character
538 }
539 inStripMode = b;
540 if (inStripMode) {
541 result.append(' ');
542 } else {
543 result.append(ch);
544 }
545 }
546
547 // remove trailing whitespaces
548 len = result.length();
549 if (len > 0 && result.charAt(len - 1) == ' ') {
550 result.setLength(len - 1);
551 }
552 // whitespaces are already collapsed,
553 // so all we have to do is to remove the last one character
554 // if it's a whitespace.
555
556 return result;
557 }
558
559 public boolean parseBoolean(String v) {
560 if(v==null) return false;
561 v=v.trim();
562 return v.equals("true") || v.equals("1");
563 }
564
565
566 @Override
567 protected void unexpectedX(String token) throws SAXException {
568 SAXParseException e = new SAXParseException(MessageFormat.format(
569 "Unexpected {0} appears at line {1} column {2}",
570 token,
571 getLocator().getLineNumber(),
572 getLocator().getColumnNumber()),
573 getLocator());
574
575 parser.errorHandler.fatalError(e);
576 throw e; // we will abort anyway
|