Mega Code Archive

 
Categories / Java Tutorial / Network
 

Find a point in URI where URI can be split into a namespace part and a local name

/*  * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.  *  * Licensed under the Aduna BSD-style license.  */ public class Utils {   /**    * Tries to find a point in the supplied URI where this URI can be safely    * split into a namespace part and a local name. According to the XML    * specifications, a local name must start with a letter or underscore and    * can be followed by zero or more 'NCName' characters.    *    * @param uri The URI to split.    * @return The index of the first character of the local name, or    * <tt>-1</tt> if the URI can not be split into a namespace and local name.    */   public static int findURISplitIndex(String uri) {     int uriLength = uri.length();     // Search last character that is not an NCName character     int i = uriLength - 1;     while (i >= 0) {       char c = uri.charAt(i);       // Check for # and / characters explicitly as these       // are used as the end of a namespace very frequently       if (c == '#' || c == '/' || !XMLUtil.isNCNameChar(c)) {         // Found it at index i         break;       }       i--;     }     // Character after the just found non-NCName character could     // be an NCName character, but not a letter or underscore.     // Skip characters that are not letters or underscores.     i++;     while (i < uriLength) {       char c = uri.charAt(i);       if (c == '_' || XMLUtil.isLetter(c)) {         break;       }       i++;     }     // Check that a legal split point has been found     if (i == uriLength) {       i = -1;     }     return i;   } }