Wednesday, October 5, 2011

There's no ObjectFactory with an @XmlElementDecl

So I was trying to use the OGC Schemas compiled with JAXB. I compiled them all and decided to test them with this class:
import net.opengis.sos.v_1_0_0.InsertObservation;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class JAXBTest {
    public static void main(String[] args) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(InsertObservation.class);
        InsertObservation insert = new InsertObservation();
        Marshaller mar = context.createMarshaller();
        mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        mar.marshal(insert, System.out);

    }
}

However it throws this unenlightening (for me at least) error:


There's no ObjectFactory with an @XmlElementDecl for the element {http://www.opengis.net/sensorML/1.0.1}_Process.
 this problem is related to the following location:
  at protected java.util.List net.opengis.om.v_1_0_0.ProcessPropertyType.content
  at net.opengis.om.v_1_0_0.ProcessPropertyType
  at public net.opengis.om.v_1_0_0.ProcessPropertyType net.opengis.om.v_1_0_0.ObjectFactory.createProcessPropertyType()
  at net.opengis.om.v_1_0_0.ObjectFactory
  at protected java.lang.Object net.opengis.om.v_1_0_0.AnyOrReferenceType.any
  at net.opengis.om.v_1_0_0.AnyOrReferenceType
  at protected net.opengis.om.v_1_0_0.AnyOrReferenceType net.opengis.om.v_1_0_0.ObservationType.metadata
  at net.opengis.om.v_1_0_0.ObservationType
  at protected net.opengis.om.v_1_0_0.ObservationType net.opengis.sos.v_1_0_0.InsertObservation.observation
  at net.opengis.sos.v_1_0_0.InsertObservation
There's no ObjectFactory with an @XmlElementDecl for the element {http://www.opengis.net/ogc}temporalOps.
 this problem is related to the following location:
  at protected javax.xml.bind.JAXBElement net.opengis.sos.v_1_0_0.GetResult$EventTime.temporalOps
  at net.opengis.sos.v_1_0_0.GetResult$EventTime
  at protected java.util.List net.opengis.sos.v_1_0_0.GetResult.eventTime
  at net.opengis.sos.v_1_0_0.GetResult
  at @javax.xml.bind.annotation.XmlSeeAlso(value=[class net.opengis.sos.v_1_0_0.DescribeFeatureType, class net.opengis.sos.v_1_0_0.DescribeResultModel, class net.opengis.sos.v_1_0_0.InsertObservation, class net.opengis.sos.v_1_0_0.DescribeSensor, class net.opengis.sos.v_1_0_0.GetObservationById, class net.opengis.sos.v_1_0_0.GetFeatureOfInterestTime, class net.opengis.sos.v_1_0_0.GetResult, class net.opengis.sos.v_1_0_0.RegisterSensor, class net.opengis.sos.v_1_0_0.GetObservation, class net.opengis.sos.v_1_0_0.DescribeObservationType, class net.opengis.sos.v_1_0_0.GetFeatureOfInterest])
  at net.opengis.sos.v_1_0_0.InsertObservation
There's no ObjectFactory with an @XmlElementDecl for the element {http://www.opengis.net/ogc}spatialOps.
 this problem is related to the following location:
  at protected javax.xml.bind.JAXBElement net.opengis.sos.v_1_0_0.GetObservation$FeatureOfInterest.spatialOps
  at net.opengis.sos.v_1_0_0.GetObservation$FeatureOfInterest
  at public net.opengis.sos.v_1_0_0.GetObservation$FeatureOfInterest net.opengis.sos.v_1_0_0.ObjectFactory.createGetObservationFeatureOfInterest()
  at net.opengis.sos.v_1_0_0.ObjectFactory
  at protected javax.xml.bind.JAXBElement net.opengis.sos.v_1_0_0.GetResult$EventTime.temporalOps
  at net.opengis.sos.v_1_0_0.GetResult$EventTime
  at protected java.util.List net.opengis.sos.v_1_0_0.GetResult.eventTime
  at net.opengis.sos.v_1_0_0.GetResult
  at @javax.xml.bind.annotation.XmlSeeAlso(value=[class net.opengis.sos.v_1_0_0.DescribeFeatureType, class net.opengis.sos.v_1_0_0.DescribeResultModel, class net.opengis.sos.v_1_0_0.InsertObservation, class net.opengis.sos.v_1_0_0.DescribeSensor, class net.opengis.sos.v_1_0_0.GetObservationById, class net.opengis.sos.v_1_0_0.GetFeatureOfInterestTime, class net.opengis.sos.v_1_0_0.GetResult, class net.opengis.sos.v_1_0_0.RegisterSensor, class net.opengis.sos.v_1_0_0.GetObservation, class net.opengis.sos.v_1_0_0.DescribeObservationType, class net.opengis.sos.v_1_0_0.GetFeatureOfInterest])
  at net.opengis.sos.v_1_0_0.InsertObservation
There's no ObjectFactory with an @XmlElementDecl for the element {http://www.opengis.net/ogc}temporalOps.
 this problem is related to the following location:
  at protected javax.xml.bind.JAXBElement net.opengis.sos.v_1_0_0.GetFeatureOfInterest$EventTime.temporalOps
  at net.opengis.sos.v_1_0_0.GetFeatureOfInterest$EventTime
  at public net.opengis.sos.v_1_0_0.GetFeatureOfInterest$EventTime net.opengis.sos.v_1_0_0.ObjectFactory.createGetFeatureOfInterestEventTime()
  at net.opengis.sos.v_1_0_0.ObjectFactory
  at protected javax.xml.bind.JAXBElement net.opengis.sos.v_1_0_0.GetResult$EventTime.temporalOps
  at net.opengis.sos.v_1_0_0.GetResult$EventTime
  at protected java.util.List net.opengis.sos.v_1_0_0.GetResult.eventTime
  at net.opengis.sos.v_1_0_0.GetResult
  at @javax.xml.bind.annotation.XmlSeeAlso(value=[class net.opengis.sos.v_1_0_0.DescribeFeatureType, class net.opengis.sos.v_1_0_0.DescribeResultModel, class net.opengis.sos.v_1_0_0.InsertObservation, class net.opengis.sos.v_1_0_0.DescribeSensor, class net.opengis.sos.v_1_0_0.GetObservationById, class net.opengis.sos.v_1_0_0.GetFeatureOfInterestTime, class net.opengis.sos.v_1_0_0.GetResult, class net.opengis.sos.v_1_0_0.RegisterSensor, class net.opengis.sos.v_1_0_0.GetObservation, class net.opengis.sos.v_1_0_0.DescribeObservationType, class net.opengis.sos.v_1_0_0.GetFeatureOfInterest])
  at net.opengis.sos.v_1_0_0.InsertObservation
There's no ObjectFactory with an @XmlElementDecl for the element {http://www.opengis.net/ogc}temporalOps.
 this problem is related to the following location:
  at protected javax.xml.bind.JAXBElement net.opengis.sos.v_1_0_0.GetObservation$EventTime.temporalOps
  at net.opengis.sos.v_1_0_0.GetObservation$EventTime
  at public net.opengis.sos.v_1_0_0.GetObservation$EventTime net.opengis.sos.v_1_0_0.ObjectFactory.createGetObservationEventTime()
  at net.opengis.sos.v_1_0_0.ObjectFactory
  at protected javax.xml.bind.JAXBElement net.opengis.sos.v_1_0_0.GetResult$EventTime.temporalOps
  at net.opengis.sos.v_1_0_0.GetResult$EventTime
  at protected java.util.List net.opengis.sos.v_1_0_0.GetResult.eventTime
  at net.opengis.sos.v_1_0_0.GetResult
  at @javax.xml.bind.annotation.XmlSeeAlso(value=[class net.opengis.sos.v_1_0_0.DescribeFeatureType, class net.opengis.sos.v_1_0_0.DescribeResultModel, class net.opengis.sos.v_1_0_0.InsertObservation, class net.opengis.sos.v_1_0_0.DescribeSensor, class net.opengis.sos.v_1_0_0.GetObservationById, class net.opengis.sos.v_1_0_0.GetFeatureOfInterestTime, class net.opengis.sos.v_1_0_0.GetResult, class net.opengis.sos.v_1_0_0.RegisterSensor, class net.opengis.sos.v_1_0_0.GetObservation, class net.opengis.sos.v_1_0_0.DescribeObservationType, class net.opengis.sos.v_1_0_0.GetFeatureOfInterest])
  at net.opengis.sos.v_1_0_0.InsertObservation
There's no ObjectFactory with an @XmlElementDecl for the element {http://www.opengis.net/ogc}comparisonOps.
 this problem is related to the following location:
  at protected javax.xml.bind.JAXBElement net.opengis.sos.v_1_0_0.GetObservation$Result.comparisonOps
  at net.opengis.sos.v_1_0_0.GetObservation$Result
  at public net.opengis.sos.v_1_0_0.GetObservation$Result net.opengis.sos.v_1_0_0.ObjectFactory.createGetObservationResult()
  at net.opengis.sos.v_1_0_0.ObjectFactory
  at protected javax.xml.bind.JAXBElement net.opengis.sos.v_1_0_0.GetResult$EventTime.temporalOps
  at net.opengis.sos.v_1_0_0.GetResult$EventTime
  at protected java.util.List net.opengis.sos.v_1_0_0.GetResult.eventTime
  at net.opengis.sos.v_1_0_0.GetResult
  at @javax.xml.bind.annotation.XmlSeeAlso(value=[class net.opengis.sos.v_1_0_0.DescribeFeatureType, class net.opengis.sos.v_1_0_0.DescribeResultModel, class net.opengis.sos.v_1_0_0.InsertObservation, class net.opengis.sos.v_1_0_0.DescribeSensor, class net.opengis.sos.v_1_0_0.GetObservationById, class net.opengis.sos.v_1_0_0.GetFeatureOfInterestTime, class net.opengis.sos.v_1_0_0.GetResult, class net.opengis.sos.v_1_0_0.RegisterSensor, class net.opengis.sos.v_1_0_0.GetObservation, class net.opengis.sos.v_1_0_0.DescribeObservationType, class net.opengis.sos.v_1_0_0.GetFeatureOfInterest])
  at net.opengis.sos.v_1_0_0.InsertObservation
There's no ObjectFactory with an @XmlElementDecl for the element {http://www.opengis.net/ogc}spatialOps.
 this problem is related to the following location:
  at protected javax.xml.bind.JAXBElement net.opengis.sos.v_1_0_0.GetFeatureOfInterest$Location.spatialOps
  at net.opengis.sos.v_1_0_0.GetFeatureOfInterest$Location
  at protected net.opengis.sos.v_1_0_0.GetFeatureOfInterest$Location net.opengis.sos.v_1_0_0.GetFeatureOfInterest.location
  at net.opengis.sos.v_1_0_0.GetFeatureOfInterest
  at public net.opengis.sos.v_1_0_0.GetFeatureOfInterest net.opengis.sos.v_1_0_0.ObjectFactory.createGetFeatureOfInterest()
  at net.opengis.sos.v_1_0_0.ObjectFactory
  at protected javax.xml.bind.JAXBElement net.opengis.sos.v_1_0_0.GetResult$EventTime.temporalOps
  at net.opengis.sos.v_1_0_0.GetResult$EventTime
  at protected java.util.List net.opengis.sos.v_1_0_0.GetResult.eventTime
  at net.opengis.sos.v_1_0_0.GetResult
  at @javax.xml.bind.annotation.XmlSeeAlso(value=[class net.opengis.sos.v_1_0_0.DescribeFeatureType, class net.opengis.sos.v_1_0_0.DescribeResultModel, class net.opengis.sos.v_1_0_0.InsertObservation, class net.opengis.sos.v_1_0_0.DescribeSensor, class net.opengis.sos.v_1_0_0.GetObservationById, class net.opengis.sos.v_1_0_0.GetFeatureOfInterestTime, class net.opengis.sos.v_1_0_0.GetResult, class net.opengis.sos.v_1_0_0.RegisterSensor, class net.opengis.sos.v_1_0_0.GetObservation, class net.opengis.sos.v_1_0_0.DescribeObservationType, class net.opengis.sos.v_1_0_0.GetFeatureOfInterest])
  at net.opengis.sos.v_1_0_0.InsertObservation

After googling for a while I found this post on a mailing list. Now I am not a fan of editing auto-generated code, so I went looking for a better solution and read up on the documentation for the XmlSeeAlso annotation, specifically:

The user would be required to create JAXBContext as JAXBContext.newInstance(Dog.class,Cat.class)


Hmm... So after reading this I gave this a quick whirl:

import net.opengis.sensorml.v_1_0_1.ProcessChainType;
import net.opengis.sos.v_1_0_0.InsertObservation;
import net.opengis.sos.v_1_0_0.filter.v_1_1_0.BinaryTemporalOpType;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class JAXBTest {
    public static void main(String[] args) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(InsertObservation.class, BinaryTemporalOpType.class, ProcessChainType.class);
        InsertObservation insert = new InsertObservation();
        Marshaller mar = context.createMarshaller();
        mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        mar.marshal(insert, System.out);

    }
}

Yeah! Success:





The thing to note is that these two extra classes are not in the same package or jar file so cannot be found by the context loader, hence having to specifically add them.

Tuesday, June 22, 2010

Visual Basic UTF8

After much googling and messing about I have some UTF8 conversion functions for Visual Basic (vb).

Option Compare Database
Public Const UTF8 = 65001
Public Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
Public Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cbMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long

'
'Converts Unicode to UTF8. It wraps the system call WideCharToMultiByte.
'
Public Function UTF8_Encode_System(ByVal sStr As String) As String
    Dim buffer As String
    Dim length As Long
    
    'Get the length of the converted data.
    length = WideCharToMultiByte(UTF8, 0, StrPtr(sStr), Len(sStr), 0, 0, 0, 0)
    
    'Ensure the buffer is the correct size.
    buffer = String$(length, 0)
    
    'Convert the string into the buffer.
    length = WideCharToMultiByte(UTF8, 0, StrPtr(sStr), Len(sStr), StrPtr(buffer), Len(buffer), 0, 0)
    
    'Access needs it in unicode?
    buffer = StrConv(buffer, vbUnicode)
    
    'Chop of any crap.
    buffer = Left$(buffer, length)
    
    'Return baby.
    UTF8_Encode_System = buffer
End Function

'
'Converts UTF8 data into unicode. It wrapps the system call MultiByteToWideChar.
'
Public Function UTF8_Decode_System(ByVal sStr As String) As String
    Dim buffer As String
    Dim length As Long
    
    'Get the length of the converted data.
    length = MultiByteToWideChar(UTF8, 0, StrPtr(StrConv(sStr, vbFromUnicode)), Len(sStr), 0, 0)
    
    'Ensure the buffer is the correct size.
    buffer = String$(length, 0)
    
    'Convert the data into the buffer.
    length = MultiByteToWideChar(UTF8, 0, StrPtr(StrConv(sStr, vbFromUnicode)), Len(sStr), StrPtr(buffer), Len(buffer))
    
    'Chop off any crap.
    buffer = Left$(buffer, length)
    
    'Return baby.
    UTF8_Decode_System = buffer
End Function