jaxws/src/jdk.xml.bind/share/classes/com/sun/xml/internal/xsom/impl/parser/NGCCRuntimeEx.java
changeset 42124 640a383428fb
parent 32904 42076a665ea1
child 43852 93a527059d8a
equal deleted inserted replaced
42002:3ee4e7827413 42124:640a383428fb
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   434     public void endPrefixMapping( String prefix ) throws SAXException {
   434     public void endPrefixMapping( String prefix ) throws SAXException {
   435         super.endPrefixMapping(prefix);
   435         super.endPrefixMapping(prefix);
   436         currentContext = currentContext.previous;
   436         currentContext = currentContext.previous;
   437     }
   437     }
   438 
   438 
   439 
       
   440 
       
   441 
       
   442 
       
   443 //
   439 //
   444 //
   440 //
   445 // Utility functions
   441 // Utility functions
   446 //
   442 //
   447 //
   443 //
   448 
   444 
   449 
   445     /**
   450     /** Parses UName under the given context. */
   446      * Parses UName under the given context.
   451     public UName parseUName( String qname ) throws SAXException {
   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 {
   452         int idx = qname.indexOf(':');
   451         int idx = qname.indexOf(':');
   453         if(idx<0) {
   452         if(idx<0) {
   454             String uri = resolveNamespacePrefix("");
   453             String uri = resolveNamespacePrefix("");
   455 
   454 
   456             // chamelon behavior. ugly...
   455             // chamelon behavior. ugly...
   470             }
   469             }
   471             return new UName( uri, qname.substring(idx+1), qname );
   470             return new UName( uri, qname.substring(idx+1), qname );
   472         }
   471         }
   473     }
   472     }
   474 
   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 
   475     public boolean parseBoolean(String v) {
   559     public boolean parseBoolean(String v) {
   476         if(v==null) return false;
   560         if(v==null) return false;
   477         v=v.trim();
   561         v=v.trim();
   478         return v.equals("true") || v.equals("1");
   562         return v.equals("true") || v.equals("1");
   479     }
   563     }