3

I have this collection of classes for manipulating this domain vehicles

Here's my question: I need to save (through parse) data to XML files. To do that I have to create an XML Schema (XSD), but am finding difficulties with regard to inheritance and interfaces.

First, it seems necessary to explain quickly my classes: enter image description here

The Vehicle class is an abstract class and contains the base attributes:

public abstract class Vehicle implements Serializable {

    public enum Stato {
        DISPONIBILE,
        NON_DISPONIBILE
    }

    private String plate;   // targa
    private String mark;    // Marca, Casa Produttrice
    private String model;   // Modello
    private String trim;    // Trim
    private float capacity; // capacità di carico
    // ... other...
    private Stato stato;    // Stato del veicolo
    private String allestimento;

    public Vehicle(){}

    public Vehicle(String plate) {
        this.plate=plate;
    }

    // Get&Set methods 
    // ...
}

And now, for example, Car :

public class Car extends Vehicle implements DrivingPart {

    public Car();

    public Car(String plate) {
        super(plate);
    }

}

... and TrailerTruck:

public class TrailerTruck extends Vehicle {

    // TRAILER TRUCK: autocarro
    // Driving Part: Car, Van, Truck
    // Driven part: Trailer (always)

    String plateFront;
    String plateTrailer;

    DrivingPart drivingVehicle;
    Trailer trailerVehicle;

    public TrailerTruck(DrivingPart drivingVehicle, Trailer trailerVehicle) {
        plateFront=drivingVehicle.getPlate();
        plateTrailer=trailerVehicle.getPlate();
        setPlate(plateFront+" - "+plateTrailer);

        this.drivingVehicle=drivingVehicle;
        this.trailerVehicle=trailerVehicle;
    }

    @Override
    public String getAllestimento() {
        return drivingVehicle.getAllestimento()
                +", "+trailerVehicle.getAllestimento();
    }

    // ...
}

Okay, this functions well. I can easily create objects Vehicle:

Vehicle car = new Car("AAA1");
car.setMark("Peugeot");
car.setModel("206");
car.setStato(Stato.DISPONIBILE);
//...

Vehicle truck = new Truck("AAA2");
truck.setMark("Scania");
//...

Vehicle trailer = new Trailer("TTT1");
trailer.setMark("Menci");
//...

Vehicle tt1 = new TrailerTruck((Truck) truck, (Trailer) trailer);
//...

End of illustration. Sorry if I have dwelt.

EDIT

Here is my attempt at a solution.

ShipperXMLSchema.xsd :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="Fleet" type="FleetType"/>


    <xs:complexType name="FleetType">
        <xs:choice maxOccurs="unbounded">
            <xs:element name="Car" type="CarType"/>
            <xs:element name="Van" type="VanType"/>
            <xs:element name="Truck" type="TruckType"/>
            <xs:element name="Trailer" type="TrailerType"/>
            <xs:element name="RoadTractor" type="RoadTractorType"/> 
            <xs:element name="SemiTrailer" type="SemiTrailerType"/>
            <xs:element name="TrailerTruck" type="TrailerTruckType"/>
            <xs:element name="SemiTrailerTruck" type="SemiTrailerTruckType"/>
        </xs:choice>
        <xs:attribute name="shipperName" type="xs:string"/>
    </xs:complexType>


    <xs:complexType name="VehicleType" abstract="true">
        <xs:sequence>
            <xs:element name="plate" type="xs:string" minOccurs="1" />
            <xs:element name="mark" type="xs:string" minOccurs="0" />
            <xs:element name="model" type="xs:string" minOccurs="1" />
            <xs:element name="trim" type="xs:string" />
            <xs:element name="allestimento" type="xs:string" minOccurs="0" />
            <xs:element name="stato" type="State"/>
            <xs:element name="carryingCapacity" type="xs:float" minOccurs="0" />
            <xs:element name="ptt" type="xs:float" minOccurs="0" />
            <xs:element name="weight" type="xs:float" minOccurs="0" />
            <xs:element name="volume" type="xs:float" minOccurs="0" />
            <xs:element name="length" type="xs:float" minOccurs="0" />
            <xs:element name="height" type="xs:float" minOccurs="0" />
            <xs:element name="width" type="xs:float" minOccurs="0" />
            <xs:element name="locazioneAttuale" type="xs:string" minOccurs="0" />
        </xs:sequence>
        <xs:attribute name="id" type="xs:string"/>
    </xs:complexType>



    <!-- Definitions: tipi Car, Van, Truck, Trailer, RoadTractor, SemiTrailer -->

    <xs:complexType name="CarType">
        <xs:complexContent>
            <xs:extension base="VehicleType"/>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="VanType">
        <xs:complexContent>
            <xs:extension base="VehicleType"/>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="TruckType">
        <xs:complexContent>
            <xs:extension base="VehicleType"/>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="TrailerType">
        <xs:complexContent>
            <xs:extension base="VehicleType"/>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="RoadTractorType">
        <xs:complexContent>
            <xs:extension base="VehicleType"/>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="SemiTrailerType">
        <xs:complexContent>
            <xs:extension base="VehicleType"/>
        </xs:complexContent>
    </xs:complexType>



    <!-- Definizione tipo TrailerTruck (autotreno) -->

    <xs:group name="DrivingPart">
        <xs:choice>
            <xs:element name="Car" type="CarType"/>
            <xs:element name="Van" type="VanType"/>
            <xs:element name="Truck" type="TruckType"/>
        </xs:choice>
    </xs:group>

    <xs:complexType name="TrailerTruckType">
        <xs:sequence>
            <xs:group ref="DrivingPart"/>
            <xs:element name="Trailer" type="TrailerType" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>



    <!-- Definition: SemiTrailerTruck -->

    <xs:complexType name="SemiTrailerTruckType" >
        <xs:sequence>
            <xs:element name="RoadTractor" type="RoadTractorType" minOccurs="1" maxOccurs="1"/>
            <xs:element name="SemiTrailer" type="SemiTrailerType" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>



    <!-- Others... -->

    <xs:simpleType name="State">
        <xs:restriction base="xs:string">
            <xs:enumeration value="DISPONIBILE"/>
            <xs:enumeration value="NON_DISPONIBILE"/>
        </xs:restriction>
    </xs:simpleType>

</xs:schema>

Where a Shipper have a FLEET of VEHICLES.

Shipper1.xml :

<?xml version="1.0" encoding="UTF-8"?>
<Fleet shipperName="Shipper1"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="VehicleXMLSchema.xsd"> 

    <Car id="c1">
        <plate>AAA</plate>
        <mark>Peugeot</mark>
        <model>206</model>
        <trim></trim>
        <allestimento></allestimento>
        <stato>DISPONIBILE</stato>
        <carryingCapacity>0.0</carryingCapacity>
        <ptt>2</ptt>
        <weight>0.0</weight>
        <volume>0.0</volume>
        <length>0.0</length>
        <height>0.0</height>
        <width>0.0</width>
        <locazioneAttuale>Bari</locazioneAttuale>
    </Car>

    <Car>
        <plate>BBB</plate>
        <mark>Peugeot</mark>
        <model>206</model>
        <trim></trim>
        <allestimento></allestimento>
        <stato>DISPONIBILE</stato>
        <carryingCapacity>0.0</carryingCapacity>
        <ptt>2</ptt>
        <weight>0.0</weight>
        <volume>0.0</volume>
        <length>0.0</length>
        <height>0.0</height>
        <width>0.0</width>
        <locazioneAttuale>Bari</locazioneAttuale>
    </Car>

    <Van>
        <plate>CCC</plate>
        <mark>Volvo</mark>
        <model></model>
        <trim></trim>
        <allestimento>frigorifero</allestimento>
        <stato>DISPONIBILE</stato>
        <carryingCapacity>0.0</carryingCapacity>
        <ptt>3</ptt>
        <weight>0.0</weight>
        <volume>0.0</volume>
        <length>0.0</length>
        <height>0.0</height>
        <width>0.0</width>
        <locazioneAttuale>Barletta</locazioneAttuale>
    </Van>

    <Truck id="1">
        <plate>DDD</plate>
        <mark>Scania</mark>
        <model></model>
        <trim></trim>
        <allestimento>Frigo</allestimento>
        <stato>DISPONIBILE</stato>
        <carryingCapacity>0.0</carryingCapacity>
        <ptt>5</ptt>
        <weight>0.0</weight>
        <volume>0.0</volume>
        <length>0.0</length>
        <height>0.0</height>
        <width>0.0</width>
        <locazioneAttuale>Andria</locazioneAttuale>
    </Truck>

    <Trailer id="t1">
        <plate>EEE</plate>
        <mark>Scania</mark>
        <model></model>
        <trim></trim>
        <allestimento>Frigo</allestimento>
        <stato>NON_DISPONIBILE</stato>
        <carryingCapacity>0.0</carryingCapacity>
        <ptt>5</ptt>
        <weight>0.0</weight>
        <volume>0.0</volume>
        <length>0.0</length>
        <height>0.0</height>
        <width>0.0</width>
        <locazioneAttuale>Andria</locazioneAttuale>
    </Trailer>

    <RoadTractor>
        <plate>FFF</plate>
        <mark>Scania</mark>
        <model></model>
        <trim></trim>
        <allestimento>Frigo</allestimento>
        <stato>NON_DISPONIBILE</stato>
        <carryingCapacity>0.0</carryingCapacity>
        <ptt>5</ptt>
        <weight>0.0</weight>
        <volume>0.0</volume>
        <length>0.0</length>
        <height>0.0</height>
        <width>0.0</width>
        <locazioneAttuale>Andria</locazioneAttuale>
    </RoadTractor>

    <SemiTrailer>
        <plate>GGG</plate>
        <mark>Scania</mark>
        <model></model>
        <trim></trim>
        <allestimento>Frigo</allestimento>
        <stato>NON_DISPONIBILE</stato>
        <carryingCapacity>0.0</carryingCapacity>
        <ptt>5</ptt>
        <weight>0.0</weight>
        <volume>0.0</volume>
        <length>0.0</length>
        <height>0.0</height>
        <width>0.0</width>
        <locazioneAttuale>Andria</locazioneAttuale>
    </SemiTrailer>


    <TrailerTruck>
        <Car id="c1">
            <plate>AAA</plate>
            <mark>Peugeot</mark>
            <model>206</model>
            <trim></trim>
            <allestimento></allestimento>
            <stato>DISPONIBILE</stato>
            <carryingCapacity>0.0</carryingCapacity>
            <ptt>2</ptt>
            <weight>0.0</weight>
            <volume>0.0</volume>
            <length>0.0</length>
            <height>0.0</height>
            <width>0.0</width>
            <locazioneAttuale>Bari</locazioneAttuale>
        </Car>
        <Trailer>
            <plate>EEE</plate>
            <mark>Scania</mark>
            <model></model>
            <trim></trim>
            <allestimento>Frigo</allestimento>
            <stato>NON_DISPONIBILE</stato>
            <carryingCapacity>0.0</carryingCapacity>
            <ptt>5</ptt>
            <weight>0.0</weight>
            <volume>0.0</volume>
            <length>0.0</length>
            <height>0.0</height>
            <width>0.0</width>
            <locazioneAttuale>Andria</locazioneAttuale>
        </Trailer>
    </TrailerTruck>


    <SemiTrailerTruck>
        <RoadTractor>
            <plate>STT1</plate>
            <mark>Scania</mark>
            <model></model>
            <trim></trim>
            <allestimento>Frigo</allestimento>
            <stato>NON_DISPONIBILE</stato>
            <carryingCapacity>0.0</carryingCapacity>
            <ptt>5</ptt>
            <weight>0.0</weight>
            <volume>0.0</volume>
            <length>0.0</length>
            <height>0.0</height>
            <width>0.0</width>
            <locazioneAttuale>Andria</locazioneAttuale>
        </RoadTractor>
        <SemiTrailer>
            <plate>ST2</plate>
            <mark>Scania</mark>
            <model></model>
            <trim></trim>
            <allestimento>Frigo</allestimento>
            <stato>NON_DISPONIBILE</stato>
            <carryingCapacity>0.0</carryingCapacity>
            <ptt>5</ptt>
            <weight>0.0</weight>
            <volume>0.0</volume>
            <length>0.0</length>
            <height>0.0</height>
            <width>0.0</width>
            <locazioneAttuale>Andria</locazioneAttuale>
        </SemiTrailer>
    </SemiTrailerTruck>

</Fleet>

With this implementation I can manipulate in Java the classes Car, Van, Truck, Trailer, RoadTractor and SemiTrailer... but not the complex classes TrailerTruck and SemiTrailerTruck. I need a different XSD that include inheritance and interfaces. But I don't know how.

Gioce90
  • 554
  • 2
  • 10
  • 31
  • Did you try [using Google](http://www.liquid-technologies.com/Tutorials/XmlSchemas/XsdTutorial_03.aspx)? – Boris the Spider Mar 28 '15 at 19:36
  • @BoristheSpider I tried, but I found problems. Why reinvent the wheel? Anyway, I edited my question with a solution, but still no go. – Gioce90 Mar 30 '15 at 15:11
  • You seem to have a hardcoded `String type` rather than XML inheritance, as the link I gave you. You need to use `xs:extension` as described in the link. There is no idea of an "interface" in XML as it's a data descriptor rather than behaviour - but you should easily be able to define a type for each of your concrete types and let JAXB deal with it. – Boris the Spider Mar 30 '15 at 15:14
  • you look my edit... what would you change? – Gioce90 Mar 30 '15 at 15:23
  • That looks much better, what's the issue? – Boris the Spider Mar 30 '15 at 15:24
  • FIRST: The TrailerTruck and SemiTrailerTruck, in Java, are composite objects. So, we need existing objects (car, truck, bla bla ...). But in the XML file, I can not put a sort of reference? I mean a reference to elements already inserted in the XML file (not sure if I explained well). Because otherwise in some cases should, for example, redefine twice the same truck. – Gioce90 Mar 30 '15 at 15:32
  • SECOND: I'm trying to create a Reader that reads all the elements of Fleet (ie the different vehicles) and that - depending on the type - instantiate the correct class for the vehicle: `` is new `Car()`, `` is new `Truck()`, and so on. I'm working on right now, but if there is a smart way, I accept suggestions. – Gioce90 Mar 30 '15 at 15:39
  • 1) see references [here](http://stackoverflow.com/q/17300793/2071828), 2) is related to 1); if you choose to define your own classes and use Eclipse Moxy then it will automagically do this for you otherwise you will have to define some custom JAXB classes to carry out the resolution. But the answer to 2) is definitely **use JAXB**. – Boris the Spider Mar 30 '15 at 15:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74115/discussion-between-gioce90-and-boris-the-spider). – Gioce90 Mar 30 '15 at 15:45
  • ok, but I do not want to use any tool or other IDE (Moxy)... I want write MY xsd and xml files, and my java parser. I want to learn. – Gioce90 Mar 30 '15 at 16:56
  • You said above _Why reinvent the wheel?_, now you are saying that wheel reinvention is your intent? – Boris the Spider Mar 30 '15 at 16:57

1 Answers1

0

I found the answer to my questions.

This is my XML Schema. VehicleXmlSchema.xsd:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="Fleet" type="FleetType"/>

    <xs:complexType name="FleetType">
        <xs:choice maxOccurs="unbounded">
            <xs:element ref="Car"/>
            <xs:element ref="Van"/>
            <xs:element ref="Truck"/>
            <xs:element ref="Trailer"/>
            <xs:element ref="RoadTractor"/> 
            <xs:element ref="SemiTrailer"/>
            <xs:element ref="TrailerTruck"/>
            <xs:element ref="SemiTrailerTruck"/>
        </xs:choice>
        <xs:attribute name="shipperName" type="xs:string"/>
    </xs:complexType>


    <xs:complexType name="VehicleType" abstract="true">
        <xs:sequence minOccurs="0">
            <xs:element name="plate" type="xs:string" minOccurs="1" />
            <xs:element name="mark" type="xs:string" minOccurs="1" />
            <xs:element name="model" type="xs:string" minOccurs="1" />
            <xs:element name="trim" type="xs:string" minOccurs="0" />
            <xs:element name="allestimento" type="xs:string" minOccurs="0" />
            <xs:element name="stato" type="State" minOccurs="1"/>
            <xs:element name="carryingCapacity" type="xs:float" minOccurs="0" />
            <xs:element name="ptt" type="xs:float" minOccurs="0" />
            <xs:element name="weight" type="xs:float" minOccurs="0" />
            <xs:element name="volume" type="xs:float" minOccurs="0" />
            <xs:element name="length" type="xs:float" minOccurs="0" />
            <xs:element name="height" type="xs:float" minOccurs="0" />
            <xs:element name="width" type="xs:float" minOccurs="0" />
            <xs:element name="locazioneAttuale" type="xs:string" minOccurs="0" />
        </xs:sequence>
        <xs:attribute name="id" type="xs:ID"/>
        <xs:attribute name="refid" type="xs:IDREF"/>
    </xs:complexType>


    <!-- Car, Van, Truck, Trailer, RoadTractor, SemiTrailer -->

    <xs:element name="Car">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="VehicleType"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="Van">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="VehicleType"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="Truck">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="VehicleType"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="Trailer">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="VehicleType"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="RoadTractor">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="VehicleType"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="SemiTrailer">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="VehicleType"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>


    <!-- TrailerTruck -->

    <xs:group name="DrivingPart">
        <xs:choice>
            <xs:element ref="Car"/>
            <xs:element ref="Van"/>
            <xs:element ref="Truck"/>
        </xs:choice>
    </xs:group>

    <xs:element name="TrailerTruck">
        <xs:complexType>
            <xs:sequence>
                <xs:group ref="DrivingPart"/>
                <xs:element ref="Trailer" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>

    </xs:element>


    <!-- SemiTrailerTruck -->

    <xs:element name="SemiTrailerTruck">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="RoadTractor" minOccurs="1" maxOccurs="1"/>
                <xs:element ref="SemiTrailer" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>


    <!-- Altre definizioni -->

    <xs:simpleType name="State">
        <xs:restriction base="xs:string">
            <xs:enumeration value="DISPONIBILE"/>
            <xs:enumeration value="NON_DISPONIBILE"/>
        </xs:restriction>
    </xs:simpleType>

</xs:schema>

As you see, it was enough to change the complexType vehicleType:

  1. Adding to the internal sequence the attribute minOccurs="0"
  2. Adding attributes:
    • <xs:attribute name="id" type="xs:ID"/>
    • <xs:attribute name="refid" type="xs:IDREF"/>

And, in a XML file I can do this:

<Car id="car1">
    <plate>AAA</plate>
    <mark>Peugeot</mark>
    <model>206</model>
    <!-- bla bla -->
</Car>

<Truck id="truck1">
    <plate>DDD</plate>
    <mark>Scania</mark>
    <model></model>
    <!-- bla bla -->
</Truck>

<Trailer id="trailer1">
    <plate>EEE</plate>
    <mark>Scania</mark>
    <model></model>
    <!-- bla bla -->
</Trailer>

<TrailerTruck>
    <Car refid="car1"/>
    <Trailer refid="trailer1"/>
</TrailerTruck>
Gioce90
  • 554
  • 2
  • 10
  • 31