|
1 |
| package com.agical.rdoc.core.service.impl; |
|
2 |
| |
|
3 |
| import java.io.IOException; |
|
4 |
| import java.io.Reader; |
|
5 |
| import java.io.StringReader; |
|
6 |
| import java.io.StringWriter; |
|
7 |
| import java.io.Writer; |
|
8 |
| |
|
9 |
| import com.agical.rdoc.core.TDDocService; |
|
10 |
| import com.agical.rdoc.core.service.NamingService; |
|
11 |
| |
|
12 |
| public class Naming extends TDDocService implements NamingService { |
|
13 |
| |
|
14 |
| |
|
15 |
8
| public String convertName(String input) {
|
|
16 |
8
| Reader inputReader = new StringReader(input);
|
|
17 |
8
| try {
|
|
18 |
8
| return new Parser().parse(inputReader);
|
|
19 |
| } catch (IOException e) { |
|
20 |
0
| return input;
|
|
21 |
| } |
|
22 |
| } |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
4
| public String convertName(String input, String stripLeading) {
|
|
27 |
4
| if (input.startsWith(stripLeading)) {
|
|
28 |
4
| return convertName(input.substring(stripLeading.length()));
|
|
29 |
| } |
|
30 |
| else { |
|
31 |
0
| return convertName(input);
|
|
32 |
| } |
|
33 |
| } |
|
34 |
| |
|
35 |
1
| public String simpleName(String input) {
|
|
36 |
1
| return input.substring(input.lastIndexOf('.')+1);
|
|
37 |
| } |
|
38 |
| |
|
39 |
| } |
|
40 |
| |
|
41 |
| class Parser { |
|
42 |
| private final ParserState INITIAL_STATE = new InitialState(); |
|
43 |
| private final ParserState IN_WORD_STATE = new InWordState(); |
|
44 |
| private final ParserState IN_VERBATIM_STATE = new InVerbatimState(); |
|
45 |
| private ParserState currentState = INITIAL_STATE; |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| private class InitialState implements ParserState { |
|
50 |
| |
|
51 |
| |
|
52 |
8
| public void parse(int input, Writer output) throws IOException {
|
|
53 |
8
| if ('_' == input) {
|
|
54 |
4
| currentState = IN_VERBATIM_STATE;
|
|
55 |
| } |
|
56 |
| else { |
|
57 |
4
| output.write((char) input);
|
|
58 |
4
| currentState = IN_WORD_STATE;
|
|
59 |
| } |
|
60 |
| } |
|
61 |
| } |
|
62 |
| |
|
63 |
| private class InWordState implements ParserState { |
|
64 |
52
| public void parse(int input, Writer output) throws IOException {
|
|
65 |
52
| if (input == '_') {
|
|
66 |
2
| output.write(' ');
|
|
67 |
2
| currentState = IN_VERBATIM_STATE;
|
|
68 |
| } |
|
69 |
50
| else if(!Character.isUpperCase((char) input)) {
|
|
70 |
42
| output.write((char) input);
|
|
71 |
| } |
|
72 |
| else { |
|
73 |
8
| output.write(' ');
|
|
74 |
8
| output.write(Character.toLowerCase((char) input));
|
|
75 |
| } |
|
76 |
| } |
|
77 |
| } |
|
78 |
| |
|
79 |
| private class InVerbatimState implements ParserState { |
|
80 |
58
| public void parse(int input, Writer output) throws IOException {
|
|
81 |
58
| if(input == '_') {
|
|
82 |
4
| currentState = IN_WORD_STATE;
|
|
83 |
| } |
|
84 |
| else { |
|
85 |
54
| output.write(input);
|
|
86 |
| } |
|
87 |
| } |
|
88 |
| } |
|
89 |
| |
|
90 |
8
| public String parse(Reader inputReader) throws IOException {
|
|
91 |
8
| StringWriter outputWriter = new StringWriter();
|
|
92 |
8
| int ch;
|
|
93 |
?
| while ((ch = inputReader.read()) != -1) {
|
|
94 |
118
| currentState.parse(ch , outputWriter);
|
|
95 |
| } |
|
96 |
8
| return outputWriter.toString();
|
|
97 |
| } |
|
98 |
| } |
|
99 |
| |
|
100 |
| interface ParserState { |
|
101 |
| void parse(int input, Writer output) throws IOException; |
|
102 |
| } |
|
103 |
| |