View Javadoc

1   package com.ontotext.ordi.wsmo4rdf.remote;
2   
3   import org.omwg.ontology.Ontology;
4   import org.wsmo.common.IRI;
5   import org.wsmo.common.TopEntity;
6   import org.wsmo.common.exception.InvalidModelException;
7   import org.wsmo.factory.Factory;
8   import org.wsmo.factory.WsmoFactory;
9   import org.wsmo.mediator.Mediator;
10  import org.wsmo.service.Goal;
11  import org.wsmo.service.WebService;
12  import org.wsmo.wsml.Parser;
13  import org.wsmo.wsml.ParserException;
14  import org.wsmo.wsml.Serializer;
15  
16  import com.ontotext.ordi.wsmo4rdf.remote.exception.UnmarshalException;
17  
18  public class Marshaller {
19      private final static Serializer serializer = Factory.createSerializer(null);
20      private final static Parser parser = Factory.createParser(null);
21      private final static WsmoFactory wsmoFactory = Factory
22  	    .createWsmoFactory(null);
23  
24      public static String marshal(TopEntity topEntity) {
25  	if (topEntity == null) {
26  	    return null;
27  	}
28  	StringBuffer outsb = new StringBuffer();
29  	serializer.serialize(new TopEntity[] { topEntity }, outsb);
30  	return outsb.toString();
31      }
32  
33      public static Goal unmarshalGoal(String goal)
34  	    throws UnmarshalException {
35  	TopEntity te = parseTopEntityFromString(goal);
36  	if (!(te instanceof Goal)) {
37  	    throw new UnmarshalException();
38  	}
39  	return (Goal) te;
40      }
41  
42      private static TopEntity parseTopEntityFromString(String topEntity)
43  	    throws UnmarshalException {
44  	TopEntity[] topent;
45  	try {
46  	    topent = parser.parse(new StringBuffer(topEntity));
47  	} catch (ParserException e) {
48  	    throw new UnmarshalException(e);
49  	} catch (InvalidModelException e) {
50  	    throw new UnmarshalException(e);
51  	}
52  	if (topent == null || topent.length < 1) {
53  	    throw new IllegalArgumentException();
54  	}
55  	return topent[0];
56      }
57  
58      public static Ontology unmarshalOntology(String ont)
59  	    throws UnmarshalException {
60  	TopEntity topent = parseTopEntityFromString(ont);
61  
62  	if (!(topent instanceof Ontology)) {
63  	    throw new UnmarshalException();
64  	}
65  	Ontology onto = (Ontology) topent;
66  	return onto;
67      }
68  
69      public static WebService unmarshalWebService(String webServiceAsString)
70  	    throws UnmarshalException {
71  	TopEntity te = parseTopEntityFromString(webServiceAsString);
72  	if (!(te instanceof WebService)) {
73  	    throw new UnmarshalException();
74  	}
75  	return (WebService) te;
76      }
77  
78      public static Mediator unmarshalMediatior(String mediatorAsString)
79  	    throws UnmarshalException {
80  	TopEntity te = parseTopEntityFromString(mediatorAsString);
81  	if (!(te instanceof Mediator)) {
82  	    throw new UnmarshalException();
83  	}
84  	return (Mediator) te;
85      }
86  
87      public static IRI newIRI(String id) {
88  	return wsmoFactory.createIRI(id);
89      }
90  
91  }