diff options
Diffstat (limited to 'src/cuchaz/enigma/mapping/NameValidator.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/NameValidator.java | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/mapping/NameValidator.java b/src/cuchaz/enigma/mapping/NameValidator.java new file mode 100644 index 0000000..30982dc --- /dev/null +++ b/src/cuchaz/enigma/mapping/NameValidator.java | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2014 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Public License v3.0 | ||
| 5 | * which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/gpl.html | ||
| 7 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.mapping; | ||
| 12 | |||
| 13 | import java.util.regex.Pattern; | ||
| 14 | |||
| 15 | public class NameValidator | ||
| 16 | { | ||
| 17 | private static final String IdentifierPattern; | ||
| 18 | private static final Pattern ClassPattern; | ||
| 19 | |||
| 20 | static | ||
| 21 | { | ||
| 22 | // java allows all kinds of weird characters... | ||
| 23 | StringBuilder startChars = new StringBuilder(); | ||
| 24 | StringBuilder partChars = new StringBuilder(); | ||
| 25 | for( int i = Character.MIN_CODE_POINT; i <= Character.MAX_CODE_POINT; i++ ) | ||
| 26 | { | ||
| 27 | if( Character.isJavaIdentifierStart( i ) ) | ||
| 28 | { | ||
| 29 | startChars.appendCodePoint( i ); | ||
| 30 | } | ||
| 31 | if( Character.isJavaIdentifierPart( i ) ) | ||
| 32 | { | ||
| 33 | partChars.appendCodePoint( i ); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | IdentifierPattern = String.format( "[\\Q%s\\E][\\Q%s\\E]*", startChars.toString(), partChars.toString() ); | ||
| 38 | ClassPattern = Pattern.compile( String.format( "^(%s(\\.|/))*(%s)$", IdentifierPattern, IdentifierPattern ) ); | ||
| 39 | } | ||
| 40 | |||
| 41 | public String validateClassName( String name ) | ||
| 42 | { | ||
| 43 | if( !ClassPattern.matcher( name ).matches() ) | ||
| 44 | { | ||
| 45 | throw new IllegalArgumentException( "Illegal name: " + name ); | ||
| 46 | } | ||
| 47 | |||
| 48 | return classNameToJavaName( name ); | ||
| 49 | } | ||
| 50 | |||
| 51 | public static String fileNameToClassName( String fileName ) | ||
| 52 | { | ||
| 53 | final String suffix = ".class"; | ||
| 54 | |||
| 55 | if( !fileName.endsWith( suffix ) ) | ||
| 56 | { | ||
| 57 | return null; | ||
| 58 | } | ||
| 59 | |||
| 60 | return fileName.substring( 0, fileName.length() - suffix.length() ).replace( "/", "." ); | ||
| 61 | } | ||
| 62 | |||
| 63 | public static String classNameToJavaName( String className ) | ||
| 64 | { | ||
| 65 | return className.replace( ".", "/" ); | ||
| 66 | } | ||
| 67 | } | ||