Skip to content

Commit fdc52b3

Browse files
committed
Pretty command line utility command prompt session mode
1 parent 2e85d32 commit fdc52b3

4 files changed

Lines changed: 130 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Adheres to [Semantic Versioning](http://semver.org/).
88

99
* CRS Reader ignore non whitespace space characters such as 'NO-BREAK SPACE'
1010
* CRS Reader error handling while reading separator when no tokens remain
11+
* Pretty command line utility command prompt session mode
1112

1213
## [1.1.2](https://github.com/ngageoint/coordinate-reference-systems-java/releases/tag/1.1.2) (02-08-2022)
1314

script/pretty/README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ Parse OGC Coordinate Reference System Well-Known Text (1|2) and pretty print Coo
88

99
### Script
1010

11-
./pretty.sh well-known_text
11+
./pretty.sh [well-known_text]
1212

1313
### Jar
1414

15-
java -jar pretty.jar well-known_text
15+
java -jar pretty.jar [well-known_text]
1616

1717
### Alias
1818

@@ -22,7 +22,7 @@ Add an alias to your shell to run from any location
2222

2323
And run
2424

25-
pretty well-known_text
25+
pretty [well-known_text]
2626

2727
## Examples
2828

@@ -53,6 +53,14 @@ Run using the script, Jar, or alias.
5353
EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"],\
5454
AUTHORITY["EPSG","3857"]]'
5555

56+
pretty
57+
wkt> GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0]]
58+
59+
wkt> GEOGCS["WGS 84",
60+
DATUM["WGS_1984",
61+
SPHEROID["WGS 84",6378137,298.257223563]],
62+
PRIMEM["Greenwich",0]]
63+
5664
## Help
5765

5866
```
@@ -68,3 +76,12 @@ DESCRIPTION
6876
Coordinate Reference System Well-Known Text
6977
Example: 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0]]'
7078
```
79+
80+
### Interactive Session
81+
82+
```
83+
Enter OGC Coordinate Reference System Well-Known Text
84+
* Parsing occurs when closing brackets ']' match or exceed opening brackets '['
85+
86+
wkt>
87+
```

src/main/java/mil/nga/crs/wkt/CRSPretty.java

Lines changed: 106 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mil.nga.crs.wkt;
22

33
import java.io.IOException;
4+
import java.util.Scanner;
45

56
import mil.nga.crs.CRS;
67
import 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
}

src/main/java/mil/nga/crs/wkt/TextReader.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ public int readUnsignedInteger() throws IOException {
429429
* character
430430
* @return true if token character
431431
*/
432-
private static boolean isTokenCharacter(char c) {
432+
public static boolean isTokenCharacter(char c) {
433433
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
434434
|| (c >= '0' && c <= '9') || c == '-' || c == '.' || c == '+'
435435
|| c == ':' || c == '_';
@@ -442,7 +442,7 @@ private static boolean isTokenCharacter(char c) {
442442
* character
443443
* @return true if quote character
444444
*/
445-
private static boolean isQuoteCharacter(char c) {
445+
public static boolean isQuoteCharacter(char c) {
446446
boolean quote = c == '"';
447447
if (!quote) {
448448
int type = Character.getType(c);
@@ -459,7 +459,7 @@ private static boolean isQuoteCharacter(char c) {
459459
* character
460460
* @return true if whitespace
461461
*/
462-
private static boolean isWhitespace(char c) {
462+
public static boolean isWhitespace(char c) {
463463
return Character.isWhitespace(c) || Character.isSpaceChar(c);
464464
}
465465

0 commit comments

Comments
 (0)