vendredi 12 août 2016

Reflection through Xerces C++ 3.1

I am working on a project verifying several parameters from an xml file as part of the application logic. The schema of the file is roughly as follows

<xs:element name="root">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="TypeA" type="TypeA" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

 <xs:complexType name="TypeA">
    <xs:sequence>
        <xs:element name="Foo" type="Foo" minOccurs="0" maxOccurs="1"/>
        <xs:element name="Foo1" type="Foo1" minOccurs="0" maxOccurs="n"/>
        <xs:choice minOccurs="1" maxOccurs="1">
            <xs:element name="TypeB" type="TypeB" minOccurs="0" maxOccurs="1"/>
            <xs:element name="TypeC" type="TypeC" minOccurs="0" maxOccurs="1"/>
            <xs:element name="TypeD" type="TypeD" minOccurs="0" maxOccurs="1"/>
            <xs:element name="TypeE" type="TypeE" minOccurs="0" maxOccurs="1"/>
        </xs:choice>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="TypeB">
    <xs:sequence>
        <xs:element name="ParamA" type="customParamType1" minOccurs="0" maxOccurs="1"/>
        <xs:element name="ParamB" type="customParamType2" minOccurs="0" maxOccurs="1"/>
        <xs:element name="ParamC" type="customParamType3" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
</xs:complexType>

I need to compare the parameters of TypesB-E to a known value. The child keys of the TypesB-E will always be one of three custom parameter types. In Java, writing generic logic to this is trivial using reflection. Pseudo-Code:

List = xmlrootobject.getTypeA.getTypeB.getMethods() //Returns a get to each key present in xml
for each method in list
    switch(method.class) // Part of the api returns the class each method returns
      case customParamType1:
        handleCustomParamType1(method)
        break;
      case customParamType2:
        handleCustomParamType2(method)
        break;
      case customParamType3:
        customParamType2(method)

In C++ I am using code synthesis xsd which uses an undelrying xerces dom to build an object model. xsd flattens the xml into an object model which explicitly requires .gets() of any possible element to validate. This gets tedious really fast. I am looking for a generic way to loop through all the children of types B,C,D,E, determine the xml type of each child, then run logic based on the type returned. I was looking into using the underlying xerces DOM interface to do this as it does provide a generic "getFirstChild","getNextChild" interface, but doesn't seem to provide a way to tell me if the child returned is a "customParamType1" or "customParamType2" etc...

Any advice on how to implement a reflection like api for runtime determined xml objects? Keep in mind it would be less work to manually implement "if getElement() != null" for every possible field than it would be to gut the project for a different XML library.





Aucun commentaire:

Enregistrer un commentaire