make a JAXB2/xjc Plugin

E

Erik

With NB 6.9.1 I want to write a Plugin for xjc, so it generates a
toString method like this:

<xs:annotation>
<xs:appinfo>
<ts:myToString>name</ts:myToString>
</xs:appinfo> </xs:annotation>

It's supposed to generate a toString method for Person(name,age),
which returns just the name of the object


But I'm baffled by all kinds of difficulties:
1: what should be in META_INF/Services ?
2: which namespace should I use where ?
3: which namespaces should be in .xsd ?

Can anyone help me out, maybe with a simple but complet NB project ?

For now I have these classes:

package com.sun.tools.xjc.addon.myToString;
public class Const {
public static final String NS =
"http://jaxb.dev.java.net/plugin/myToString";
}
------
package com.sun.tools.xjc.addon.myToString;

import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.Plugin;
import com.sun.tools.xjc.addon.code_injector.Const;
import com.sun.tools.xjc.model.CPluginCustomization;
import com.sun.tools.xjc.outline.ClassOutline;
import com.sun.tools.xjc.outline.Outline;
import com.sun.tools.xjc.util.DOMUtils;


import java.util.Collections;
import java.util.List;
import org.xml.sax.ErrorHandler;
public class PluginImpl extends Plugin {

public String getOptionName() {
return "XmyToString";
}

public List<String> getCustomizationURIs() {
return Collections.singletonList(Const.NS);
}

public boolean isCustomizationTagName(String nsUri, String
localName) {
return nsUri.equals(Const.NS) &&
localName.equals("myToString");
}

public String getUsage() {
return " -XmyToString : make a toString method.";
}

public boolean run(Outline model, Options opt, ErrorHandler
errorHandler) {
for (ClassOutline co : model.getClasses()) {
CPluginCustomization c =
co.target.getCustomizations().find(Const.NS, "myToString");
if (c == null) {
continue; // no customization --- nothing to inject
here
}
c.markAsAcknowledged();

// inject the specified code fragment into the
implementation class.
co.implClass.direct("\npublic String toString() {\n" +
" return " +
DOMUtils.getElementText(c.element) + ";\n" +
"}\n");
}

return true;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top