001/* 002 * Copyright 2015-2024 the original author or authors 003 * 004 * This software is licensed under the Apache License, Version 2.0, 005 * the GNU Lesser General Public License version 2 or later ("LGPL") 006 * and the WTFPL. 007 * You may choose either license to govern your use of this software only 008 * upon the condition that you accept all of the terms of either 009 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL. 010 */ 011package org.minidns.dnslabel; 012 013import java.util.Locale; 014 015import org.minidns.idna.MiniDnsIdna; 016 017/** 018 * A label that begins with "xn--" and follows the LDH rule. 019 */ 020public abstract class XnLabel extends ReservedLdhLabel { 021 022 protected XnLabel(String label) { 023 super(label); 024 } 025 026 protected static LdhLabel fromInternal(String label) { 027 assert isIdnAcePrefixed(label); 028 029 String uLabel = MiniDnsIdna.toUnicode(label); 030 if (label.equals(uLabel)) { 031 // No Punycode conversation to Unicode was performed, this is a fake A-label! 032 return new FakeALabel(label); 033 } else { 034 return new ALabel(label); 035 } 036 } 037 038 public static boolean isXnLabel(String label) { 039 if (!isReservedLdhLabel(label)) { 040 return false; 041 } 042 return isXnLabelInternal(label); 043 } 044 045 static boolean isXnLabelInternal(String label) { 046 // Note that we already ensure the minimum label length here, since reserved LDH 047 // labels must start with "xn--". 048 return label.substring(0, 2).toLowerCase(Locale.US).equals("xn"); 049 } 050}