11package mil .nga .crs .wkt ;
22
33import java .io .IOException ;
4+ import java .util .Scanner ;
45
56import mil .nga .crs .CRS ;
67import mil .nga .crs .CRSException ;
@@ -26,6 +27,11 @@ public class CRSPretty {
2627 */
2728 private static final String HELP_ARG = "-help" ;
2829
30+ /**
31+ * Command prompt
32+ */
33+ public static final String COMMAND_PROMPT = "wkt> " ;
34+
2935 /**
3036 * Main method to generate tiles in a GeoPackage
3137 *
@@ -36,37 +42,126 @@ public class CRSPretty {
3642 */
3743 public static void main (String [] args ) throws IOException {
3844
45+ boolean help = false ;
3946 StringBuilder builder = new StringBuilder ();
4047
41- if (args .length > 0 && ! args [ 0 ]. equalsIgnoreCase ( HELP_ARG ) ) {
48+ if (args .length > 0 ) {
4249
43- for (int i = 0 ; i < args .length ; i ++) {
50+ if (args [0 ].equalsIgnoreCase (HELP_ARG )) {
51+ help = true ;
52+ } else {
4453
45- String [] lines = args [ i ]. trim (). split ( " \n " );
54+ for ( int i = 0 ; i < args . length ; i ++) {
4655
47- for ( int j = 0 ; j < lines . length ; j ++) {
56+ String [] lines = args [ i ]. trim (). split ( " \n " );
4857
49- String line = lines [j ].trim ();
50- if (line .endsWith ("\\ " ) || line .endsWith ("/" )) {
51- line = line .substring (0 , line .length () - 1 );
52- }
58+ for (int j = 0 ; j < lines .length ; j ++) {
5359
54- builder .append (line );
60+ String line = lines [j ].trim ();
61+ if (line .endsWith ("\\ " ) || line .endsWith ("/" )) {
62+ line = line .substring (0 , line .length () - 1 );
63+ }
64+
65+ builder .append (line );
66+
67+ }
5568
5669 }
5770
5871 }
5972
6073 }
6174
62- if (builder . length () == 0 ) {
75+ if (help ) {
6376 printUsage ();
77+ } else if (builder .length () == 0 ) {
78+ commandPrompt ();
6479 } else {
6580 parseAndPrint (builder .toString ());
6681 }
6782
6883 }
6984
85+ /**
86+ * Command prompt accepting well-known text
87+ */
88+ private static void commandPrompt () {
89+
90+ Scanner scanner = new Scanner (System .in );
91+ try {
92+ System .out .println ();
93+ System .out .println (
94+ "Enter OGC Coordinate Reference System Well-Known Text" );
95+ System .out .println (
96+ "* Parsing occurs when closing brackets ']' match or exceed opening brackets '['" );
97+
98+ StringBuilder wktBuilder = new StringBuilder ();
99+ resetCommandPrompt (wktBuilder );
100+ int openBrackets = 0 ;
101+ int closeBrackets = 0 ;
102+
103+ while (scanner .hasNextLine ()) {
104+ try {
105+ String line = scanner .nextLine ().trim ();
106+ wktBuilder .append (line );
107+
108+ openBrackets += line .length ()
109+ - line .replaceAll ("\\ [" , "" ).length ();
110+ closeBrackets += line .length ()
111+ - line .replaceAll ("]" , "" ).length ();
112+
113+ if (closeBrackets >= openBrackets && closeBrackets > 0 ) {
114+
115+ String wkt = wktBuilder .toString ().trim ();
116+ char firstChar = wkt .charAt (0 );
117+ char lastChar = wkt .charAt (wkt .length () - 1 );
118+ if (isQuoteCharacter (firstChar )
119+ && firstChar == lastChar ) {
120+ wkt = wkt .substring (1 , wkt .length () - 1 ).trim ();
121+ }
122+
123+ parseAndPrint (wkt );
124+ resetCommandPrompt (wktBuilder );
125+
126+ openBrackets = 0 ;
127+ closeBrackets = 0 ;
128+
129+ }
130+
131+ } catch (Exception e ) {
132+ System .out .println (e );
133+ resetCommandPrompt (wktBuilder );
134+ }
135+ }
136+ } finally {
137+ scanner .close ();
138+ }
139+
140+ }
141+
142+ /**
143+ * Reset the command prompt
144+ *
145+ * @param builder
146+ * string builder
147+ */
148+ private static void resetCommandPrompt (StringBuilder builder ) {
149+ builder .setLength (0 );
150+ System .out .println ();
151+ System .out .print (COMMAND_PROMPT );
152+ }
153+
154+ /**
155+ * Check if the character is a quote character
156+ *
157+ * @param c
158+ * character
159+ * @return true if quote character
160+ */
161+ public static boolean isQuoteCharacter (char c ) {
162+ return c == '\'' || TextReader .isQuoteCharacter (c );
163+ }
164+
70165 /**
71166 * Project coordinates
72167 *
@@ -114,7 +209,7 @@ public static void parseAndPrint(String wkt) throws IOException {
114209 System .out .println ("Failed to parse Coordinate Reference System" );
115210 System .out .println ("-------------------------------------------" );
116211 System .out .println ();
117- e .printStackTrace ();
212+ e .printStackTrace (System . out );
118213 System .out .println ();
119214
120215 }
0 commit comments