XMLSerialization issue - xml-serialization

I need to generate the XML file which will be consumed by the external application. And I have observed that it depends on ordering of the namespace defined in the XML. Since the xml is consumed by external application, I don't have the exact error details.
Below is the sample code
[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.4.0.32990")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://mycompany.com/2010/package")]
[System.Xml.Serialization.XmlRootAttribute("Catalog", Namespace="http://mycompany.com/2010/package", IsNullable=false)]
public partial class PackageT : System.ComponentModel.INotifyPropertyChanged {
}
Extension class written in order to include custom schemalocation
public partial class PackageT
{
[XmlAttributeAttribute("schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string xsiSchemaLocation = "http://mycompany.com/2010/catalog.xsd";
}
Code to serialize the object to XML
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("xml", "http://www.w3.org/XML/1998/namespace");
namespaces.Add("test", "http://mycompany.com/2010/package");
XmlSerializer serializer = new XmlSerializer(typeof(PackageT));
TextWriter writer = new StreamWriter("package.xml");
serializer.Serialize(writer, catalog, namespaces);
writer.Flush();
writer.Close();
Generated XML ( not working)
<?xml version="1.0" encoding="utf-8"?>
<test:Catalog
xmlns:xml="http://www.w3.org/XML/1998/namespace"
d1p1:schemaLocation="http://mycompany.com/2010/catalog.xsd"
xmlns:d1p1="http://www.w3.org/2001/XMLSchema-instance" xmlns:test="http://mycompany.com/2010/package">
....
</test:Catalog>
EXpected XML which is working
<?xml version="1.0" encoding="utf-8"?>
<test:Catalog d1p1:schemaLocation="http://mycompany.com/2010/catalog.xsd"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:d1p1="http://www.w3.org/2001/XMLSchema-instance" xmlns:test="http://mycompany.com/2010/package">
....
</test:Catalog>
Please help me!!

If you specify the element name and namespace for the root you can generate the XML you're looking for. Modify PackageT as below:
[XmlRoot(ElementName = "Catalog", Namespace = "http://mycompany.com/2010/package") ]
public partial class PackageT
{
[XmlAttribute("schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string xsiSchemaLocation = "http://mycompany.com/2010/catalog.xsd";
}
and the output becomes:
<?xml version="1.0" encoding="utf-8"?>
<test:Catalog xmlns:xml="http://www.w3.org/XML/1998/namespace"
d1p1:schemaLocation="http://mycompany.com/2010/catalog.xsd"
xmlns:d1p1="http://www.w3.org/2001/XMLSchema-instance"
xmlns:test="http://mycompany.com/2010/package" />

Related

Is it possible to apply xml Templates in JAVA DSL Runner

we have a lot of old citrus xml Testcases and templates in our Projects. After Upgrading to newer version I decided to make the switch to Java DSL. Is it possible to keep using the old templates? If i Try to do so, I get a "No bean named .. is defined" exception.
I tried the to import the template file via #ImportResource but without success.
You can write a simple custom test action that loads the template and executes it with current test context:
Given the following template in templates/hello-template.xml
<spring:beans xmlns="http://www.citrusframework.org/schema/testcase"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.citrusframework.org/schema/testcase http://www.citrusframework.org/schema/testcase/citrus-testcase.xsd">
<template name="helloTemplate">
<echo>
<message>Hello ${user}</message>
</echo>
</template>
</spring:beans>
You can write a custom test action for loading that template:
public class TemplateTest extends TestNGCitrusTestRunner {
#Test
#CitrusTest
public void test() {
run(new CallTemplateAction("templates/hello-template.xml", "helloTemplate"));
}
private class CallTemplateAction extends AbstractTestAction {
private final String templateName;
private final String templateLocation;
public CallTemplateAction(String templateLocation, String templateName) {
this.templateLocation = templateLocation;
this.templateName = templateName;
}
#Override
public void doExecute(TestContext testContext) {
Template template = new ClassPathXmlApplicationContext(new String[] { templateLocation },
testContext.getApplicationContext())
.getBean(templateName, Template.class);
template.getParameter().put("user", "foo");
template.execute(testContext);
}
}
}
You should probably cache the template instance and/or close the application context when done with the action.

"Type class is not known to the MapperRegistry" in MyBatis

My Problem
I'm getting the error Type class myPackage.MyClass is not known to the MapperRegistry.
I successfully acquired a session, and upon debugging I can see that it otherwise appears to be configured correctly so the interface association seems to be working; therefor I'm confident this error is distinct from the stack-overflow-suggested Type interface is not known... question.
I'm new to myBatis but from the documentation I understood that the following was all that was required to get resultType auto-mapping to work.
Update: This also happens when mapping the mapper resources by xml file instead of by class.
My Mapper
public interface MyClassMapper{
MyClass getMyClass(Integer id);
}
My Model
public class MyClass{
private String itemValue;
public String getItemValue() {
return itemValue;
}
public void setItemValue(String itemValue) {
this.itemValue = itemValue;
}
}
My Sql Map
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="myPackage.orm.sqlMap.MyClassMapper" >
<select id="getMyClass" resultType="myPackage.MyClass" >
select itemValue
from SOME_TABLE
WHERE id = #{id}
</select>
</mapper>
My mybatis-config.xml
...
<mappers>
<mapper class="myPackage.MyClass" />
</mappers>
...
Fixed:
public MyClass getMyClassValue(Integer id) throws Exception{
SqlSession session = MyBatisSessionFactory.openSession();
MyClassMapper mapper = (MyClassMapper) session.getMapper(MyClass.class);
return mapper.getMyClass(id);
}
Here is the code I was using to execute the query, discovered that I was looking up the mapper in the mapper registry by the model class name, rather than the mapper interface name. Works just fine now.
In your mapper.xml file mapper's namespace should be the path to the mapper interface.
for example:
<mapper namespace="com.mapper.LineMapper">
<select id="selectLine" resultType="com.jiaotong114.jiaotong.beans.Line">
select * from bus_line where id = #{id}
</select>
</mapper>
your mapper interface should be in com.mapper package and the name of it is LineMapper.
hope help.
I solved this issue by adding the mapper XML to the mybatis xml configuration file
<mappers>
<mapper resource="com/java/Mapper.xml"/>
</mappers>

MvvmCross passing configuration to configurable plugin loader

I am creating a portable MockGeoLocationWatcher that one can substitute in place of the concrete implementations of IMvxGeoLocationWatcher until one has an actual device. This should facilitate development and testing of applications that require geo location.
The PluginLoader class for this plugin currently looks like this:
namespace Pidac.MvvmCross.Plugins.Location
{
public class PluginLoader : IMvxConfigurablePluginLoader
{
private bool _loaded;
public static readonly PluginLoader Instance = new PluginLoader();
public void EnsureLoaded()
{
if (_loaded)
return;
_loaded = true;
var locationWatcher = new MockGeoLocationWatcher();
var data = #"<?xml version='1.0' encoding='utf-8'?>
<WindowsPhoneEmulator xmlns='http://schemas.microsoft.com/WindowsPhoneEmulator/2009/08/SensorData'>
<SensorData>
<Header version='1' />
<GpsData latitude='48.619934106826' longitude='-84.5247359841114' />
<GpsData latitude='48.6852544862377' longitude='-83.9864059059864' />
<GpsData latitude='48.8445703681025' longitude='-83.7337203591114' />
<GpsData latitude='48.8662561090809' longitude='-83.2393355934864' />
<GpsData latitude='49.0825970371386' longitude='-83.0415816872364' />
<GpsData latitude='49.2621642999055' longitude='-82.7229781716114' />
<GpsData latitude='49.2621642999055' longitude='-82.6021285622364' />
<GpsData latitude='49.2047736379815' longitude='-82.3054977028614' />
</SensorData>
</WindowsPhoneEmulator>";
locationWatcher.SensorLocationData = data;
Mvx.RegisterSingleton(typeof(IMvxGeoLocationWatcher), locationWatcher);
}
public void Configure(IMvxPluginConfiguration configuration)
{
}
}
public class MockLocationWatcherConfiguration : IMvxPluginConfiguration
{
public static readonly MockLocationWatcherConfiguration Default = new MockLocationWatcherConfiguration();
// ideally, we should use this property to point to a file or string containing location data
// this should be configurable outside of code base.
public string SensorLocationData { get; set; }
}
}
I will like to pass the sensor data, currently hardcoded into the variable called "data" through an instance of MockLocationWatcherConfiguration but do not know where the MvvmCross framework is expecting to load the configuration for this plugin before IMvxConfigurablePluginLoader.Configure(configuration) is invoked. Ideally, I should specify this through configuration.
I looked at the Json plugin's implementation of PluginLoaded but still could not figure out where the configuration was retrieved before a cast was attempted in IMvxConfigurablePluginLoader.Configure.
Any ideas or pointers will be greatly appreciated.
TIA.
This is covered in the draft wiki page https://github.com/slodge/MvvmCross/wiki/MvvmCross-plugins - see "writing a configurable plugin"

XSD2Code namespace issue

I am using XSD2Code to generate C# class from XSD file.
I got stuck with the following problem.
XML file looks something like
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Notification xmlns="http://message.domain.com">
<Object xmlns="http://type.domain.com" ID="97440" />
</Notification>
XML gets succefsully deserialized when xmls for Object is empty. But when there is a value like in the sample above, I get an error "Object reference not set to an instance of an object".
What could cause this error?
you have to change the Serializer to something like that
private static System.Xml.Serialization.XmlSerializer Serializer
{
get
{
if ((serializer == null))
{
serializer = new System.Xml.Serialization.XmlSerializer(typeof(Notification), "http://message.domain.com");
}
return serializer;
}
}
To turn off encoding, disable encoding on the Serialization tab

get list of services implementations with OSGi declarative services

I have a very simple example of declarative services. I'm following this tutorial http://www.eclipsezone.com/eclipse/forums/t97690.html?start=0. Every thing is working as expected. However, I cannot figure out how I can make the "SampleImporter" (which is the bundle that is expected to use other bundles' services) aware of the list of "SampleExporter" (bundle providing a service). In other words, I want the "SamlpeImporter" to see the ID of the bundle(s) that it is eventually using. This information is very useful for my application.
here is the XML file for SampleExporter:
<?xml version="1.0"?>
<component name="samplerunnable">
<implementation class="org.example.ds.SampleRunnable"/>
<property name="ID" value="expoter" />
<service>
<provide interface="java.lang.Runnable"/>
</service>
while for the SampleImporter:
<?xml version="1.0"?>
<component name="commandprovider1">
<implementation class="org.example.ds.SampleCommandProvider1"/>
<service>
<provide interface="org.eclipse.osgi.framework.console.CommandProvider"/>
</service>
<reference name="RUNNABLE"
interface="java.lang.Runnable"
bind="setRunnable"
unbind="unsetRunnable"
cardinality="0..1"
policy="dynamic"/>
</component>
In the Importer side, I have the following function:
public class SampleCommandProvider1 implements CommandProvider {
private Runnable runnable;
public synchronized void setRunnable(Runnable r) {
runnable = r;
}
public synchronized void unsetRunnable(Runnable r) {
runnable = null;
}
public synchronized void _run(CommandInterpreter ci) {
if(runnable != null) {
runnable.run();
} else {
ci.println("Error, no Runnable available");
}
}
public String getHelp() {
return "\trun - execute a Runnable service";
}
}
This works fine but then if I want to get the value of the property, using
public synchronized void setRunnable(Runnable r, Map properties)
or
public synchronized void setRunnable(Runnable r, ServiceReference reference)
the method run of the exporter is never called which means that the bind function (setRunnable is not called).Hwever, using the console command "services" I see that the exporter bundle is used by the imporeter one. Also, using ss and ls I can see that the component eporter is "satisfied".
What is wrong with my implementetion?
Thanks in advance
Cheers
Marie
The following bind signature is not supported by any version of DS:
public void setRunnable(Runnable r, ServiceReference ref)
Instead you will have to take only the ServiceReference and use either the ComponentContext or BundleContext to access the service instance object.
Alternatively if you want a more POJO-style way of accessing service properties, the following bind signature is allowed in DS 1.1 (but not in DS 1.0):
public void setRunnable(Runnable r, Map properties)
To access DS 1.1 features, you need to add the correct namespace to your XML as follows:
<component xmlns='http://www.osgi.org/xmlns/scr/v1.1.0' name='...'>
By the way, I wrote this original article a very long time ago! These days I would use bnd annotations to avoid having to write the XML document by hand.