Java源码示例:org.jibx.runtime.IBindingFactory
示例1
public void toXML(ModelDefinition.XMLBindingType bindingType, OutputStream xml)
{
try
{
if(bindingType == null)
{
bindingType = ModelDefinition.XMLBindingType.DEFAULT;
}
String bindingName = bindingType.toString();
IBindingFactory factory = (bindingName != null) ? BindingDirectory.getFactory(bindingName, M2Model.class) :
BindingDirectory.getFactory("default", M2Model.class);
IMarshallingContext context = factory.createMarshallingContext();
context.setIndent(4);
context.marshalDocument(this, "UTF-8", null, xml);
}
catch(JiBXException e)
{
throw new DictionaryException(ERR_CREATE_M2MODEL_FAILURE, e);
}
}
示例2
private SymbolizerTypeInfo toStyle(String sld) throws Exception {
String style = "<StyledLayerDescriptor version=\"1.0.0\"\n" +
" xsi:schemaLocation=\"http://www.opengis.net/sld StyledLayerDescriptor.xsd\" \n" +
" xmlns=\"http://www.opengis.net/sld\" \n" +
" xmlns:ogc=\"http://www.opengis.net/ogc\" \n" +
" xmlns:xlink=\"http://www.w3.org/1999/xlink\" \n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
" <NamedLayer>\n" +
" <Name>test</Name>\n" +
" <UserStyle>\n" +
" <Title>test</Title>\n" +
" <FeatureTypeStyle>\n" +
" <Rule>\n" +
""+sld+" </Rule>\n" +
" </FeatureTypeStyle>\n" +
" </UserStyle>\n" +
" </NamedLayer>\n" +
"</StyledLayerDescriptor>";
IBindingFactory bfact = BindingDirectory.getFactory(StyledLayerDescriptorInfo.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
Object object = uctx.unmarshalDocument(new StringReader(style), null);
return ((StyledLayerDescriptorInfo) object).getChoiceList().get(0).getNamedLayer().getChoiceList().get(0)
.getUserStyle().getFeatureTypeStyleList().get(0).getRuleList().get(0).getSymbolizerList().get(0);
}
示例3
@Test
public void testStyleWithExternalGraphicNoSize() throws JiBXException, LayerException {
IBindingFactory bfact = BindingDirectory.getFactory(StyledLayerDescriptorInfo.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
Object object = uctx.unmarshalDocument(
getClass().getResourceAsStream("/org/geomajas/testdata/sld/point_externalgraphicnosize.sld"),
null);
StyledLayerDescriptorInfo sld = (StyledLayerDescriptorInfo) object;
NamedStyleInfo info = styleConverterService.convert(sld.getChoiceList().get(0).getNamedLayer().getChoiceList()
.get(0).getUserStyle(), featureInfo);
Assert.assertNotNull(info);
}
示例4
public SimpleRulesData(String ruleName, int width, int height) throws JiBXException {
this.width = width;
this.height = height;
IBindingFactory bfact = BindingDirectory.getFactory(StyledLayerDescriptorInfo.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
Object object = uctx.unmarshalDocument(
getClass().getResourceAsStream("/org/geomajas/testdata/sld/simple_rules.sld"), null);
StyledLayerDescriptorInfo sld = (StyledLayerDescriptorInfo) object;
NamedLayerInfo namedLayerInfo = sld.getChoiceList().get(0).getNamedLayer();
UserStyleInfo userStyleInfo = namedLayerInfo.getChoiceList().get(0).getUserStyle();
FeatureTypeStyleInfo featureTypeStyleInfo = userStyleInfo.getFeatureTypeStyleList().get(0);
for (RuleInfo rule : featureTypeStyleInfo.getRuleList()) {
if (ruleName.equals(rule.getName())) {
ruleInfo = rule;
}
}
}
示例5
/**
* Unmarshal this xml Message to an object.
* @param xml
* @param system
* @return
*/
public static Object unmarshalMessage(String xml, String version)
{
String packageName = "org.dslforum." + version;
String bindingName = "binding";
try {
IBindingFactory jc = BindingDirectory.getFactory(bindingName, packageName);
IUnmarshallingContext unmarshaller = jc.createUnmarshallingContext();
Reader inStream = new StringReader(xml);
Object message = unmarshaller.unmarshalDocument( inStream, bindingName);
return message;
} catch (JiBXException e) {
e.printStackTrace();
}
return null;
}
示例6
/**
* Create System Info from XML representation
*
* @param xml xml representation of system info
* @return the System Info
*/
public static SystemInfo createSystemInfo(InputStream xml)
{
try
{
IBindingFactory factory = BindingDirectory.getFactory(SystemInfo.class);
IUnmarshallingContext context = factory.createUnmarshallingContext();
Object obj = context.unmarshalDocument(xml, null);
return (SystemInfo)obj;
}
catch(JiBXException e)
{
throw new DictionaryException("Failed to parse System Info", e);
}
}
示例7
/**
* Create XML representation of System Info
*
* @param xml xml representation of system info
*/
public void toXML(OutputStream xml)
{
try
{
IBindingFactory factory = BindingDirectory.getFactory(SystemInfo.class);
IMarshallingContext context = factory.createMarshallingContext();
context.setIndent(4);
context.marshalDocument(this, "UTF-8", null, xml);
}
catch(JiBXException e)
{
throw new DictionaryException("Failed to create System Info", e);
}
}
示例8
public static M2Model createModel(String bindingName, InputStream xml)
{
try
{
IBindingFactory factory = BindingDirectory.getFactory(bindingName, M2Model.class);
IUnmarshallingContext context = factory.createUnmarshallingContext();
Object obj = context.unmarshalDocument(xml, null);
return (M2Model)obj;
}
catch(JiBXException e)
{
throw new DictionaryException(ERR_PARSE_FAILURE, e);
}
}
示例9
public String marshalEmployee(Employee employee){
try {
IBindingFactory bfact = BindingDirectory.getFactory(Employee.class);
IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.setIndent(2);
StringWriter stringWriter = new StringWriter();
mctx.setOutput(stringWriter);
mctx.marshalDocument(employee, "UTF-8", null);
String output = stringWriter.toString();
return output;
} catch (JiBXException e) {
e.printStackTrace();
}
return null;
}
示例10
public void unMarshalEmployee(String inputXml){
try {
IBindingFactory bfact = BindingDirectory.getFactory(Employee.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
StringReader stringReader = new StringReader(inputXml);
Employee employee = (Employee) uctx.unmarshalDocument(stringReader, null);
System.out.println("Employee ID:"+employee.getId());
} catch (JiBXException e) {
e.printStackTrace();
}
}
示例11
/**
* Create the svc_result XML result for any type of result (error or success)
*
* @param mlpSvcResult Fully filled in SvcResult object to marshal (convert to XML)
* @return String of XML result to send to client
* @throws org.jibx.runtime.JiBXException JiBX had an internal failure of some kind while marshalling the XML
* @throws IOException IO error occurred while generating the XML result
*/
private String marshalMlpResult(org.oma.protocols.mlp.svc_result.SvcResult mlpSvcResult)
throws org.jibx.runtime.JiBXException, IOException {
String lXml = null;
IBindingFactory jc = BindingDirectory.getFactory(org.oma.protocols.mlp.svc_result.SvcResult.class);
IMarshallingContext marshaller = jc.createMarshallingContext();
ByteArrayOutputStream lOutputStream = new ByteArrayOutputStream();
marshaller.setOutput(lOutputStream, "UTF-8");
IXMLWriter ix = marshaller.getXmlWriter();
// Add XML and DOCTYPE headers
ix.writeXMLDecl("1.0", "UTF-8", null);
ix.writeDocType("svc_result", "MLP_SVC_RESULT_310.DTD", null, null);
// Set 4 spaces as the default indenting
marshaller.setIndent(4);
// Generate the XML
marshaller.marshalDocument(mlpSvcResult);
// Convert the stream to a string
lXml = new String(lOutputStream.toByteArray(), "UTF-8");
// Return our XML string result
return lXml;
}
示例12
/**
* Parse incoming XML request data via JiBX's unmarshaller and return only the MSISDN being requested
*
* @param requestStream InputStream (likely directly from the HTTP POST) of the XML input data
* @return MSISDN of device to locate
* @throws MLPException
*/
public String parseRequest(InputStream requestStream) throws MLPException {
// Result XML
String requestingserviceid, requestingMSISDN = null;
// Process the request
try {
// Create the JiBX unmarshalling object
IBindingFactory jc = BindingDirectory.getFactory(org.oma.protocols.mlp.svc_init.SvcInit.class);
IUnmarshallingContext unmarshaller = jc.createUnmarshallingContext();
// Unmarshal directly from the POST input stream
org.oma.protocols.mlp.svc_init.SvcInit svcInit = (org.oma.protocols.mlp.svc_init.SvcInit) unmarshaller.unmarshalDocument(requestStream, "UTF-8");
// Process the location request for the specified MSISDN
org.oma.protocols.mlp.svc_init.Msids msids = svcInit.getSlir().getMsids();
org.oma.protocols.mlp.svc_init.Msids.Choice c = msids.getChoiceList().get(0);
org.oma.protocols.mlp.svc_init.Msid msisdn = c.getMsid();
requestingMSISDN = msisdn.getString();
//Process the location request for serviceid
org.oma.protocols.mlp.svc_init.Serviceid serviceidd = svcInit.getHdr().getClient().getServiceid();
requestingserviceid = serviceidd.getServiceid();
this.logger.info("Parsed location request for MSISDN: " + requestingMSISDN);
return requestingMSISDN + ";" + requestingserviceid;
} catch (JiBXException e) {
e.printStackTrace();
this.logger.info("Exception while unmarshalling XML request data: " + e.getMessage());
// Set a custom error message for delivering directly to the client
// and throw a new exception
MLPException mlpException = new MLPException(e.getMessage());
mlpException.setMlpClientErrorMessage("Invalid XML received: " + e.getMessage());
mlpException.setMlpClientErrorType(MLPResponse.MLPResultType.FORMAT_ERROR);
throw mlpException;
}
}
示例13
@Override
public Style convert(UserStyleInfo userStyleInfo) throws LayerException {
IBindingFactory bindingFactory;
try {
// create a dummy SLD root
StyledLayerDescriptorInfo sld = new StyledLayerDescriptorInfo();
sld.setVersion(SLD_VERSION);
StyledLayerDescriptorInfo.ChoiceInfo choice = new StyledLayerDescriptorInfo.ChoiceInfo();
NamedLayerInfo namedLayerInfo = new NamedLayerInfo();
namedLayerInfo.setName(DUMMY_NAMED_LAYER);
NamedLayerInfo.ChoiceInfo userChoice = new NamedLayerInfo.ChoiceInfo();
userChoice.setUserStyle(userStyleInfo);
namedLayerInfo.getChoiceList().add(userChoice);
choice.setNamedLayer(namedLayerInfo);
sld.getChoiceList().add(choice);
// force through Geotools parser
bindingFactory = BindingDirectory.getFactory(StyledLayerDescriptorInfo.class);
IMarshallingContext marshallingContext = bindingFactory.createMarshallingContext();
StringWriter sw = new StringWriter();
marshallingContext.setOutput(sw);
marshallingContext.marshalDocument(sld);
SLDParser parser = new SLDParser(styleFactory, filterService.getFilterFactory());
parser.setOnLineResourceLocator(new ResourceServiceBasedLocator());
parser.setInput(new StringReader(sw.toString()));
Style[] styles = parser.readXML();
if (styles.length != 0) {
return styles[0];
} else {
throw new LayerException(ExceptionCode.INVALID_USER_STYLE, userStyleInfo.getName());
}
} catch (Exception e) {
throw new LayerException(e, ExceptionCode.INVALID_USER_STYLE, userStyleInfo.getName());
}
}
示例14
@Test
public void testSingleStyle() throws JiBXException, LayerException {
IBindingFactory bfact = BindingDirectory.getFactory(StyledLayerDescriptorInfo.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
Object object = uctx.unmarshalDocument(
getClass().getResourceAsStream("/org/geomajas/testdata/sld/single_layer_no_stylename.sld"), null);
StyledLayerDescriptorInfo sld = (StyledLayerDescriptorInfo) object;
NamedStyleInfo info = styleConverterService.convert(sld.getChoiceList().get(0).getNamedLayer().getChoiceList()
.get(0).getUserStyle(), featureInfo);
Assert.assertNotNull(info);
Assert.assertEquals("Some title", info.getName());
}
示例15
@Test
public void testStyleWithAttributeLabel() throws JiBXException, LayerException {
IBindingFactory bfact = BindingDirectory.getFactory(StyledLayerDescriptorInfo.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
Object object = uctx.unmarshalDocument(
getClass().getResourceAsStream("/org/geomajas/testdata/sld/line_labelfollowingline.sld"),
null);
StyledLayerDescriptorInfo sld = (StyledLayerDescriptorInfo) object;
NamedStyleInfo info = styleConverterService.convert(sld.getChoiceList().get(0).getNamedLayer().getChoiceList()
.get(0).getUserStyle(), featureInfo);
Assert.assertNotNull(info);
Assert.assertEquals("name", info.getLabelStyle().getLabelAttributeName());
}
示例16
@Test
public void testStyleWithLiteralLabel() throws JiBXException, LayerException {
IBindingFactory bfact = BindingDirectory.getFactory(StyledLayerDescriptorInfo.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
Object object = uctx.unmarshalDocument(
getClass().getResourceAsStream("/org/geomajas/testdata/sld/line_literallabelfollowingline.sld"),
null);
StyledLayerDescriptorInfo sld = (StyledLayerDescriptorInfo) object;
NamedStyleInfo info = styleConverterService.convert(sld.getChoiceList().get(0).getNamedLayer().getChoiceList()
.get(0).getUserStyle(), featureInfo);
Assert.assertNotNull(info);
Assert.assertEquals("'\u2192'", info.getLabelStyle().getLabelValueExpression());
}
示例17
@Test
public void testStyleWithLiteralCssParameter() throws JiBXException, LayerException {
IBindingFactory bfact = BindingDirectory.getFactory(StyledLayerDescriptorInfo.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
Object object = uctx.unmarshalDocument(
getClass().getResourceAsStream("/org/geomajas/testdata/sld/polygon_literalcssparameter.sld"),
null);
StyledLayerDescriptorInfo sld = (StyledLayerDescriptorInfo) object;
NamedStyleInfo info = styleConverterService.convert(sld.getChoiceList().get(0).getNamedLayer().getChoiceList()
.get(0).getUserStyle(), featureInfo);
Assert.assertNotNull(info);
}
示例18
@Test
public void whenUnmarshalXML_ThenFieldsAreMapped() throws JiBXException, FileNotFoundException {
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
assertEquals("Stefan Jaeger", customer.getPerson().getName());
assertEquals("Davos Dorf", customer.getCity());
}
示例19
@Test
public void WhenUnmarshal_ThenMappingInherited() throws JiBXException, FileNotFoundException {
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
assertEquals(12345, customer.getPerson().getCustomerId());
}
示例20
@Test
public void WhenUnmarshal_ThenPhoneMappingRead() throws JiBXException, FileNotFoundException {
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
assertEquals("234678", customer.getHomePhone().getNumber());
}
示例21
public static Object unmarshalMessage(Class className, String xml, String version) {
String packageName = "org.dslforum." + version;
String bindingName = "binding";
try {
IBindingFactory jc = BindingDirectory.getFactory(bindingName, className);
IUnmarshallingContext unmarshaller = jc.createUnmarshallingContext();
Reader inStream = new StringReader(xml);
Object message = unmarshaller.unmarshalDocument( inStream, bindingName);
return message;
} catch (JiBXException e) {
e.printStackTrace();
}
return null;
}
示例22
/**
* Convert xml string to Java Objects
*/
public Object deserialize(String xml) {
IBindingFactory bfact = null;
StringReader sr = new StringReader(xml);
Object obj = null;
try {
bfact = BindingDirectory.getFactory(bindingName, packageName);
obj = bfact.createUnmarshallingContext().unmarshalDocument(sr);
} catch (JiBXException e) {
log.error(e);
throw new XMLMappingException("Can't deserialize xml: " + xml, e);
}
return obj;
}
示例23
/**
* Convert Java Object to XML String
*/
public String serialize(Object obj) {
IBindingFactory bfact = null;
StringWriter sw = new StringWriter();
try {
bfact = BindingDirectory.getFactory(obj.getClass());
bfact.createMarshallingContext().marshalDocument(obj, "UTF-8", null, sw );
} catch (JiBXException e) {
log.error(e);
throw new XMLMappingException("Can't serialize object: " + obj.toString(), e );
}
return sw.toString();
}
示例24
/**
* Internal XML generation support function for above getSystemErrorResponseXML()
*
* @param mlpClientErrorType Error type to return to client
* @param mlpClientErrorMessage Error message to send to client
* @return String XML result to return to client
* @throws org.jibx.runtime.JiBXException JiBX had an internal failure of some kind while marshalling the XML
* @throws IOException IO error occurred while generating the XML result
*/
private String generateSystemErrorXML(MLPResultType mlpClientErrorType, String mlpClientErrorMessage)
throws org.jibx.runtime.JiBXException, IOException {
String lXml = null;
String ver = "3.1.0";
// Create all the objects we'll use to generate our svc_result XML
org.oma.protocols.mlp.svc_result.SvcResult mlpSvcResult = new org.oma.protocols.mlp.svc_result.SvcResult();
org.oma.protocols.mlp.svc_result.Slia mlpSlia = new org.oma.protocols.mlp.svc_result.Slia();
org.oma.protocols.mlp.svc_result.Result mlpResult = new org.oma.protocols.mlp.svc_result.Result();
org.oma.protocols.mlp.svc_result.AddInfo mlpAddInfo = new org.oma.protocols.mlp.svc_result.AddInfo();
// Set the additional data error message if one is available
if (mlpClientErrorMessage != null) {
mlpAddInfo.setAddInfo(mlpClientErrorMessage);
mlpSlia.setAddInfo(mlpAddInfo);
}
mlpResult.setString(MLPResponse.getResultStringForType(mlpClientErrorType));
mlpResult.setResid(MLPResponse.getResultCodeForType(mlpClientErrorType));
mlpSlia.setResult(mlpResult);
mlpSlia.setVer(ver);
mlpSvcResult.setSlia(mlpSlia);
mlpSvcResult.setVer(ver);
IBindingFactory jc = BindingDirectory.getFactory(org.oma.protocols.mlp.svc_result.SvcResult.class);
IMarshallingContext marshaller = jc.createMarshallingContext();
ByteArrayOutputStream lOutputStream = new ByteArrayOutputStream();
marshaller.setOutput(lOutputStream, "UTF-8");
IXMLWriter ix = marshaller.getXmlWriter();
// Add XML and DOCTYPE headers
ix.writeXMLDecl("1.0", "UTF-8", null);
ix.writeDocType("svc_result", "MLP_SVC_RESULT_310.DTD", null, null);
// Set 4 spaces as the default indenting
marshaller.setIndent(4);
// Generate the XML
marshaller.marshalDocument(mlpSvcResult);
// Convert the stream to a string
lXml = new String(lOutputStream.toByteArray(), "UTF-8");
// Return our XML string result
return lXml;
}