diff options
Diffstat (limited to 'src/cuchaz/enigma/mapping/SignatureUpdater.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/SignatureUpdater.java | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/mapping/SignatureUpdater.java b/src/cuchaz/enigma/mapping/SignatureUpdater.java new file mode 100644 index 0000000..4c0dbac --- /dev/null +++ b/src/cuchaz/enigma/mapping/SignatureUpdater.java | |||
| @@ -0,0 +1,87 @@ | |||
| 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.io.IOException; | ||
| 14 | import java.io.StringReader; | ||
| 15 | |||
| 16 | public class SignatureUpdater | ||
| 17 | { | ||
| 18 | public interface ClassNameUpdater | ||
| 19 | { | ||
| 20 | String update( String className ); | ||
| 21 | } | ||
| 22 | |||
| 23 | public static String update( String signature, ClassNameUpdater updater ) | ||
| 24 | { | ||
| 25 | try | ||
| 26 | { | ||
| 27 | StringBuilder buf = new StringBuilder(); | ||
| 28 | |||
| 29 | // read the signature character-by-character | ||
| 30 | StringReader reader = new StringReader( signature ); | ||
| 31 | int i = -1; | ||
| 32 | while( ( i = reader.read() ) != -1 ) | ||
| 33 | { | ||
| 34 | char c = (char)i; | ||
| 35 | |||
| 36 | // does this character start a class name? | ||
| 37 | if( c == 'L' ) | ||
| 38 | { | ||
| 39 | // update the class name and add it to the buffer | ||
| 40 | buf.append( 'L' ); | ||
| 41 | String className = readClass( reader ); | ||
| 42 | if( className == null ) | ||
| 43 | { | ||
| 44 | throw new IllegalArgumentException( "Malformed signature: " + signature ); | ||
| 45 | } | ||
| 46 | buf.append( updater.update( className ) ); | ||
| 47 | buf.append( ';' ); | ||
| 48 | } | ||
| 49 | else | ||
| 50 | { | ||
| 51 | // copy the character into the buffer | ||
| 52 | buf.append( c ); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | return buf.toString(); | ||
| 57 | } | ||
| 58 | catch( IOException ex ) | ||
| 59 | { | ||
| 60 | // I'm pretty sure a StringReader will never throw one of these | ||
| 61 | throw new Error( ex ); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | private static String readClass( StringReader reader ) | ||
| 66 | throws IOException | ||
| 67 | { | ||
| 68 | // read all the characters in the buffer until we hit a ';' | ||
| 69 | StringBuilder buf = new StringBuilder(); | ||
| 70 | int i = -1; | ||
| 71 | while( ( i = reader.read() ) != -1 ) | ||
| 72 | { | ||
| 73 | char c = (char)i; | ||
| 74 | |||
| 75 | if( c == ';' ) | ||
| 76 | { | ||
| 77 | return buf.toString(); | ||
| 78 | } | ||
| 79 | else | ||
| 80 | { | ||
| 81 | buf.append( c ); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | return null; | ||
| 86 | } | ||
| 87 | } | ||