sonatype / modello

http://modello.codehaus.org/
7 stars 11 forks source link

Problem with codegernation in case of special Names #18

Open khmarbaise opened 9 years ago

khmarbaise commented 9 years ago

Currently i'm creating a modell with modello which contains a name of a class Name which results in the following code generation (Xpp3Reader):

    private Name parseName( XmlPullParser parser, boolean strict )
        throws IOException, XmlPullParserException
    {
        String tagName = parser.getName();
        Name name = new Name();
        for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
        {
            String name = parser.getAttributeName( i );
            String value = parser.getAttributeValue( i );

            if ( name.indexOf( ':' ) >= 0 )
            {
                // just ignore attributes with non-default namespace (for example: xmlns:xsi)
            }
            else if ( "id".equals( name ) )
            {
                name.setId( interpolatedTrimmed( value, "id" ) );
            }
            else
            {
                checkUnknownAttribute( parser, name, tagName, strict );
            }
        }

The problem is the name for name which is used. This will produce a clash with the attribute name. I would suggest to change the code generation to use attributeName instead of name which will reduce the possibility of a name clash. Maybe the name before the for loop schould be changed into something like className = new XYZ() ?

Here the culprit model:

<?xml version="1.0"?>
<model xmlns="http://modello.codehaus.org/MODELLO/1.4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://modello.codehaus.org/MODELLO/1.4.1 http://modello.codehaus.org/xsd/modello-1.4.1.xsd">

  <id>configuration</id>
  <name>Configuration</name>
  <defaults>
    <default>
      <key>package</key>
      <value>configuration.model</value>
    </default>
  </defaults>

  <classes>
    <class rootElement="true" xml.tagName="configuration" java.builder="true" java.staticCreator="true">
      <name>Configuration</name>
      <version>1.0.0+</version>
      <fields>
        <field xml.tagName="stage">
          <name>stages</name>
          <version>1.0.0+</version>
          <association xml.itemsStyle="flat">
            <type>Stage</type>
            <multiplicity>*</multiplicity>
          </association>
        </field>
      </fields>
    </class>

    <class java.builder="true" java.staticCreator="true">
      <id>stage</id>
      <name>Stage</name>
      <version>1.0.0+</version>
      <fields>
        <field xml.attribute="true">
          <version>1.0.0+</version>
          <required>true</required>
          <name>id</name>
          <type>String</type>
        </field>
        <field xml.tagName="name">
          <name>names</name>
          <version>1.0.0+</version>
          <association xml.itemsStyle="flat">
            <type>Name</type>
            <multiplicity>*</multiplicity>
          </association>
        </field>
      </fields>
    </class>
    <class java.builder="true" java.staticCreator="true">
      <id>idName</id>
      <name>Name</name>
      <version>1.0.0+</version>
      <fields>
        <field xml.attribute="true">
          <version>1.0.0+</version>
          <required>true</required>
          <name>id</name>
          <type>String</type>
        </field>
        <field xml.tagName="env">
          <name>environments</name>
          <version>1.0.0+</version>
          <association xml.itemsStyle="flat">
            <type>Env</type>
            <multiplicity>*</multiplicity>
          </association>
        </field>

      </fields>
    </class>
    <class java.builder="true" java.staticCreator="true">
      <id>env</id>
      <name>Env</name>
      <version>1.0.0+</version>
      <fields>
        <field xml.attribute="true">
          <version>1.0.0+</version>
          <required>true</required>
          <name>id</name>
          <type>String</type>
        </field>
        <field xml.attribute="true">
          <version>1.0.0+</version>
          <required>true</required>
          <name>value</name>
          <type>String</type>
        </field>
      </fields>
    </class>
  </classes>
</model>