Java tutorial
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. Portions Copyrighted 2012 Daniel * Huss. * * The contents of this file are subject to the terms of either the GNU General Public License * Version 2 only ("GPL") or the Common Development and Distribution License("CDDL") (collectively, * the "License"). You may not use this file except in compliance with the License. You can obtain a * copy of the License at http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. * See the License for the specific language governing permissions and limitations under the * License. When distributing the software, include this License Header Notice in each file and * include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this particular file * as subject to the "Classpath" exception as provided by Sun in the GPL Version 2 section of the * License file that accompanied this code. If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * Contributor(s): * * The Original Software is NetBeans. The Initial Developer of the Original Software is Sun * Microsystems, Inc. Portions Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. * * If you wish your version of this file to be governed by only the CDDL or only the GPL Version 2, * indicate your decision by adding "[Contributor] elects to include this software in this * distribution under the [CDDL or GPL Version 2] license." If you do not indicate a single choice * of license, a recipient has the option to distribute your version of this file under either the * CDDL, the GPL Version 2 or to extend the choice of license to its licensees as provided above. * However, if you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then the * option applies only if the new code is made subject to such option by the copyright holder. */ package de.unentscheidbar.validation.builtin; import javax.annotation.concurrent.Immutable; import org.apache.commons.lang3.StringUtils; import de.unentscheidbar.validation.ValidationMessage; import de.unentscheidbar.validation.ValidationResult; import de.unentscheidbar.validation.swing.Validation; /** * Only accepts strings that are valid host names as specified in <a * href="http://www.ietf.org/rfc/rfc1123.txt">RFC 1123</a>. * * @author Tim Boudreau * @author Daniel Huss */ @Immutable public final class HostNameValidator extends EmptyStringAcceptingValidator { private static final long serialVersionUID = Validation.VERSION; private static final String VALID_LABEL_CHARS = validLabelChars(); private static final HostNameValidator INSTANCE = new HostNameValidator(); public static HostNameValidator instance() { return INSTANCE; } private HostNameValidator() { super(); } private static String validLabelChars() { StringBuilder b = new StringBuilder("-"); for (char c = 'a'; c <= 'z'; c++) { b.append(c); b.append(Character.toUpperCase(c)); } for (char c = '0'; c <= '9'; c++) { b.append(c); } return b.toString(); } public static enum Id implements ValidationMessage.Id { HOST_STARTS_WITH_DOT, HOST_STARTS_OR_ENDS_WITH_HYPHEN, LABEL_TOO_LONG, LABEL_EMPTY, BAD_CHAR_IN_HOSTNAME, HOST_TOO_LONG; } @Override protected void validateNonEmptyString(ValidationResult problems, String model) { /* * #1 Host names must not start with a dot. */ if (model.startsWith(".")) { problems.add(Id.HOST_STARTS_WITH_DOT, model); return; } /* * #2 Host names must not start or end with a hyphen. */ if (model.endsWith("-") || model.startsWith("-")) { problems.add(Id.HOST_STARTS_OR_ENDS_WITH_HYPHEN, model); } /* * #3 The complete host name must not be longer than 255 characters and does not permit * leading or trailing whitespace. */ MayNotContainWhitespaceValidator.instance().validate(problems, model); if (model.length() > 255) { problems.add(Id.HOST_TOO_LONG); } /* * #4 Each label must consist of 1-63 characters. Only ASCII a-z, 0-9, period and hypen are * allowed. */ String[] parts = StringUtils.splitPreserveAllTokens(model, '.'); for (int i = 0; i < parts.length; i++) { validateLabel(problems, parts[i]); } } private void validateLabel(ValidationResult problems, String label) { if (label.length() > 63) { problems.add(Id.LABEL_TOO_LONG, label); } else if (label.length() == 0) { problems.add(Id.LABEL_EMPTY, label); } int idx = StringUtils.indexOfAnyBut(label, VALID_LABEL_CHARS); if (idx != -1) problems.add(Id.BAD_CHAR_IN_HOSTNAME, String.valueOf(label.charAt(idx))); } }