Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 395 : add option to generate default-attribute-value in java class #396

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.sun.tools.xjc.outline.EnumOutline;
import com.sun.tools.xjc.outline.FieldOutline;
import com.sun.tools.xjc.outline.Outline;
import com.sun.xml.xsom.XmlString;
import com.sun.xml.xsom.XSAttributeUse;
import com.sun.xml.xsom.XSElementDecl;
import com.sun.xml.xsom.XSParticle;
import com.sun.xml.xsom.XSTerm;
Expand Down Expand Up @@ -62,6 +64,8 @@ public class DefaultValuePlugin
extends Plugin
{

private static final String OPTION_NAME_ALL = "-Xdefault-value:all";
private boolean all = false;
/**
* Name of Option to enable this plugin
*/
Expand Down Expand Up @@ -92,7 +96,9 @@ public String getOptionName() {
*/
public String getUsage()
{
return " -"+OPTION_NAME+" : enable rewriting of classes to set default values for fields as specified in XML schema";
return " -"+OPTION_NAME+" : enable rewriting of classes to set default values for fields as specified in XML schema\n"
+ " [-"+OPTION_NAME+":all : enable rewriting of classes for default values of all fields and attributes]"
;
}

/**
Expand All @@ -102,8 +108,7 @@ public String getUsage()
* <li>Look for fields that:
* <ul>
* <li>Were generated from XSD description</li>
* <li>The XSD description is of type xsd:element (code level default values
* are not necessary for fields generated from attributes)</li>
* <li>The XSD description is of type xsd:element (or xsd:attribute if all is set)</li>
* <li>A default value is specified</li>
* <li>Map to one of the supported types</li>
* </ul>
Expand All @@ -123,24 +128,27 @@ public boolean run(Outline outline, Options opt, ErrorHandler errorHandler)
// check all Fields in Class
for (FieldOutline f : co.getDeclaredFields()) {
CPropertyInfo fieldInfo = f.getPropertyInfo();

// Do nothing if Field is not created from an xsd particle
if (!(fieldInfo.getSchemaComponent() instanceof XSParticle)) {
continue;
}
XSTerm term = ((XSParticle) fieldInfo.getSchemaComponent()).getTerm();

// Default values only necessary for fields derived from an xsd:element
if (!term.isElementDecl()) {
XmlString xmlDefaultValue = null;
if (fieldInfo.getSchemaComponent() instanceof XSParticle) {
XSTerm term = ((XSParticle) fieldInfo.getSchemaComponent()).getTerm();// Default values only necessary for fields derived from an xsd:element
if (!term.isElementDecl()) {
continue;
}
XSElementDecl element = term.asElementDecl();
xmlDefaultValue = element.getDefaultValue();
} else if (all && fieldInfo.getSchemaComponent() instanceof XSAttributeUse) {
XSAttributeUse attribute = ((XSAttributeUse) fieldInfo.getSchemaComponent());
xmlDefaultValue = attribute.getDefaultValue();
} else {
// Do nothing if Field is not created from an xsd particle
continue;
}
XSElementDecl element = term.asElementDecl();

// Do nothing if no default value
if (element.getDefaultValue() == null) {
if (xmlDefaultValue == null) {
continue;
}
String defaultValue = element.getDefaultValue().value;
String defaultValue = xmlDefaultValue.value;

// Get handle to JModel representing the field
Map<String, JFieldVar> fields = co.implClass.fields();
Expand Down Expand Up @@ -320,5 +328,16 @@ private JFieldVar installDtF(final JDefinedClass parentClass)
}
}

@Override
public int parseArgument(Options opt, String[] args, int i) {
// eg. -Xdefault-value:all
String arg = args[i].trim();
if (arg.startsWith(OPTION_NAME_ALL)) {
all = true;
return 1;
}
return 0;
}


}
1 change: 1 addition & 0 deletions jaxb-plugins-parent/tests/defaultvalue/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<extension>true</extension>
<args>
<arg>-Xdefault-value</arg>
<arg>-Xdefault-value:all</arg>
</args>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@
<xs:element name="state" type="xs:string"/>
<xs:element name="ZIP" type="xs:string"/>
</xs:sequence>
<xs:attribute name="house" type="xs:boolean" default="true" />
<xs:attribute name="floor" type="xs:int" />
</xs:complexType>
</xs:schema>