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:
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.