Skip to content

Commit 1d5831f

Browse files
kwinhboutemy
authored andcommitted
Support arbitrary element names via 'xs:any'
Those have attribute 'xml.tagName' set to '*'
1 parent c5ef5eb commit 1d5831f

File tree

7 files changed

+22635
-23
lines changed

7 files changed

+22635
-23
lines changed

modello-plugins/modello-plugin-xsd/src/main/java/org/codehaus/modello/plugin/xsd/XsdGenerator.java

+24-3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
public class XsdGenerator
5555
extends AbstractXmlGenerator
5656
{
57+
/**
58+
* Value standing for any element name (used on xml.tagName)
59+
*/
60+
private static final String ANY_NAME = "*";
5761
protected static final String LS = System.getProperty( "line.separator" );
5862

5963
public void generate( Model model, Properties parameters )
@@ -245,6 +249,15 @@ private void writeComplexTypeDescriptor( XMLWriter w, Model objectModel, ModelCl
245249

246250
if ( !hasContentField )
247251
{
252+
if ( fieldTagName.equals( ANY_NAME ) )
253+
{
254+
w.startElement( "xs:any" );
255+
w.addAttribute( "minOccurs", "0" );
256+
w.addAttribute( "maxOccurs", "unbounded" );
257+
w.addAttribute( "processContents", "skip" );
258+
w.endElement();
259+
continue;
260+
}
248261
w.startElement( "xs:element" );
249262

250263
if ( !enforceMandatoryElements || !field.isRequired() )
@@ -489,11 +502,19 @@ private void writeListElement( XMLWriter w, XmlFieldMetadata xmlFieldMetadata,
489502

490503
w.startElement( "xs:sequence" );
491504

492-
w.startElement( "xs:element" );
493-
w.addAttribute( "name", valuesTagName );
505+
if ( valuesTagName.equals( ANY_NAME ) )
506+
{
507+
w.startElement( "xs:any" );
508+
w.addAttribute( "processContents", "skip" );
509+
}
510+
else
511+
{
512+
w.startElement( "xs:element" );
513+
w.addAttribute( "type", type );
514+
w.addAttribute( "name", valuesTagName );
515+
}
494516
w.addAttribute( "minOccurs", "0" );
495517
w.addAttribute( "maxOccurs", "unbounded" );
496-
w.addAttribute( "type", type );
497518

498519
w.endElement();
499520

modello-plugins/modello-plugin-xsd/src/test/java/org/codehaus/modello/plugin/xsd/ModelloXsdGeneratorTest.java

+1-20
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@
3030
import org.xml.sax.SAXParseException;
3131
import org.xml.sax.helpers.DefaultHandler;
3232

33-
import java.io.File;
3433
import java.util.Properties;
3534

3635
import javax.xml.parsers.SAXParser;
37-
import javax.xml.parsers.SAXParserFactory;
3836

3937
/**
4038
* Check that features.mdo (which tries to be the most complete model) can be checked against XSD generated from
@@ -61,24 +59,7 @@ public void testXsdGenerator()
6159

6260
modello.generate( model, "xsd", parameters );
6361

64-
/* only available in JAXP 1.3, JDK 5+
65-
SchemaFactory factory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
66-
Schema schema = factory.newSchema( new StreamSource( new File( generatedSources, "features.xsd" ) ) );
67-
68-
SAXParserFactory spf = SAXParserFactory.newInstance();
69-
spf.setSchema( schema );
70-
SAXParser parser = spf.newSAXParser();
71-
parser.parse( new InputSource( getClass().getResourceAsStream( "/features.xml" ) ) );
72-
*/
73-
74-
SAXParserFactory factory = SAXParserFactory.newInstance();
75-
factory.setValidating( true );
76-
factory.setNamespaceAware( true );
77-
SAXParser saxParser = factory.newSAXParser();
78-
saxParser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
79-
"http://www.w3.org/2001/XMLSchema" );
80-
saxParser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaSource",
81-
new File( getOutputDirectory(), "modello-1.4.0.xsd" ) );
62+
SAXParser saxParser = createSaxParserWithSchema( "modello-1.4.0.xsd" );
8263

8364
// first self-test: validate Modello model with xsd generated from it
8465
saxParser.parse( getTestFile( "../../src/main/mdo/modello.mdo" ), new Handler() );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.codehaus.modello.plugin.xsd;
2+
3+
import javax.xml.parsers.ParserConfigurationException;
4+
import javax.xml.parsers.SAXParser;
5+
6+
import java.io.IOException;
7+
import java.util.Properties;
8+
9+
import org.codehaus.modello.AbstractModelloGeneratorTest;
10+
import org.codehaus.modello.ModelloException;
11+
import org.codehaus.modello.core.ModelloCore;
12+
import org.codehaus.modello.model.Model;
13+
import org.codehaus.modello.model.ModelValidationException;
14+
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
15+
import org.junit.Test;
16+
import org.xml.sax.SAXException;
17+
import org.xml.sax.SAXParseException;
18+
import org.xml.sax.helpers.DefaultHandler;
19+
20+
/**
21+
* Test that the <a href="https://github.com/apache/maven/blob/master/maven-plugin-api/src/main/mdo/plugin.mdo">Maven plugin descriptor</a>
22+
* generates an XSD which can validate plugin descriptors (with arbitrary element names below {@code <configuration>}).
23+
* @see <a href="https://github.com/codehaus-plexus/modello/issues/264">Issue 264</a>
24+
*
25+
*/
26+
public class PluginsXsdGeneratorTest
27+
extends AbstractModelloGeneratorTest
28+
{
29+
30+
public PluginsXsdGeneratorTest()
31+
{
32+
super( "plugins" );
33+
}
34+
35+
@Test
36+
public void testWithNameWildcard()
37+
throws ModelloException, ModelValidationException, IOException, ComponentLookupException,
38+
ParserConfigurationException, SAXException
39+
{
40+
ModelloCore modello = (ModelloCore) lookup( ModelloCore.ROLE );
41+
42+
Model model = modello.loadModel( getXmlResourceReader( "/plugin.mdo" ) );
43+
44+
// generate XSD file
45+
Properties parameters = getModelloParameters( "1.0.0" );
46+
47+
modello.generate( model, "xsd", parameters );
48+
49+
SAXParser parser = createSaxParserWithSchema( "plugin-1.0.0.xsd" );
50+
parser.parse( getClass().getResourceAsStream( "/plugin.xml" ), new Handler() );
51+
}
52+
53+
private static class Handler
54+
extends DefaultHandler
55+
{
56+
public void warning( SAXParseException e )
57+
throws SAXException
58+
{
59+
throw e;
60+
}
61+
62+
public void error( SAXParseException e )
63+
throws SAXException
64+
{
65+
throw e;
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)