I want to customize the marshalling of dates in JAXB. It's a variant of this already asked question. I would think I would use an XMLAdapter, as this answer questions specifies.
But I can't do that exactly, because I'm going the other way around, generating the JAXB beans from an .XSD -- I can't add annotations to the JAXB beans because they are generated code.
I've tried calling Marshaller.setAdapter(), but with no luck.
final Marshaller marshaller = getJaxbContext().createMarshaller();
marshaller.setSchema(kniSchema);
marshaller.setAdapter(new DateAdapter());
...
private static class DateAdapter extends XmlAdapter<String, XMLGregorianCalendar> {
#Override
public String marshal(XMLGregorianCalendar v) throws Exception {
return "hello"; //Just a test to see if it's working
}
#Override
public XMLGregorianCalendar unmarshal(String v) throws Exception {
return null; // Don't care about this for now
}
}
Where the relevant part of my generated JAXB bean looks like this:
#XmlSchemaType(name = "date")
protected XMLGregorianCalendar activeSince;
When I do this, what the default date/XMLGregorianCalendar marshalling happens. It's as if I didn't do it all.
Any help is appreciated.
Thanks,
Charles
Related
I am testing JSF component but I am getting NullPointerException :( The problem code is :
#FacesComponent(value="mycomponent0")
public class MyComponent extends HtmlPanelGroup{
MyComponent(){
String a0=this.getAttributes().get("attr0");
}
}
the taglib.xml's tag contains the attr0 attribute and the tags usage is :
<abc:mycomponent attr0="helloworld"></abc:mycomponent>
So my question is what causes the issue and how to handle it?
Thanks
I guess I could figure out how to workaround the NPE issue...
I don't use constructor's body to get attributes but get attribute in overriden encodeBegin method;
#Override
public void encodeBegin(FacesContext context) throws IOException {
String id=this.getAttributes().get("id").toString();
System.out.println("debug: attribute id="+id);
String color=this.getAttributes().get("attr0").toString();
System.out.println("debug: attribute attr0="+attr0);
HtmlOutputLabel label0=new HtmlOutputLabel();
label0.setValue("attribute attr0 value="+attr0);
this.getChildren().add(label0);
super.encodeBegin(context);
}
So it's working and I think I am OK with this solution; I am not sure is it the most optimal way so please do share some snippets...
I am trying to have a custom SystemEventListener that registers for all instances of type UIInput and reacts to their postValidate-Events. Based on an example I found on the web I managed to get one running for HtmlInputText by registering it in the faces-config.xml as follows:
<system-event-listener>
<source-class>javax.faces.component.html.HtmlInputText</source-class>
<system-event-class>javax.faces.event.PostValidateEvent</system-event-class>
<system-event-listener-class>com.ourcompany.ourproduct.validators.inputPostValidationListener</system-event-listener-class>
</system-event-listener>
I then tried to 1) broaden this to work for UIInputs in general and to 2) use the #ListenerForannotation, but I just can't seem to get either of it to work.
for 1) I couldn't really find any examples or documentation, so i just tried around by a) defining multiple source-class-tags or by b) using javax.faces.component.UIInput as source-class. Neither worked.
for 2) I tried
#ListenerFor(systemEventClass = PostValidateEvent.class, sourceClass = UIInput.class)
which worked neither for UIInput nor for html.HtmlInputText.
Now, when I duplicate the same XML-configuration for all the other types of HTML-Inputs, this does the trick, but it just clutters up the xml and altogether seems quite annoying to me.
So the question is: Am I generally doing something wrong with the #ListenerFor annotation? Is there a restriction on which source-classes are possible, i.e. why can't I use the more generic UIInput? Is there a more efficient way to register the listener for all those different inputs than repeating the XML? And finally: I'd rather like to implement ComponentSystemEventListener. Assuming that the above problem was resolved, I'd just change the implements-Statement and implement the abstract processEvent accordingly, right? Would that work just the same or is the registration/xml-config different in this case (e.g. maybe <component-system-event-listener> instead of just <system-event-listener>?
(and as an after-note: is it just me or is it kind of hard to find any non-trivial examples for this kind of stuff on the web?)
The #ListenerFor is supposed to be set on an UIComponent or Renderer implementation, not on a standalone SystemEventListener implementation. See also the javadoc (emphasis mine):
The default implementation must support attaching this annotation to UIComponent or Renderer classes. In both cases, the annotation processing described herein must commence during the implementation of any variant of Application.createComponent() and must complete before the UIComponent instance is returned from createComponent(). The annotation processing must proceed according to an algorithm semantically equivalent to the following.
...
In order to have a global listener, not specific to an UIComponent or Renderer, your best bet is creating and registering a PhaseListener which subscribes the listener to the view root.
public class PostValidateListener implements PhaseListener {
#Override
public PhaseId getPhaseId() {
return PhaseId.PROCESS_VALIDATIONS;
}
#Override
public void beforePhase(PhaseEvent event) {
event.getFacesContext().getViewRoot()
.subscribeToViewEvent(PostValidateEvent.class, new InputPostValidationListener()); // Capitalize class name?
}
#Override
public void afterPhase(PhaseEvent event) {
// NOOP.
}
}
To get it to run, register it as follows in faces-config.xml:
<lifecycle>
<phase-listener>com.example.PostValidateListener</phase-listener>
</lifecycle>
You can even make your InputPostValidationListener itself a PhaseListener.
public class InputPostValidationListener implements PhaseListener, SystemEventListener {
#Override
public void beforePhase(PhaseEvent event) {
event.getFacesContext().getViewRoot().subscribeToViewEvent(PostValidateEvent.class, this);
}
// ...
}
I'm running an enviroment with JSF + Primefaces + tomcat 6.0.32 in netbeans using EclipseLink (JPA 2.0).
My application works fine, but everytime I run it, I get a lot of warnings saying that cannot Serializate my session beans, and shows me blocks like this for every session bean:
18-jul-2012 23:05:46 org.apache.catalina.session.StandardSession writeObject
ADVERTENCIA: No puedo serializar atributo de sesión facturacionController para sesión 62A53325838E1E7C6EB6607B1E7965E6
java.io.NotSerializableException: org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
... and so on...
The thing is that my session beans already implements Serializable.
So what can I do to solve this ?
Thanks !
---- added info 07/20/2012 ----
The only point where I'm making a reference to EntityManager from the session bean is when I create the jpaController in the getter property, like this:
private JpaController getJpaController() {
if (jpaController == null) {
jpaController = new JpaController(Persistence.createEntityManagerFactory("myPersistenceUnit"));
}
return jpaControllerPedido;
}
That is because I defined the jpaController constructor like this:
public JpaController(EntityManagerFactory emf) {
this.emf = emf;
}
Making a class Serializable does not means everything in it will be serializable. All references(dependencies/properties) in your class, they themselves should be serializable and in turn their references.
As per above exception it seems your session bean is having reference to EntityManagerFactoryImpl object which is not serializable and hence the error.
To solve this you can define it as transient than it wont be serialized but only problem will be during de-serialization you will have to build the object or assign reference manually.
I suggest have a look at this article on Serilization.
How to solve this, I don't do JPA so cannot tell if there is some serialized class for same,
To solve it define the reference as transient
transient EntityManagerFactory entityManagerFactory
and assign the reference back to bean manually in deserialization hook method as described below.
private void readObject(java.io.ObjectInputStream stream)
throws java.io.IOException, ClassNotFoundException
{
stream.defaultReadObject();
// assign reference manually.
this.entityManagerFactory = //get from factory;
}
Hope this helps !!!!
You only add this:
transient EntityManagerFactory entityManagerFactory;
But if have any other Object that implement entityManagerFactory, this object must also be defined as. For Example
transient static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("porpertiesConexion");
transient EntityManager em;
public void beginTransaction() {
em = emf.createEntityManager(); //
//code....
}
I want XML like this:
<simple>Foo</simple>
I can do this successfully via a JAXB class that looks like this:
#XmlRootElement(name="simple")
class Simple {
#XmlValue
public String contents;
}
But now I need to make the Simple class be a subclass of another class like so:
#XmlRootElement(name="simple")
class Simple extends OtherClass {
#XmlValue
public String contents;
}
That fails with #XmlValue is not allowed on a class that derives another class. I can't easily refactor the superclass away (because of the way we're using #XmlElementRef on a wrapper class). Is there a workaround that will let me annotate my subclass to generate that simple XML?
The accepted answer didn't work for me.
Everything is fine as described but I also needed to add the #XmlTransient to the superclass
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
This use case is supported by MOXy, and IMHO should be supported by the JAXB RI as well:
Simple
This class has a field mapped with #XmlValue and extends OtherClass:
package forum809827;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
#XmlRootElement(name="simple")
class Simple extends OtherClass {
#XmlValue
// #XmlValueExtension
// As of moxy 2.6, XmlValueExtension needs to be added for this to work
public String contents;
}
OtherClass
This is the super class. In MOXy the subclass can map a field/property with #XmlValue as long as the super class does not have any mappings to an XML element:
package forum809827;
import javax.xml.bind.annotation.XmlAttribute;
public class OtherClass {
#XmlAttribute
public String other;
}
Demo
package forum809827;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Simple.class);
Simple simple = new Simple();
simple.contents = "FOO";
simple.other = "BAR";
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(simple, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8"?>
<simple xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" other="BAR">FOO</simple>
For More Information on Specifying MOXy as Your JAXB Provider
http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
I was able to make this work by changing #XmlValue to #XmlMixed and changing the variable to a list. The resulting class should look like the following.
#XmlRootElement(name="simple")
class Simple extends OtherClass {
#XmlMixed
public List<String> contents;
}
This problem happened to me , and took me a little bit time.
Thanks to Blaise Doughan
I go through his blog and find the answer
you have to add a
jaxb.properties file with javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
in the same package in order to use MOXy
add moxy to your maven dependency or add moxy jar
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.5.0</version>
</dependency>
then all set
I have sample here you can go though my project and take a look at
https://github.com/cicidi/HelloCCD/tree/master/Jaxb
I have a web server responding with xml data and a client consuming it.
Both share the same domain code. One of the domain objects looks like this:
#XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
#XmlRootElement(name = "image")
public class Image {
private String filename;
private ImageTypeEnum type;
#XmlElement(name = "imageUri")
public String getAbsoluteUri() {
// some complex computation
return uri;
}
}
When I try to unmarshal the response from the server into this object, since there's no setter for absoluteUri, I don't have the imageUri in the class. So I extend it like this:
public class FEImage extends Image{
private String imageUri;
public String getAbsoluteUri() {
return imageUri;
}
public void setAbsoluteUri(String imageUri) {
this.imageUri = imageUri;
}
}
My ObjectFactory
#XmlRegistry
public class ObjectFactory {
public Image createImage(){
return new FEImage();
}
}
My code to unmarshal is here:
JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setProperty("com.sun.xml.bind.ObjectFactory",new ObjectFactory());
((JAXBElement)unmarshaller.unmarshal((InputStream) response.getEntity())).getValue();
However, the setAbsoluteUri doesn't seem to be getting called in FEImage while unmarshalling. When I add a dummy setAbsoluteUri in Image.java, everything works as expected.
Can someone tell me how can I cleanly extend from Image.java?
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
A JAXB implementation is not required to use the ObjectFactory class when instantiating an object. You can configure instantiation to be done via a factory class using the #XmlType annotation:
#XmlType(factoryClass=ObjectFactory.class, factoryMethod="createImage")
public class Image {
private String filename;
private ImageTypeEnum type;
#XmlElement(name = "imageUri")
public String getAbsoluteUri() {
// some complex computation
return uri;
}
}
http://blog.bdoughan.com/2011/06/jaxb-and-factory-methods.html
If you do the above, then your JAXB implementation will still use the Image class to derive the metadata so it will not solve your problem. An alternate approach would be to use an XmlAdapter for this use case:
http://blog.bdoughan.com/2010/12/jaxb-and-immutable-objects.html
Better still, when a property on your domain object does not have a setter, you can tell your
JAXB implementation (EclipseLink MOXy, Metro, Apache JaxMe, etc) to use field (instance variable) access instead using #XmlAccessorType(XmlAccessType.FIELD):
#XmlAccessorType(XmlAccessType.FIELD)
public class Image {
}
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
UPDATE #1
If you are not able to modify the domain objects, then you may be interested in MOXy's externalized metadata. This extension provides a means via XML to provide JAXB metadata for classes where you cannot modify the source.
For More Information
http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
http://wiki.eclipse.org/EclipseLink/UserGuide/MOXy/Runtime/XML_Bindings
UPDATE #2 - Based on results of chat
Image
Below is the implementation of the Image class that I will use for this example. For the complex computation of getAbsoluteUri() I simply add the prefix "CDN" to the filename:
package forum7552310;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
#XmlRootElement(name = "image")
public class Image {
private String filename;
private ImageTypeEnum type;
#XmlElement(name = "imageUri")
public String getAbsoluteUri() {
return "CDN" + filename;
}
}
binding.xml
Below is the MOXy binding document I put together. In this file I do a few things:
Set XmlAccessorType to FIELD
Mark the absoluteURI property to be XmlTransient since we will be mapping the filename field instead.
Specify that an XmlAdapter will be used with the filename field. This is to apply the logic that is done in the getAbsoluteUri() method.
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum7552310">
<java-types>
<java-type name="Image" xml-accessor-type="FIELD">
<java-attributes>
<xml-element java-attribute="filename" name="imageUri">
<xml-java-type-adapter value="forum7552310.FileNameAdapter"/>
</xml-element>
<xml-transient java-attribute="absoluteUri"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
FileNameAdapter
Below is the implementation of the XmlAdapter that applies the same name algorithm as the getAbsoluteUri() method:
package forum7552310;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class FileNameAdapter extends XmlAdapter<String, String> {
#Override
public String marshal(String string) throws Exception {
return "CDN" + string;
}
#Override
public String unmarshal(String adaptedString) throws Exception {
return adaptedString.substring(3);
}
}
Demo
Below is the demo code demonstrating how to apply the binding file when creating the JAXBContext:
package forum7552310;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum7552310/binding.xml");
JAXBContext jc = JAXBContext.newInstance(new Class[] {Image.class}, properties);
File xml = new File("src/forum7552310/input.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Image image = (Image) unmarshaller.unmarshal(xml);
System.out.println(image.getAbsoluteUri());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(image, System.out);
}
}
jaxb.properties
You need to include a file named jaxb.properties with the following contents in the same package as your Image class:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
input.xml
Here is the XML input I used:
<?xml version="1.0" encoding="UTF-8"?>
<image>
<imageUri>CDNURI</imageUri>
</image>
Output
And here is the output from running the demo code:
CDNURI
<?xml version="1.0" encoding="UTF-8"?>
<image>
<imageUri>CDNURI</imageUri>
</image>