org.jibx.runtime.JiBXException Java Examples

The following examples show how to use org.jibx.runtime.JiBXException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: M2Model.java    From alfresco-data-model with GNU Lesser General Public License v3.0 6 votes vote down vote up
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);
    }
}
 
Example #2
Source File: JibxHelper.java    From tr069-simulator with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example #3
Source File: LegendGraphicServiceTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
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;
		}
	}
}
 
Example #4
Source File: StyleConverterServiceTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
@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);
}
 
Example #5
Source File: StyleConverterServiceTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@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());
}
 
Example #6
Source File: JibxMarshaller.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void marshalOutputStream(Object graph, OutputStream outputStream)
		throws XmlMappingException, IOException {
	try {
		IMarshallingContext marshallingContext = createMarshallingContext();
		marshallingContext.startDocument(this.encoding, this.standalone, outputStream);
		marshalDocument(marshallingContext, graph);
	}
	catch (JiBXException ex) {
		throw convertJibxException(ex, true);
	}
}
 
Example #7
Source File: JibxMarshaller.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException {
	try {
		IMarshallingContext marshallingContext = createMarshallingContext();
		marshallingContext.startDocument(this.encoding, this.standalone, writer);
		marshalDocument(marshallingContext, graph);
	}
	catch (JiBXException ex) {
		throw convertJibxException(ex, true);
	}
}
 
Example #8
Source File: JibxMarshaller.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void marshalDocument(IMarshallingContext marshallingContext, Object graph) throws IOException, JiBXException {
	if (StringUtils.hasLength(docTypeRootElementName)) {
		IXMLWriter xmlWriter = marshallingContext.getXmlWriter();
		xmlWriter.writeDocType(docTypeRootElementName, docTypeSystemId, docTypePublicId, docTypeInternalSubset);
	}
	marshallingContext.marshalDocument(graph);
}
 
Example #9
Source File: JibxMarshaller.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException {
	try {
		MarshallingContext marshallingContext = (MarshallingContext) createMarshallingContext();
		IXMLWriter xmlWriter = new StAXWriter(marshallingContext.getNamespaces(), streamWriter);
		marshallingContext.setXmlWriter(xmlWriter);
		marshallingContext.marshalDocument(graph);
	}
	catch (JiBXException ex) {
		throw convertJibxException(ex, false);
	}
}
 
Example #10
Source File: JibxMarshaller.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) {
	try {
		UnmarshallingContext unmarshallingContext = (UnmarshallingContext) createUnmarshallingContext();
		IXMLReader xmlReader = new StAXReaderWrapper(streamReader, null, true);
		unmarshallingContext.setDocument(xmlReader);
		return unmarshallingContext.unmarshalElement();
	}
	catch (JiBXException ex) {
		throw convertJibxException(ex, false);
	}
}
 
Example #11
Source File: JibxMarshaller.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
	try {
		IUnmarshallingContext unmarshallingContext = createUnmarshallingContext();
		return unmarshallingContext.unmarshalDocument(inputStream, encoding);
	}
	catch (JiBXException ex) {
		throw convertJibxException(ex, false);
	}
}
 
Example #12
Source File: JibxMarshaller.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException {
	try {
		IUnmarshallingContext unmarshallingContext = createUnmarshallingContext();
		return unmarshallingContext.unmarshalDocument(reader);
	}
	catch (JiBXException ex) {
		throw convertJibxException(ex, false);
	}
}
 
Example #13
Source File: JibxMarshaller.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the given {@code JiBXException} to an appropriate exception from the
 * {@code org.springframework.oxm} hierarchy.
 * <p>A boolean flag is used to indicate whether this exception occurs during marshalling or
 * unmarshalling, since JiBX itself does not make this distinction in its exception hierarchy.
 * @param ex {@code JiBXException} that occured
 * @param marshalling indicates whether the exception occurs during marshalling ({@code true}),
 * or unmarshalling ({@code false})
 * @return the corresponding {@code XmlMappingException}
 */
public XmlMappingException convertJibxException(JiBXException ex, boolean marshalling) {
	if (ex instanceof ValidationException) {
		return new ValidationFailureException("JiBX validation exception", ex);
	}
	else {
		if (marshalling) {
			return new MarshallingFailureException("JiBX marshalling exception", ex);
		}
		else {
			return new UnmarshallingFailureException("JiBX unmarshalling exception", ex);
		}
	}
}
 
Example #14
Source File: JibxTest.java    From journaldev with MIT License 5 votes vote down vote up
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;
}
 
Example #15
Source File: JibxTest.java    From journaldev with MIT License 5 votes vote down vote up
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();
	}	
}
 
Example #16
Source File: MLPRequest.java    From gmlc with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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;
  }
}
 
Example #17
Source File: JibxMarshaller.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws JiBXException {
	if (this.targetClass != null) {
		if (StringUtils.hasLength(this.bindingName)) {
			if (logger.isInfoEnabled()) {
				logger.info("Configured for target class [" + this.targetClass + "] using binding [" + this.bindingName + "]");
			}
			this.bindingFactory = BindingDirectory.getFactory(this.bindingName, this.targetClass);
		}
		else {
			if (logger.isInfoEnabled()) {
				logger.info("Configured for target class [" + this.targetClass + "]");
			}
			this.bindingFactory = BindingDirectory.getFactory(this.targetClass);
		}
	}
	else if (this.targetPackage != null) {
		if (!StringUtils.hasLength(bindingName)) {
			bindingName = DEFAULT_BINDING_NAME;
		}
		if (logger.isInfoEnabled()) {
			logger.info("Configured for target package [" + this.targetPackage	+ "] using binding [" + this.bindingName + "]");
		}
		this.bindingFactory = BindingDirectory.getFactory(this.bindingName, this.targetPackage);
	}
	else {
		throw new IllegalArgumentException("Either 'targetClass' or 'targetPackage' is required");
	}
}
 
Example #18
Source File: StyleConverterServiceTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@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());

}
 
Example #19
Source File: StyleConverterServiceTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@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());

}
 
Example #20
Source File: StyleConverterServiceTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #21
Source File: CustomerUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@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());

}
 
Example #22
Source File: CustomerUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@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());

}
 
Example #23
Source File: CustomerUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@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());

}
 
Example #24
Source File: JibxHelper.java    From tr069-simulator with MIT License 5 votes vote down vote up
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;
}
 
Example #25
Source File: JibXXMLMapper.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #26
Source File: JibXXMLMapper.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example #27
Source File: JibxMarshaller.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void marshalDocument(IMarshallingContext marshallingContext, Object graph) throws IOException, JiBXException {
	if (StringUtils.hasLength(this.docTypeRootElementName)) {
		IXMLWriter xmlWriter = marshallingContext.getXmlWriter();
		xmlWriter.writeDocType(this.docTypeRootElementName, this.docTypeSystemId,
				this.docTypePublicId, this.docTypeInternalSubset);
	}
	marshallingContext.marshalDocument(graph);
}
 
Example #28
Source File: JibxMarshaller.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void marshalOutputStream(Object graph, OutputStream outputStream)
		throws XmlMappingException, IOException {
	try {
		IMarshallingContext marshallingContext = createMarshallingContext();
		marshallingContext.startDocument(this.encoding, this.standalone, outputStream);
		marshalDocument(marshallingContext, graph);
	}
	catch (JiBXException ex) {
		throw convertJibxException(ex, true);
	}
}
 
Example #29
Source File: JibxMarshaller.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException {
	try {
		IMarshallingContext marshallingContext = createMarshallingContext();
		marshallingContext.startDocument(this.encoding, this.standalone, writer);
		marshalDocument(marshallingContext, graph);
	}
	catch (JiBXException ex) {
		throw convertJibxException(ex, true);
	}
}
 
Example #30
Source File: JibxMarshaller.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void marshalDocument(IMarshallingContext marshallingContext, Object graph) throws IOException, JiBXException {
	if (StringUtils.hasLength(this.docTypeRootElementName)) {
		IXMLWriter xmlWriter = marshallingContext.getXmlWriter();
		xmlWriter.writeDocType(this.docTypeRootElementName, this.docTypeSystemId,
				this.docTypePublicId, this.docTypeInternalSubset);
	}
	marshallingContext.marshalDocument(graph);
}