2 Copyright (C) 2001, 2006 United States Government as represented by
3 the Administrator of the National Aeronautics and Space Administration.
6 package gov
.nasa
.worldwind
.formats
.rpf
;
8 import gov
.nasa
.worldwind
.*;
10 import static java
.util
.logging
.Level
.*;
14 * @version $Id: RpfFrameFilenameUtil.java 1762 2007-05-07 19:43:55Z dcollins $
16 public class RpfFrameFilenameUtil
18 /* [Section 30.6, MIL-C-89038] */
19 /* [Section A.3.6, MIL-PRF-89041A] */
20 public static final char[] BASE34_ALPHABET
=
21 {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
22 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
23 private static char[] charArray
;
25 /* [Section 30.6, MIL-C-89038] */
26 /* [Section A.3.6, MIL-PRF-89041A] */
27 public static char[] base34ValueOf(int i
, char[] dest
, int offset
, int count
)
29 if (dest
== null || dest
.length
< count
)
30 dest
= new char[count
];
31 for (int digit
= count
+ offset
- 1; digit
>= offset
; digit
--)
33 dest
[digit
] = BASE34_ALPHABET
[i
% 34];
39 private static void ensureCharArray(int length
)
41 if (charArray
== null || charArray
.length
< length
)
42 charArray
= new char[length
];
45 public static String
filenameFor(RpfFrameProperties frameProperties
, String rpfDataType
)
47 validateFrameProperties(frameProperties
);
48 if (rpfDataType
== null)
50 String message
= WorldWind
.retrieveErrMsg("nullValue.StringIsNull");
51 WorldWind
.logger().log(FINE
, message
);
52 throw new IllegalArgumentException(message
);
55 if (rpfDataType
.compareToIgnoreCase("CADRG") == 0)
56 filenameForCadrgOrCib(frameProperties
, 5, 2, charArray
);
57 else if (rpfDataType
.compareToIgnoreCase("CIB") == 0)
58 filenameForCadrgOrCib(frameProperties
, 6, 1, charArray
);
61 String message
= WorldWind
.retrieveErrMsg("RpfFrameFilenameUtil.UnknownRpfDataType") + rpfDataType
;
62 WorldWind
.logger().log(FINE
, message
);
63 throw new IllegalArgumentException(message
);
65 return new String(charArray
, 0, 12);
68 public static String
filenameFor(RpfFrameProperties frameProperties
)
70 validateFrameProperties(frameProperties
);
71 return filenameFor(frameProperties
, frameProperties
.dataSeries
.rpfDataType
);
74 /* [Section 30.6, MIL-C-89038] */
75 /* [Section A.3.6, MIL-PRF-89041A] */
76 private static void filenameForCadrgOrCib(RpfFrameProperties frameProperties
, int frameChars
, int versionChars
,
80 base34ValueOf(frameProperties
.frameNumber
, dest
, index
, frameChars
);
82 base34ValueOf(frameProperties
.version
, dest
, index
, versionChars
);
83 index
+= versionChars
;
84 dest
[index
++] = frameProperties
.producer
.id
;
86 frameProperties
.dataSeries
.seriesCode
.getChars(0, 2, dest
, index
);
88 dest
[index
] = frameProperties
.zone
.zoneCode
;
91 /* [Section 30.6, MIL-C-89038] */
92 /* [Section A.3.6, MIL-PRF-89041A] */
93 public static int parseBase34(char[] src
, int offset
, int count
)
96 for (int digit
= offset
; digit
< offset
+ count
; digit
++)
99 char charUpper
= Character
.toUpperCase(src
[digit
]);
100 if (charUpper
>= '0' && charUpper
<= '9')
101 index
= charUpper
- '0';
102 else if (charUpper
>= 'A' && charUpper
<= 'H')
103 index
= 10 + charUpper
- 'A';
104 else if (charUpper
>= 'J' && charUpper
<= 'N')
105 index
= 18 + charUpper
- 'J';
106 else if (charUpper
>= 'P' && charUpper
<= 'Z')
107 index
= 23 + charUpper
- 'P';
110 String message
= WorldWind
.retrieveErrMsg("RpfFrameFilenameUtil.Base34Error");
111 WorldWind
.logger().log(FINE
, message
);
112 throw new IllegalArgumentException(message
);
114 i
= (i
* 34) + index
;
119 public static RpfFrameProperties
parseFilename(String filename
, String rpfDataType
)
121 if (filename
== null || rpfDataType
== null)
123 String message
= WorldWind
.retrieveErrMsg("nullValue.StringIsNull");
124 WorldWind
.logger().log(FINE
, message
);
125 throw new IllegalArgumentException(message
);
127 if (filename
.length() != 12)
129 String message
= WorldWind
.retrieveErrMsg("RpfFrameFilenameUtil.BadFilenameLength") + filename
;
130 WorldWind
.logger().log(FINE
, message
);
131 throw new RpfFrameFilenameFormatException(message
);
134 filename
.getChars(0, 12, charArray
, 0);
135 RpfFrameProperties frameProperties
;
136 if (rpfDataType
.compareToIgnoreCase("CADRG") == 0)
137 frameProperties
= parseFilenameCadrgOrCib(charArray
, 5, 2);
138 else if (rpfDataType
.compareToIgnoreCase("CIB") == 0)
139 frameProperties
= parseFilenameCadrgOrCib(charArray
, 6, 1);
142 String message
= WorldWind
.retrieveErrMsg("RpfFrameFilenameUtil.UnknownRpfDataType") + rpfDataType
;
143 WorldWind
.logger().log(FINE
, message
);
144 throw new IllegalArgumentException(message
);
146 return frameProperties
;
149 public static RpfFrameProperties
parseFilename(String filename
)
151 if (filename
== null)
153 String message
= WorldWind
.retrieveErrMsg("nullValue.StringIsNull");
154 WorldWind
.logger().log(FINE
, message
);
155 throw new IllegalArgumentException(message
);
157 if (filename
.length() != 12)
159 String message
= WorldWind
.retrieveErrMsg("RpfFrameFilenameUtil.BadFilenameLength") + filename
;
160 WorldWind
.logger().log(FINE
, message
);
161 throw new RpfFrameFilenameFormatException(message
);
163 RpfDataSeries dataSeries
;
166 dataSeries
= RpfDataSeries
.dataSeriesFor(filename
.substring(9, 11));
168 catch (EnumConstantNotPresentException e
)
170 String message
= WorldWind
.retrieveErrMsg("RpfFrameFilenameUtil.EnumNotFound");
171 WorldWind
.logger().log(FINE
, message
);
172 throw new RpfFrameFilenameFormatException(message
, e
);
174 return parseFilename(filename
, dataSeries
.rpfDataType
);
177 /* [Section 30.6, MIL-C-89038] */
178 /* [Section A.3.6, MIL-PRF-89041A] */
179 private static RpfFrameProperties
parseFilenameCadrgOrCib(char[] src
, int frameChars
, int versionChars
)
187 frameNumber
= parseBase34(src
, index
, frameChars
);
189 version
= parseBase34(src
, index
, versionChars
);
190 index
+= versionChars
;
192 catch (IllegalArgumentException e
)
194 String message
= WorldWind
.retrieveErrMsg("RpfFrameFilenameUtil.IntegerNotParsed");
195 WorldWind
.logger().log(FINE
, message
);
196 throw new RpfFrameFilenameFormatException(message
, e
);
199 RpfProducer producer
;
200 RpfDataSeries dataSeries
;
204 producer
= RpfProducer
.producerFor(src
[index
]);
206 dataSeries
= RpfDataSeries
.dataSeriesFor(new String(src
, index
, 2));
208 zone
= RpfZone
.zoneFor(src
[index
]);
210 catch (EnumConstantNotPresentException e
)
212 String message
= WorldWind
.retrieveErrMsg("RpfFrameFilenameUtil.EnumNotFound");
213 WorldWind
.logger().log(FINE
, message
);
214 throw new RpfFrameFilenameFormatException(message
, e
);
217 return new RpfFrameProperties(zone
, frameNumber
, dataSeries
, producer
, version
);
220 private static void validateFrameProperties(RpfFrameProperties frameProperties
)
222 if (frameProperties
== null)
224 String message
= WorldWind
.retrieveErrMsg("nullValue.RpfFramePropertiesIsNull");
225 WorldWind
.logger().log(FINE
, message
);
226 throw new IllegalArgumentException(message
);
228 if (frameProperties
.zone
== null)
230 String message
= WorldWind
.retrieveErrMsg("nullValue.RpfZoneIsNull");
231 WorldWind
.logger().log(FINE
, message
);
232 throw new IllegalArgumentException(message
);
234 if (frameProperties
.frameNumber
< 0)
236 String message
= WorldWind
.retrieveErrMsg("RpfFrameProperties.BadFrameNumber")
237 + frameProperties
.frameNumber
;
238 WorldWind
.logger().log(FINE
, message
);
239 throw new IllegalArgumentException(message
);
241 if (frameProperties
.dataSeries
== null)
243 String message
= WorldWind
.retrieveErrMsg("nullValue.RpfDataSeriesIsNull");
244 WorldWind
.logger().log(FINE
, message
);
245 throw new IllegalArgumentException(message
);
247 if (frameProperties
.producer
== null)
249 String message
= WorldWind
.retrieveErrMsg("nullValue.RpfProducerIsNull");
250 WorldWind
.logger().log(FINE
, message
);
251 throw new IllegalArgumentException(message
);
253 if (frameProperties
.version
< 0)
255 String message
= WorldWind
.retrieveErrMsg("RpfFrameProperties.BadVersion")
256 + frameProperties
.version
;
257 WorldWind
.logger().log(FINE
, message
);
258 throw new IllegalArgumentException(message
);