Skip to content

Method: getContent()

1: package de.fhdw.wtf.generator.transformer.clipper.internal;
2:
3: import java.util.ArrayList;
4: import java.util.List;
5:
6: import de.fhdw.wtf.generator.transformer.clipper.ClipperUtils;
7: import de.fhdw.wtf.generator.transformer.exception.ClipperImportFormatException;
8:
9: /**
10: *
11: * Diese Klasse analysiert eine Java-Source-Datei und schneidet die darin enthaltenen inneren Klassen/Interfaces heraus.
12: *
13: * @author hfw413hy
14: *
15: */
16: public class ClipperJavaFileShadowCopy {
17:         
18:         /**
19:          * Enthält den Inhalt einer Klasse/Interface zwischen den geschweiften Klammern.
20:          */
21:         protected String content = "";
22:         
23:         /**
24:          * Enthält den kompletten Text einer Klasse oder eines Interfaces.
25:          */
26:         protected String fullClass = "";
27:         
28:         /**
29:          * Enthält den Typ ("class", "interface").
30:          */
31:         protected String type = "";
32:         
33:         /**
34:          * Enthält den zugewiesenen Namen (Person, Person$Impl, etc.).
35:          */
36:         protected String name = "";
37:         
38:         /**
39:          * Enthält die Klassen/Interfaces die innerhalb einer Klasse/eines Interfaces definiert wurden.
40:          */
41:         protected List<ClipperJavaFileShadowCopy> innerClasses = new ArrayList<>();
42:         
43:         /**
44:          * Erzeugt eine bestimmte Anzahl an Leerzeichen.
45:          *
46:          * @param count
47:          * Anzahl der zu erzeugenden Leerzeichen.
48:          * @return String mit <count> Leerzeichen
49:          */
50:         private String getWhitespaces(final int count) {
51:                 return new String(new char[count]).replace('\0', ' ');
52:         }
53:         
54:         /**
55:          * Liefert den Typ zurück.
56:          *
57:          * @return Der Typ
58:          */
59:         public String getType() {
60:                 return this.type;
61:         }
62:         
63:         /**
64:          * Setzt den Typ.
65:          *
66:          * @param type
67:          */
68:         public void setType(final String type) {
69:                 this.type = type;
70:         }
71:         
72:         /**
73:          * Liefert den Namen zurück.
74:          *
75:          * @return Der Name
76:          */
77:         public String getName() {
78:                 return this.name;
79:         }
80:         
81:         /**
82:          * Setzt den Namen.
83:          *
84:          * @param name
85:          */
86:         public void setName(final String name) {
87:                 this.name = name;
88:         }
89:         
90:         /**
91:          * Liefert den Inhalt einer Klasse/Eines Interfaces.
92:          *
93:          * @return Der Inhalt zwischen den geschweiften Klammern
94:          */
95:         public String getContent() {
96:                 return this.content;
97:         }
98:         
99:         /**
100:          * Liefert eine komplette Klasse, nur ohne ihre "inneren Klassen".
101:          *
102:          * @return Die komplette Klasse
103:          */
104:         public String getFullClass() {
105:                 return this.fullClass;
106:         }
107:         
108:         /**
109:          * Eine Liste an Klassen/Interfaces die in dieser Klasse/diesem Interface definiert wurden.
110:          *
111:          * @return Die Liste an inneren Klassen
112:          */
113:         public List<ClipperJavaFileShadowCopy> getInnerClasses() {
114:                 return this.innerClasses;
115:         }
116:         
117:         /**
118:          * Starts the analysis of inner classes.
119:          *
120:          * @param baseClassContent
121:          * @throws ClipperImportFormatException
122:          */
123:         public void analyze(final String baseClassContent) throws ClipperImportFormatException {
124:                 final String tmpCopy = ClipperUtils.simplifyJavaCode(baseClassContent);
125:                 
126:                 if (baseClassContent.indexOf("class ") > -1 || baseClassContent.indexOf("interface ") > -1) {
127:                         // Diesen Teil nur ausführen, wenn wir "class" oder "interface" gefunden haben!
128:                         this.parseType(tmpCopy);
129:                         this.parseName(tmpCopy);
130:                         
131:                         final int idxOpen = tmpCopy.indexOf('{');
132:                         final int idxClose = ClipperUtils.findCorrespondingClosingCurlyBracket(idxOpen, tmpCopy);
133:                         
134:                         final String strContentOfThisClass = baseClassContent.substring(idxOpen + 1, idxClose);
135:                         
136:                         this.content = this.parseInnerClasses(strContentOfThisClass);
137:                         this.fullClass = baseClassContent.substring(0, idxOpen + 1) + "\n" + this.content + "}";
138:                 } else {
139:                         // Diesen Teil ausführen, wenn beispielsweise nur Kommentare in der Datei enthalten sind.
140:                         // Sonst kann es dazu kommen, dass in "parseType" oder "parseName" eine Exception geworfen wird!
141:                         this.fullClass = baseClassContent;
142:                 }
143:         }
144:         
145:         /**
146:          *
147:          * @param baseClassContent
148:          * @return Returns the direct content of this class/interface and parses the containing classes/interfaces
149:          * @throws ClipperImportFormatException
150:          */
151:         private String parseInnerClasses(final String baseClassContent) throws ClipperImportFormatException {
152:                 String result = baseClassContent;
153:                 String resultShadow = ClipperUtils.simplifyJavaCode(baseClassContent);
154:                 int idxOpen = resultShadow.indexOf('{');
155:                 while (idxOpen > -1) {
156:                         final int idxClose = ClipperUtils.findCorrespondingClosingCurlyBracket(idxOpen, resultShadow);
157:                         final int idxNL = resultShadow.substring(0, idxOpen).lastIndexOf('\n');
158:                         
159:                         final String subClass = result.substring(idxNL + 1, idxClose + 1);
160:                         result = result.substring(0, idxNL) // + this.getWhitespaces(subClass.length())
161:                                         + result.substring(idxClose + 1);
162:                         resultShadow = resultShadow.substring(0, idxNL) // + this.getWhitespaces(subClass.length())
163:                                         + resultShadow.substring(idxClose + 1);
164:                         idxOpen = resultShadow.indexOf('{');
165:                         
166:                         final ClipperJavaFileShadowCopy inner = new ClipperJavaFileShadowCopy();
167:                         inner.analyze(subClass);
168:                         this.getInnerClasses().add(inner);
169:                 }
170:                 
171:                 return result;
172:         }
173:         
174:         /**
175:          * Parses the name of the first class/interface in the given shadowFile.
176:          *
177:          * @param shadowFile
178:          * @throws ClipperImportFormatException
179:          */
180:         private void parseName(final String shadowFile) throws ClipperImportFormatException {
181:                 final int idxOpen = shadowFile.indexOf('{');
182:                 String subStr =
183:                                 shadowFile.substring(shadowFile.indexOf(this.getType()) + this.getType().length(), idxOpen).trim();
184:                 if (subStr.contains(" ")) {
185:                         subStr = subStr.split(" ")[0];
186:                 }
187:                 this.setName(subStr);
188:         }
189:         
190:         /**
191:          * Parses the type (if it is class or interface) of the first class/interface in the given shadowFile.
192:          *
193:          * @param shadowFile
194:          * @throws ClipperImportFormatException
195:          */
196:         private void parseType(final String shadowFile) throws ClipperImportFormatException {
197:                 final int idxOpen = shadowFile.indexOf('{');
198:                 final int idxInterface = shadowFile.substring(0, idxOpen).indexOf(" interface ");
199:                 final int idxClass = shadowFile.substring(0, idxOpen).indexOf(" class ");
200:                 
201:                 if (idxInterface > -1 && idxClass > -1) {
202:                         if (idxInterface > idxClass) {
203:                                 this.setType("class");
204:                         } else {
205:                                 this.setType("interface");
206:                         }
207:                 } else if (idxInterface > -1 && idxClass == -1) {
208:                         this.setType("interface");
209:                 } else if (idxClass > -1 && idxInterface == -1) {
210:                         this.setType("class");
211:                 } else if (idxClass == -1 && idxInterface == -1) {
212:                         throw new ClipperImportFormatException("Bezeichnung 'class' und 'interface' nicht gefunden!");
213:                 }
214:         }
215:         
216: }