diff options
Diffstat (limited to 'src/cuchaz/enigma/mapping/MappingsReader.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/MappingsReader.java | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/mapping/MappingsReader.java b/src/cuchaz/enigma/mapping/MappingsReader.java new file mode 100644 index 0000000..b039409 --- /dev/null +++ b/src/cuchaz/enigma/mapping/MappingsReader.java | |||
| @@ -0,0 +1,129 @@ | |||
| 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.BufferedReader; | ||
| 14 | import java.io.IOException; | ||
| 15 | import java.io.Reader; | ||
| 16 | import java.util.NoSuchElementException; | ||
| 17 | import java.util.Scanner; | ||
| 18 | |||
| 19 | import cuchaz.enigma.Util; | ||
| 20 | |||
| 21 | public class MappingsReader | ||
| 22 | { | ||
| 23 | public Mappings read( Reader in ) | ||
| 24 | throws IOException | ||
| 25 | { | ||
| 26 | return read( new BufferedReader( in ) ); | ||
| 27 | } | ||
| 28 | |||
| 29 | public Mappings read( BufferedReader in ) | ||
| 30 | throws IOException | ||
| 31 | { | ||
| 32 | Mappings mappings = new Mappings(); | ||
| 33 | ClassMapping classMapping = null; | ||
| 34 | MethodMapping methodMapping = null; | ||
| 35 | |||
| 36 | int lineNumber = 0; | ||
| 37 | String line = null; | ||
| 38 | while( ( line = in.readLine() ) != null ) | ||
| 39 | { | ||
| 40 | lineNumber++; | ||
| 41 | |||
| 42 | // strip comments | ||
| 43 | int commentPos = line.indexOf( '#' ); | ||
| 44 | if( commentPos >= 0 ) | ||
| 45 | { | ||
| 46 | line = line.substring( 0, commentPos ); | ||
| 47 | } | ||
| 48 | |||
| 49 | // skip blank lines | ||
| 50 | line = line.trim(); | ||
| 51 | if( line.length() <= 0 ) | ||
| 52 | { | ||
| 53 | continue; | ||
| 54 | } | ||
| 55 | |||
| 56 | Scanner scanner = new Scanner( line ); | ||
| 57 | try | ||
| 58 | { | ||
| 59 | while( scanner.hasNext() ) | ||
| 60 | { | ||
| 61 | // read the first token | ||
| 62 | String token = scanner.next(); | ||
| 63 | |||
| 64 | if( token.equalsIgnoreCase( "CLASS" ) ) | ||
| 65 | { | ||
| 66 | classMapping = readClass( scanner ); | ||
| 67 | mappings.addClassMapping( classMapping ); | ||
| 68 | methodMapping = null; | ||
| 69 | } | ||
| 70 | else if( token.equalsIgnoreCase( "FIELD" ) ) | ||
| 71 | { | ||
| 72 | if( classMapping == null ) | ||
| 73 | { | ||
| 74 | throw new IllegalArgumentException( "Line " + lineNumber + ": Unexpected FIELD entry here!" ); | ||
| 75 | } | ||
| 76 | classMapping.addFieldMapping( readField( scanner ) ); | ||
| 77 | } | ||
| 78 | else if( token.equalsIgnoreCase( "METHOD" ) ) | ||
| 79 | { | ||
| 80 | if( classMapping == null ) | ||
| 81 | { | ||
| 82 | throw new IllegalArgumentException( "Line " + lineNumber + ": Unexpected METHOD entry here!" ); | ||
| 83 | } | ||
| 84 | methodMapping = readMethod( scanner ); | ||
| 85 | classMapping.addMethodMapping( methodMapping ); | ||
| 86 | } | ||
| 87 | else if( token.equalsIgnoreCase( "ARG" ) ) | ||
| 88 | { | ||
| 89 | if( classMapping == null || methodMapping == null ) | ||
| 90 | { | ||
| 91 | throw new IllegalArgumentException( "Line " + lineNumber + ": Unexpected ARG entry here!" ); | ||
| 92 | } | ||
| 93 | methodMapping.addArgumentMapping( readArgument( scanner ) ); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | catch( NoSuchElementException ex ) | ||
| 98 | { | ||
| 99 | throw new IllegalArgumentException( "Line " + lineNumber + ": malformed line!" ); | ||
| 100 | } | ||
| 101 | finally | ||
| 102 | { | ||
| 103 | Util.closeQuietly( scanner ); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | return mappings; | ||
| 108 | } | ||
| 109 | |||
| 110 | private ArgumentMapping readArgument( Scanner scanner ) | ||
| 111 | { | ||
| 112 | return new ArgumentMapping( scanner.nextInt(), scanner.next() ); | ||
| 113 | } | ||
| 114 | |||
| 115 | private ClassMapping readClass( Scanner scanner ) | ||
| 116 | { | ||
| 117 | return new ClassMapping( scanner.next(), scanner.next() ); | ||
| 118 | } | ||
| 119 | |||
| 120 | private FieldMapping readField( Scanner scanner ) | ||
| 121 | { | ||
| 122 | return new FieldMapping( scanner.next(), scanner.next() ); | ||
| 123 | } | ||
| 124 | |||
| 125 | private MethodMapping readMethod( Scanner scanner ) | ||
| 126 | { | ||
| 127 | return new MethodMapping( scanner.next(), scanner.next(), scanner.next(), scanner.next() ); | ||
| 128 | } | ||
| 129 | } | ||