Java tutorial
//package com.java2s; /* This file is part of the OWL API. * The contents of this file are subject to the LGPL License, Version 3.0. * Copyright 2014, The University of Manchester * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/. * * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import javax.annotation.Nullable; public class Main { /** * Determines if a character sequence is an NCName (Non-Colonised Name). An * NCName is a string which starts with an NCName start character and is * followed by zero or more NCName characters. * * @param s * The character sequence to be tested. * @return {@code true} if {@code s} is an NCName, otherwise {@code false}. */ public static boolean isNCName(@Nullable CharSequence s) { if (isNullOrEmpty(s)) { return false; } assert s != null; int firstCodePoint = Character.codePointAt(s, 0); if (!isNCNameStartChar(firstCodePoint)) { return false; } for (int i = Character.charCount(firstCodePoint); i < s.length();) { int codePoint = Character.codePointAt(s, i); if (!isNCNameChar(codePoint)) { return false; } i += Character.charCount(codePoint); } return true; } /** * Determines if a character sequence is {@code null} or empty. * * @param s * The character sequence. * @return {@code true} if the character sequence is {@code null}, * {@code true} if the character sequence is empty, otherwise * {@code false}. */ public static boolean isNullOrEmpty(@Nullable CharSequence s) { return s == null || s.length() == 0; } /** * Deterimines if a character is an NCName (Non-Colonised Name) start * character. * * @param codePoint * The code point of the character to be tested. For UTF-8 and UTF-16 * characters the code point corresponds to the value of the char * that represents the character. * @return {@code true} if {@code codePoint} is a NCName start character, * otherwise {@code false}. */ public static boolean isNCNameStartChar(int codePoint) { return codePoint != ':' && isXMLNameStartCharacter(codePoint); } /** * Deterimines if a character is an NCName (Non-Colonised Name) character. * * @param codePoint * The code point of the character to be tested. For UTF-8 and UTF-16 * characters the code point corresponds to the value of the char * that represents the character. * @return {@code true} if {@code codePoint} is a NCName character, * otherwise {@code false}. */ public static boolean isNCNameChar(int codePoint) { return codePoint != ':' && isXMLNameChar(codePoint); } /** * Determines if a character is an XML name start character. * * @param codePoint * The code point of the character to be tested. For UTF-16 * characters the code point corresponds to the value of the char * that represents the character. * @return {@code true} if {@code codePoint} is an XML name start character, * otherwise {@code false} */ public static boolean isXMLNameStartCharacter(int codePoint) { return codePoint == ':' || codePoint >= 'A' && codePoint <= 'Z' || codePoint == '_' || codePoint >= 'a' && codePoint <= 'z' || codePoint >= 0xC0 && codePoint <= 0xD6 || codePoint >= 0xD8 && codePoint <= 0xF6 || codePoint >= 0xF8 && codePoint <= 0x2FF || codePoint >= 0x370 && codePoint <= 0x37D || codePoint >= 0x37F && codePoint <= 0x1FFF || codePoint >= 0x200C && codePoint <= 0x200D || codePoint >= 0x2070 && codePoint <= 0x218F || codePoint >= 0x2C00 && codePoint <= 0x2FEF || codePoint >= 0x3001 && codePoint <= 0xD7FF || codePoint >= 0xF900 && codePoint <= 0xFDCF || codePoint >= 0xFDF0 && codePoint <= 0xFFFD || codePoint >= 0x10000 && codePoint <= 0xEFFFF; } /** * Determines if a character is an XML name character. * * @param codePoint * The code point of the character to be tested. For UTF-8 and UTF-16 * characters the code point corresponds to the value of the char * that represents the character. * @return {@code true} if {@code codePoint} is an XML name start character, * otherwise {@code false} */ public static boolean isXMLNameChar(int codePoint) { return isXMLNameStartCharacter(codePoint) || codePoint == '-' || codePoint == '.' || codePoint >= '0' && codePoint <= '9' || codePoint == 0xB7 || codePoint >= 0x0300 && codePoint <= 0x036F || codePoint >= 0x203F && codePoint <= 0x2040; } }