Skip to content

Commit 2590c0f

Browse files
committed
manage xdoc anchor name conflicts (2 classes with same anchor)
1 parent 676c4ab commit 2590c0f

File tree

7 files changed

+69
-22
lines changed

7 files changed

+69
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ target/
55
bin
66
.idea/
77
*.iml
8+
.factorypath
89
pom.xml.releaseBackup

modello-plugins/modello-plugin-xdoc/src/main/java/org/codehaus/modello/plugin/xdoc/XdocGenerator.java

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
import java.io.File;
2626
import java.io.IOException;
2727
import java.io.Writer;
28+
import java.util.HashMap;
2829
import java.util.HashSet;
2930
import java.util.List;
31+
import java.util.Map;
3032
import java.util.Properties;
3133
import java.util.Set;
3234
import java.util.Stack;
@@ -42,6 +44,7 @@
4244
import org.codehaus.modello.model.ModelField;
4345
import org.codehaus.modello.model.Version;
4446
import org.codehaus.modello.model.VersionRange;
47+
import org.codehaus.modello.plugin.xdoc.metadata.XdocClassMetadata;
4548
import org.codehaus.modello.plugin.xdoc.metadata.XdocFieldMetadata;
4649
import org.codehaus.modello.plugin.xsd.XsdModelHelper;
4750
import org.codehaus.modello.plugins.xml.AbstractXmlGenerator;
@@ -171,11 +174,16 @@ private void generateXdoc( Properties parameters )
171174
* Get the anchor name by which model classes can be accessed in the generated xdoc/html file.
172175
*
173176
* @param tagName the name of the XML tag of the model class
177+
* @param modelClass the model class, that eventually can have customized anchor name
174178
* @return the corresponding anchor name
175179
*/
176-
private String getAnchorName( String tagName )
180+
private String getAnchorName( String tagName, ModelClass modelClass )
177181
{
178-
return "class_" + tagName ;
182+
XdocClassMetadata xdocClassMetadata = (XdocClassMetadata) modelClass.getMetadata( XdocClassMetadata.ID );
183+
184+
String anchorName = xdocClassMetadata.getAnchorName();
185+
186+
return "class_" + ( anchorName == null ? tagName : anchorName );
179187
}
180188

181189
/**
@@ -186,7 +194,7 @@ private String getAnchorName( String tagName )
186194
*/
187195
private void writeModelDescriptor( XMLWriter w, ModelClass rootModelClass )
188196
{
189-
writeElementDescriptor( w, rootModelClass, null, new HashSet<String>() );
197+
writeElementDescriptor( w, rootModelClass, null, new HashSet<>(), new HashMap<>() );
190198
}
191199

192200
/**
@@ -195,26 +203,37 @@ private void writeModelDescriptor( XMLWriter w, ModelClass rootModelClass )
195203
* @param w the output writer
196204
* @param modelClass the mode class to describe
197205
* @param association the association we are coming from (can be <code>null</code>)
198-
* @param written set of data already written
206+
* @param writtenIds set of data already written ids
207+
* @param writtenAnchors map of already written anchors with corresponding ids
199208
*/
200209
private void writeElementDescriptor( XMLWriter w, ModelClass modelClass, ModelAssociation association,
201-
Set<String> written )
210+
Set<String> writtenIds, Map<String, String> writtenAnchors )
202211
{
203212
String tagName = resolveTagName( modelClass, association );
204213

205214
String id = getId( tagName, modelClass );
206-
if ( written.contains( id ) )
215+
if ( writtenIds.contains( id ) )
207216
{
208217
// tag already written for this model class accessed as this tag name
209218
return;
210219
}
211-
written.add( id );
220+
writtenIds.add( id );
212221

213-
written.add( tagName );
222+
String anchorName = getAnchorName( tagName, modelClass );
223+
if ( writtenAnchors.containsKey( anchorName ) )
224+
{
225+
// TODO use logging API?
226+
System.out.println( "[warn] model class " + id + " with tagName " + tagName + " gets duplicate anchorName "
227+
+ anchorName + ", conflicting with model class " + writtenAnchors.get( anchorName ) );
228+
}
229+
else
230+
{
231+
writtenAnchors.put( anchorName, id );
232+
}
214233

215234
w.startElement( "a" );
216235

217-
w.addAttribute( "name", getAnchorName( tagName ) );
236+
w.addAttribute( "name", anchorName );
218237

219238
w.endElement();
220239

@@ -257,9 +276,9 @@ private void writeElementDescriptor( XMLWriter w, ModelClass modelClass, ModelAs
257276
ModelAssociation assoc = (ModelAssociation) f;
258277
ModelClass fieldModelClass = getModel().getClass( assoc.getTo(), getGeneratedVersion() );
259278

260-
if ( !written.contains( getId( resolveTagName( fieldModelClass, assoc ), fieldModelClass ) ) )
279+
if ( !writtenIds.contains( getId( resolveTagName( fieldModelClass, assoc ), fieldModelClass ) ) )
261280
{
262-
writeElementDescriptor( w, fieldModelClass, assoc, written );
281+
writeElementDescriptor( w, fieldModelClass, assoc, writtenIds, writtenAnchors );
263282
}
264283
}
265284
}
@@ -350,7 +369,7 @@ private void writeFieldsTable( XMLWriter w, List<ModelField> fields, boolean ele
350369
if ( isInnerAssociation( f ) )
351370
{
352371
w.startElement( "a" );
353-
w.addAttribute( "href", "#" + getAnchorName( itemTagName ) );
372+
w.addAttribute( "href", "#" + getAnchorName( itemTagName, assoc.getToClass() ) );
354373
w.writeText( itemTagName );
355374
w.endElement();
356375
}
@@ -497,7 +516,7 @@ private String getElementXmlDescriptor( ModelClass modelClass, ModelAssociation
497516
String tagName = resolveTagName( modelClass, association );
498517

499518
// <tagName
500-
sb.append( "&lt;<a href=\"#" ).append( getAnchorName( tagName ) ).append( "\">" );
519+
sb.append( "&lt;<a href=\"#" ).append( getAnchorName( tagName, modelClass ) ).append( "\">" );
501520
sb.append( tagName ).append( "</a>" );
502521

503522
boolean addNewline = false;
@@ -672,7 +691,7 @@ else if ( ModelDefault.PROPERTIES.equals( f.getType() ) )
672691
* @param modelClass the class we are looking for the tag name
673692
* @param association the association where this class is used
674693
* @return the tag name to use
675-
* @todo refactor to use resolveTagName helpers instead
694+
* @todo refactor to use XmlModelHelpers.resolveTagName helpers instead
676695
*/
677696
private String resolveTagName( ModelClass modelClass, ModelAssociation association )
678697
{

modello-plugins/modello-plugin-xdoc/src/main/java/org/codehaus/modello/plugin/xdoc/metadata/XdocClassMetadata.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,16 @@ public class XdocClassMetadata
3131
implements ClassMetadata
3232
{
3333
public static final String ID = XdocClassMetadata.class.getName();
34+
35+
private String anchorName;
36+
37+
public String getAnchorName()
38+
{
39+
return anchorName;
40+
}
41+
42+
public void setAnchorName( String anchorName )
43+
{
44+
this.anchorName = anchorName;
45+
}
3446
}

modello-plugins/modello-plugin-xdoc/src/main/java/org/codehaus/modello/plugin/xdoc/metadata/XdocMetadataPlugin.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,15 @@ public class XdocMetadataPlugin
4646
implements MetadataPlugin
4747
{
4848
public static final String XDOC_SEPARATOR = "xdoc.separator";
49+
public static final String XDOC_ANCHORNAME = "xdoc.anchorName";
4950

5051
public ClassMetadata getClassMetadata( ModelClass clazz, Map<String, String> data )
5152
{
52-
return new XdocClassMetadata();
53+
XdocClassMetadata metadata = new XdocClassMetadata();
54+
55+
metadata.setAnchorName( getString( data, XDOC_ANCHORNAME ) );
56+
57+
return metadata;
5358
}
5459

5560
public InterfaceMetadata getInterfaceMetadata( ModelInterface iface, Map<String, String> data )

modello-plugins/modello-plugin-xdoc/src/test/java/org/codehaus/modello/plugin/xdoc/XdocGeneratorTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ public void testXdocGenerator()
6969
checkFeaturesXdocGenerator();
7070
checkSettingsXdocGenerator();
7171
}
72-
73-
public void testHtmlToXml() throws Exception
72+
73+
public void testHtmlToXml()
74+
throws Exception
7475
{
7576
ModelloCore modello = (ModelloCore) lookup( ModelloCore.ROLE );
7677

@@ -79,11 +80,11 @@ public void testHtmlToXml() throws Exception
7980
Properties parameters = getModelloParameters( "1.0.0" );
8081

8182
modello.generate( model, "xdoc", parameters );
82-
83+
8384
Diff diff = DiffBuilder.compare( Input.fromStream( XdocGeneratorTest.class.getResourceAsStream( "/html4.expected.xml" ) ) )
8485
.withTest( Input.fromFile( new File( getOutputDirectory(), "html4.xml" ) ) ).build();
85-
86-
assertFalse(diff.toString(), diff.hasDifferences());
86+
87+
assertFalse( diff.toString(), diff.hasDifferences() );
8788
}
8889

8990
private void checkMavenXdocGenerator()
@@ -166,7 +167,7 @@ public void checkSettingsXdocGenerator()
166167

167168
String content = FileUtils.fileRead( new File( getOutputDirectory(), "settings.xml" ), "UTF-8" );
168169

169-
assertTrue( "Properties field was erroneously documented", !content.contains("&lt;properties/&gt;"));
170+
assertTrue( "Properties field was erroneously documented", !content.contains("&lt;properties/&gt;") );
170171
}
171172

172173
/**

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@
616616
<models>
617617
<model>src/main/mdo/modello.mdo</model>
618618
</models>
619-
<version>1.8.0</version>
619+
<version>2.0.0</version>
620620
</configuration>
621621
<executions>
622622
<execution>

src/main/mdo/modello.mdo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,15 @@
441441
</description>
442442
<comment>see org.codehaus.modello.plugin.model.ModelMetadataPlugin</comment>
443443
</field>
444+
<field xml.attribute="true" xml.tagName="xdoc.anchorName">
445+
<name>anchorName</name>
446+
<version>2.0.0+</version>
447+
<type>String</type>
448+
<description>
449+
Define a anchor base name to be used in XML content, which can be different from the XML tag name name.
450+
</description>
451+
<comment>see org.codehaus.modello.plugin.model.ModelMetadataPlugin</comment>
452+
</field>
444453
<field xml.attribute="true" xml.tagName="java.enabled">
445454
<name>enabled</name>
446455
<version>1.0.0+</version>

0 commit comments

Comments
 (0)