Skip to content

Commit 1afcdae

Browse files
committed
refactor: code cleanup, introduce generics
1 parent a2ccccd commit 1afcdae

File tree

1 file changed

+51
-56
lines changed

1 file changed

+51
-56
lines changed

modules/xmlbeans-codegen/src/main/java/org/apache/axis2/xmlbeans/CodeGenerationUtility.java

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484
import java.util.Map;
8585
import java.util.Stack;
8686
import java.util.StringTokenizer;
87-
import java.util.Vector;
8887
import java.util.stream.Stream;
8988

9089
/**
@@ -110,7 +109,7 @@ public class CodeGenerationUtility {
110109
* @param additionalSchemas
111110
* @throws RuntimeException
112111
*/
113-
public static TypeMapper processSchemas(List schemas,
112+
public static TypeMapper processSchemas(List<?> schemas,
114113
Element[] additionalSchemas,
115114
CodeGenConfiguration cgconfig,
116115
String typeSystemName) throws RuntimeException {
@@ -125,8 +124,8 @@ public static TypeMapper processSchemas(List schemas,
125124
}
126125

127126
SchemaTypeSystem sts;
128-
List completeSchemaList = new ArrayList();
129-
List topLevelSchemaList = new ArrayList();
127+
List<XmlSchema> completeSchemaList = new ArrayList<>();
128+
List<SchemaDocument> topLevelSchemaList = new ArrayList<>();
130129

131130
//create the type mapper
132131
//First try to take the one that is already there
@@ -139,11 +138,11 @@ public static TypeMapper processSchemas(List schemas,
139138
//xmlbeans specific XMLObject
140139
mapper.setDefaultMappingName(XmlObject.class.getName());
141140

142-
Map nameSpacesMap = new HashMap();
143-
List axisServices = cgconfig.getAxisServices();
141+
Map<String,String> nameSpacesMap = new HashMap<>();
142+
List<AxisService> axisServices = cgconfig.getAxisServices();
144143
AxisService axisService;
145-
for (Iterator iter = axisServices.iterator(); iter.hasNext();) {
146-
axisService = (AxisService)iter.next();
144+
for (Iterator<AxisService> iter = axisServices.iterator(); iter.hasNext();) {
145+
axisService = iter.next();
147146
nameSpacesMap.putAll(axisService.getNamespaceMap());
148147
}
149148

@@ -167,7 +166,7 @@ public static TypeMapper processSchemas(List schemas,
167166
options.setLoadAdditionalNamespaces(
168167
nameSpacesMap); //add the namespaces
169168
topLevelSchemaList.add(
170-
XmlObject.Factory.parse(
169+
(SchemaDocument) XmlObject.Factory.parse(
171170
getSchemaAsString(schema)
172171
, options));
173172

@@ -179,15 +178,15 @@ public static TypeMapper processSchemas(List schemas,
179178
//make the generated code work efficiently
180179
for (int i = 0; i < additionalSchemas.length; i++) {
181180
completeSchemaList.add(extras.read(additionalSchemas[i]));
182-
topLevelSchemaList.add(XmlObject.Factory.parse(
181+
topLevelSchemaList.add((SchemaDocument) XmlObject.Factory.parse(
183182
additionalSchemas[i]
184183
, null));
185184
}
186185

187186
//compile the type system
188187
Axis2EntityResolver er = new Axis2EntityResolver();
189-
er.setSchemas((XmlSchema[])completeSchemaList
190-
.toArray(new XmlSchema[completeSchemaList.size()]));
188+
er.setSchemas(completeSchemaList
189+
.toArray(new XmlSchema[0]));
191190
er.setBaseUri(cgconfig.getBaseURI());
192191

193192
String xsdConfigFile = (String) cgconfig.getProperties().get(XMLBeansExtension.XSDCONFIG_OPTION_LONG);
@@ -241,11 +240,11 @@ public static TypeMapper processSchemas(List schemas,
241240
if (!cgconfig.isParametersWrapped()) {
242241
//figure out the unwrapped operations
243242
axisServices = cgconfig.getAxisServices();
244-
for (Iterator servicesIter = axisServices.iterator(); servicesIter.hasNext();) {
245-
axisService = (AxisService)servicesIter.next();
246-
for (Iterator operations = axisService.getOperations();
243+
for (Iterator<AxisService> servicesIter = axisServices.iterator(); servicesIter.hasNext();) {
244+
axisService = servicesIter.next();
245+
for (Iterator<AxisOperation> operations = axisService.getOperations();
247246
operations.hasNext();) {
248-
AxisOperation op = (AxisOperation)operations.next();
247+
AxisOperation op = operations.next();
249248

250249
if (WSDLUtil.isInputPresentForMEP(op.getMessageExchangePattern())) {
251250
AxisMessage message = op.getMessage(
@@ -350,16 +349,16 @@ public static TypeMapper processSchemas(List schemas,
350349
*
351350
* @param sts
352351
*/
353-
private static List findBase64Types(SchemaTypeSystem sts) {
354-
List allSeenTypes = new ArrayList();
355-
List base64ElementQNamesList = new ArrayList();
352+
private static List<QName> findBase64Types(SchemaTypeSystem sts) {
353+
List<SchemaType> allSeenTypes = new ArrayList<>();
354+
List<QName> base64ElementQNamesList = new ArrayList<>();
356355
SchemaType outerType;
357356
//add the document types and global types
358357
allSeenTypes.addAll(Arrays.asList(sts.documentTypes()));
359358
allSeenTypes.addAll(Arrays.asList(sts.globalTypes()));
360359

361360
for (int i = 0; i < allSeenTypes.size(); i++) {
362-
SchemaType sType = (SchemaType)allSeenTypes.get(i);
361+
SchemaType sType = allSeenTypes.get(i);
363362

364363
if (sType.getContentType() == SchemaType.SIMPLE_CONTENT &&
365364
sType.getPrimitiveType() != null) {
@@ -392,17 +391,17 @@ private static List findBase64Types(SchemaTypeSystem sts) {
392391
* @param sts
393392
* @return array list
394393
*/
395-
private static List findPlainBase64Types(SchemaTypeSystem sts) {
396-
ArrayList allSeenTypes = new ArrayList();
394+
private static List<QName> findPlainBase64Types(SchemaTypeSystem sts) {
395+
ArrayList<SchemaType> allSeenTypes = new ArrayList<>();
397396

398397
allSeenTypes.addAll(Arrays.asList(sts.documentTypes()));
399398
allSeenTypes.addAll(Arrays.asList(sts.globalTypes()));
400399

401-
ArrayList base64Types = new ArrayList();
400+
ArrayList<QName> base64Types = new ArrayList<>();
402401

403-
for (Iterator iterator = allSeenTypes.iterator(); iterator.hasNext();) {
404-
SchemaType stype = (SchemaType)iterator.next();
405-
findPlainBase64Types(stype, base64Types, new ArrayList());
402+
for (Iterator<SchemaType> iterator = allSeenTypes.iterator(); iterator.hasNext();) {
403+
SchemaType stype = iterator.next();
404+
findPlainBase64Types(stype, base64Types, new ArrayList<>());
406405
}
407406

408407
return base64Types;
@@ -413,8 +412,8 @@ private static List findPlainBase64Types(SchemaTypeSystem sts) {
413412
* @param base64Types
414413
*/
415414
private static void findPlainBase64Types(SchemaType stype,
416-
ArrayList base64Types,
417-
ArrayList processedTypes) {
415+
ArrayList<QName> base64Types,
416+
ArrayList<QName> processedTypes) {
418417

419418
SchemaProperty[] elementProperties = stype.getElementProperties();
420419
QName name;
@@ -492,14 +491,19 @@ public Writer createSourceFile(String typename)
492491
file.createNewFile();
493492
return new FileWriter(file);
494493
}
494+
495+
@Override
496+
public Writer createSourceFile(String s, String s1) throws IOException {
497+
return createSourceFile(s);
498+
}
495499
}
496500

497501
/**
498502
* Convert schema into a String
499503
*
500504
* @param schema
501505
*/
502-
private static String getSchemaAsString(XmlSchema schema) throws IOException {
506+
private static String getSchemaAsString(XmlSchema schema) {
503507
StringWriter writer = new StringWriter();
504508
schema.write(writer);
505509
return writer.toString();
@@ -513,15 +517,15 @@ private static String getSchemaAsString(XmlSchema schema) throws IOException {
513517
*/
514518
private static class Axis2BindingConfig extends BindingConfig {
515519

516-
private Map uri2packageMappings = null;
520+
private Map<String, String> uri2packageMappings;
517521
private BindingConfig bindConf = null;
518522

519-
public Axis2BindingConfig(Map uri2packageMappings, String xsdConfigfile, File[] javaFiles,
523+
public Axis2BindingConfig(Map<String, String> uri2packageMappings, String xsdConfigfile, File[] javaFiles,
520524
File[] classpath) {
521525
this.uri2packageMappings = uri2packageMappings;
522526
if (this.uri2packageMappings == null) {
523527
//make an empty one to avoid nasty surprises
524-
this.uri2packageMappings = new HashMap();
528+
this.uri2packageMappings = new HashMap<>();
525529
}
526530

527531
// Do we have an xsdconfig file?
@@ -538,9 +542,9 @@ private BindingConfig buildBindingConfig(String configPath, File[] javaFiles,
538542
SchemaTypeLoader loader = XmlBeans
539543
.typeLoaderForClassLoader(SchemaDocument.class.getClassLoader());
540544
XmlOptions options = new XmlOptions();
541-
options.put(XmlOptions.LOAD_LINE_NUMBERS);
545+
options.setLoadLineNumbers();
542546
// options.setEntityResolver(entResolver); // useless?
543-
Map<String, String> MAP_COMPATIBILITY_CONFIG_URIS = new HashMap<String, String>();
547+
Map<String, String> MAP_COMPATIBILITY_CONFIG_URIS = new HashMap<>();
544548
MAP_COMPATIBILITY_CONFIG_URIS.put("http://www.bea.com/2002/09/xbean/config",
545549
"http://xml.apache.org/xmlbeans/2004/02/xbean/config");
546550
options.setLoadSubstituteNamespaces(MAP_COMPATIBILITY_CONFIG_URIS);
@@ -576,7 +580,7 @@ public String lookupPackageForNamespace(String uri) {
576580
}
577581

578582
if (uri2packageMappings.containsKey(uri)) {
579-
return (String)uri2packageMappings.get(uri);
583+
return uri2packageMappings.get(uri);
580584
} else {
581585
return URLProcessor.makePackageName(uri);
582586
}
@@ -598,14 +602,6 @@ public String lookupSuffixForNamespace(String uri) {
598602
}
599603
}
600604

601-
public String lookupJavanameForQName(QName qname) {
602-
if (bindConf != null) {
603-
return bindConf.lookupJavanameForQName(qname);
604-
} else {
605-
return super.lookupJavanameForQName(qname);
606-
}
607-
}
608-
609605
public String lookupJavanameForQName(QName qname, int kind) {
610606
if (bindConf != null) {
611607
return bindConf.lookupJavanameForQName(qname, kind);
@@ -674,7 +670,7 @@ private static File[] getBindingConfigJavaFiles(String javaFileNames) {
674670
if (javaFileNames == null) {
675671
return new File[0];
676672
}
677-
List<File> files = new Vector<File>();
673+
List<File> files = new ArrayList<>();
678674
for (String javaFileName : javaFileNames.split("\\s")) {
679675
try (Stream<Path> pathStream = Files.walk(new File(javaFileName).toPath(),
680676
FileVisitOption.FOLLOW_LINKS)) {
@@ -710,12 +706,12 @@ private static File[] getBindingConfigClasspath(String classpathNames) {
710706
* @param vec
711707
* @return schema array
712708
*/
713-
private static SchemaDocument.Schema[] convertToSchemaArray(List vec) {
709+
private static SchemaDocument.Schema[] convertToSchemaArray(List<SchemaDocument> vec) {
714710
SchemaDocument[] schemaDocuments =
715-
(SchemaDocument[])vec.toArray(new SchemaDocument[vec.size()]);
711+
vec.toArray(new SchemaDocument[0]);
716712
//remove duplicates
717-
Vector uniqueSchemas = new Vector(schemaDocuments.length);
718-
Vector uniqueSchemaTns = new Vector(schemaDocuments.length);
713+
List<SchemaDocument.Schema> uniqueSchemas = new ArrayList<>(schemaDocuments.length);
714+
List<String> uniqueSchemaTns = new ArrayList<>(schemaDocuments.length);
719715
SchemaDocument.Schema s;
720716
for (int i = 0; i < schemaDocuments.length; i++) {
721717
s = schemaDocuments[i].getSchema();
@@ -726,9 +722,8 @@ private static SchemaDocument.Schema[] convertToSchemaArray(List vec) {
726722
uniqueSchemas.add(s);
727723
}
728724
}
729-
return (SchemaDocument.Schema[])
730-
uniqueSchemas.toArray(
731-
new SchemaDocument.Schema[uniqueSchemas.size()]);
725+
return uniqueSchemas.toArray(
726+
new SchemaDocument.Schema[0]);
732727
}
733728

734729
/** Axis2 specific entity resolver */
@@ -752,7 +747,7 @@ public InputSource resolveEntity(String publicId, String systemId)
752747
// to avoid this we check whether it is started with http:// or not
753748
if (!systemId.startsWith("http://")) {
754749
StringTokenizer pathElements = new StringTokenizer(systemId, "/");
755-
Stack pathElementStack = new Stack();
750+
Stack<String> pathElementStack = new Stack<>();
756751
while (pathElements.hasMoreTokens()) {
757752
String pathElement = pathElements.nextToken();
758753
if (".".equals(pathElement)) {
@@ -763,11 +758,11 @@ public InputSource resolveEntity(String publicId, String systemId)
763758
pathElementStack.push(pathElement);
764759
}
765760
}
766-
StringBuffer pathBuilder = new StringBuffer();
767-
for (Iterator iter = pathElementStack.iterator(); iter.hasNext();) {
768-
pathBuilder.append(File.separator + iter.next());
761+
StringBuilder pathBuilder = new StringBuilder();
762+
for (Iterator<String> iter = pathElementStack.iterator(); iter.hasNext();) {
763+
pathBuilder.append(File.separator).append(iter.next());
769764
}
770-
systemId = pathBuilder.toString().substring(1);
765+
systemId = pathBuilder.substring(1);
771766
}
772767

773768

0 commit comments

Comments
 (0)