castor-data-binding / castor

http://castor-data-binding.github.io/castor/
35 stars 29 forks source link

ClassName is not a valid Java identifier #70

Open jiangdequan opened 6 years ago

jiangdequan commented 6 years ago

There is a problem about the classname! In the model castor-codegen of org.exolab.castor.builder.conflict.strategy.BaseClassNameConflictResolver.calculateXPathPrefix, in some cases, when the input is like '/Voucher{http://www.your.org/Test/Space}/Name/Surname', but the output is 'WwwYourOrgTestSpace}NameSurname'.This is not a valid java identifier.

the source code below split the input string words, but not remove the invalid identifier in model castor-xml(org.castor.xml.JavaNamingImpl)

protected String calculateXPathPrefix(final String xpath) {
    String prefix = "";
    StringTokenizer stringTokenizer = new StringTokenizer(xpath, "/.");
    while (stringTokenizer.hasMoreTokens()) {
      String token = stringTokenizer.nextToken();
      // break on last token
      if (!stringTokenizer.hasMoreTokens()) {
        break;
      }
      if (token.startsWith(ExtendedBinding.COMPLEXTYPE_ID)
          || token.startsWith(ExtendedBinding.SIMPLETYPE_ID)
          || token.startsWith(ExtendedBinding.ENUMTYPE_ID)
          || token.startsWith(ExtendedBinding.GROUP_ID)) {
        token = token.substring(token.indexOf(":") + 1);
      }
      prefix += _sourceGenerator.getJavaNaming().toJavaClassName(token);
    }
    return prefix;
  }

fix: add some code to remove "}" in model castor-xml(org.castor.xml.JavaNamingImpl)

public final String toJavaClassName(final String name) {

        if ((name == null) || (name.length() <= 0)) {
            // handle error
            return name; // -- for now just return name
        }
        // Remove namespace prefix (Andrew Fawcett, temporary until namespace
        // changes go in)
        int colon = name.indexOf(':');
        if (colon != -1) {
            return toJavaName(name.substring(colon + 1), true);
        }
        return toJavaName(name, true);

    } // -- toJavaClassName
wguttmn commented 6 years ago

Can you please provide a pull request ? And possibly include a unit test as well that clearly shows the abnormal behavior ?

jiangdequan commented 6 years ago

It occured when I add property 'org.exolab.castor.builder.automaticConflictResolution=true' to resolve class name conflict.The following is the error log:

Caused by: java.lang.IllegalArgumentException: 'WwwYourOrgTestSpace}NameSurname' is not a valid Java identifier.
    at org.exolab.javasource.JStructure.<init>(JStructure.java:109)
    at org.exolab.javasource.AbstractJClass.<init>(AbstractJClass.java:78)
    at org.exolab.javasource.AbstractJClass.<init>(AbstractJClass.java:69)
    at org.exolab.javasource.JClass.<init>(JClass.java:59)
    at org.exolab.castor.builder.descriptors.DescriptorJClass.<init>(DescriptorJClass.java:92)
    at org.exolab.castor.builder.descriptors.DescriptorSourceFactory.createSource(DescriptorSourceFactory.java:112)
    at org.exolab.castor.builder.SingleClassGenerator.processClassDescriptor(SingleClassGenerator.java:390)
    at org.exolab.castor.builder.SingleClassGenerator.process(SingleClassGenerator.java:370)
    at org.exolab.castor.builder.SingleClassGenerator.process(SingleClassGenerator.java:310)
    at org.exolab.castor.builder.SourceGenerator.createClasses(SourceGenerator.java:933)
    at org.exolab.castor.builder.SourceGenerator.processContentModel(SourceGenerator.java:1078)
    at org.exolab.castor.builder.SourceGenerator.processContentModel(SourceGenerator.java:1081)
    at org.exolab.castor.builder.SourceGenerator.processComplexType(SourceGenerator.java:1019)
    at org.exolab.castor.builder.SourceGenerator.createClasses(SourceGenerator.java:939)
    at org.exolab.castor.builder.SourceGenerator.generateAllClassFiles(SourceGenerator.java:786)
    at org.exolab.castor.builder.SourceGenerator.generateSource(SourceGenerator.java:744)
    at org.exolab.castor.builder.SourceGenerator.generateSource(SourceGenerator.java:690)
    at org.exolab.castor.builder.SourceGenerator.generateSource(SourceGenerator.java:587)

Due to the development environment, I commited several times, but the last one 'a57c621' is useful.