View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004-2007, Ontotext Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  
19  package org.wsmo.grounding.sawsdl.examples;
20  
21  
22  import java.io.*;
23  import java.net.URI;
24  import java.util.List;
25  
26  import javax.xml.namespace.QName;
27  
28  import org.wsmo.factory.WsmoFactory;
29  import org.wsmo.grounding.sawsdl.*;
30  import org.wsmo.grounding.sawsdl.io.*;
31  
32  /*
33   * An example program demonstrating the usage of the SAWSDL grounding API.
34   * The scenario is quite simple:
35   *    - opening a WSDL file (SAWSDLexample.wsdl) which is based on the latest SAWSDL specification
36   *    - constructing a grounding object (one for the whole file)
37   *    - listing the detected SAWSDL annotations
38   *    - adding various types of annotation to the grounding object
39   *    - listing the new state of the annotation
40   *    - printing the result in a new separate file (SAWSDLexampleNew.wsdl)
41   */
42  
43  public class SawsdlExample {
44  
45      private static final String INPUT_FILE = "WSDL-input.wsdl";
46      private static final String OUTPUT_FILE = "SAWSDL-output.wsdl";
47  
48      public static void main(String[] args) throws Exception {
49  
50          // Reading the SAWSDL annotation
51          Grounding grounding = readGrounding(INPUT_FILE);
52          
53          // Printing the current state of the annotation
54          System.out.println("* * * * * * * *  INITIAL GROUNDING STATE * * * * * * ");
55          dumpGrounding(grounding);
56          
57          // Constructing and adding new annotation objects:
58          WsmoFactory factory = org.wsmo.factory.Factory.createWsmoFactory(null);
59          String targetNamespace = "http://www.w3.org/2002/ws/sawsdl/spec/wsdl/order#";
60          
61          grounding.createElementModelRef(
62                  new QName(targetNamespace, "OrderRequest"), 
63                  factory.createIRI("http://www.example-one.org#elementRef"));
64  
65          grounding.createAttributeModelRef(
66                  new QName(targetNamespace, "quantity"), 
67                  factory.createIRI("http://www.example-one.org#attrRef"));
68  
69          grounding.createComplexTypeModelRef(
70                  new QName(targetNamespace, "item"), 
71                  factory.createIRI("http://www.example-one.org#complexTypeRef"));
72  
73          grounding.createSimpleTypeModelRef(
74                  new QName(targetNamespace, "confirmation"), 
75                  factory.createIRI("http://www.example-one.org#simpleTypeRef"));
76  
77          grounding.createOperationModelRef(
78                  new QName(targetNamespace, "order"), 
79                  factory.createIRI("http://www.example-one.org#operationRef"));
80  
81          grounding.createFaultModelRef(
82                  new QName(targetNamespace, "orderFault"), 
83                  factory.createIRI("http://www.example-one.org#faultRef"));
84  
85          grounding.createInterfaceCategory(
86                  new QName(targetNamespace, "Order"), 
87                  factory.createIRI("http://www.example-one.org#orderRef"));
88  
89          grounding.createLiftingSchemaMapping(
90                  new QName(targetNamespace, "OrderRequest"), 
91                  new URI("http://www.example-one.org/Request2RDFOnt.xml"));
92  
93          // printing the new state of the annotation
94          System.out.println("\n* * * * * * * *  FINAL GROUNDING STATE * * * * * * ");
95          dumpGrounding(grounding);
96  
97          // Saving the result to a new file
98          System.out.print("Saving to " + OUTPUT_FILE + " ...");
99          writeGrounding(OUTPUT_FILE, grounding);
100         System.out.println("Done");
101     }
102     
103     private static Grounding readGrounding(String inputWSDL) throws Exception {
104 
105         Reader in = new InputStreamReader(
106                 ClassLoader.getSystemResourceAsStream(inputWSDL));
107         
108         // Instanciate the default implementation of the Grounding parser
109         GroundingParser parser = Factory.createParser(null);
110         return parser.parse(in);
111     }
112     
113     private static void writeGrounding(String outputWSDL, Grounding grounding) throws Exception {
114 
115         Writer out = new OutputStreamWriter(
116                 new FileOutputStream(outputWSDL));
117         
118         // Instanciate the default implementation of the Grounding serializer
119         GroundingSerializer serializer = Factory.createSerializer(null);
120         
121         serializer.serialize(grounding, out);
122     }
123 
124     private static void dumpGrounding(Grounding grounding) throws Exception {
125         List<ModelRef> refs = grounding.listModelRefs();
126         System.out.println(refs.size() + " model reference(s) detected:");
127         for(ModelRef ref : refs) {
128             String type;
129             if (ref instanceof AttributeModelRef) {
130                 type = "Attribute";
131             }
132             else if (ref instanceof ElementModelRef) {
133                 type = "Element";
134             } 
135             else if (ref instanceof ComplexTypeModelRef) {
136                 type = "ComplexType";
137             } 
138             else if (ref instanceof SimpleTypeModelRef) {
139                 type = "SimpleType";
140             } 
141             else if (ref instanceof InterfaceCategory) {
142                 type = "Interface";
143             } 
144             else if (ref instanceof OperationModelRef) {
145                 type = "Operation";
146             } 
147             else if (ref instanceof FaultModelRef) {
148                 type = "Fault";
149             }
150             else {
151                 type = "unknown";
152             }
153             System.out.println("  " + type + " [" + ref.getSource().getLocalPart() + "  ->  " + ref.getTarget() + "]");
154         }
155         List<SchemaMapping> mappings = grounding.listSchemaMappings();
156         
157         System.out.println(mappings.size() + " schema mappings detected:");
158         for(SchemaMapping ref : mappings) {
159             String type;
160             if (ref instanceof LiftingSchemaMapping) {
161                 type = "  Lifting";
162             }
163             else {
164                 type = "  Lowering";
165             } 
166             System.out.println(type + " schema mapping [" + ref.getSource() + "  ->  " + ref.getSchema() + "]");
167         }
168 
169     }
170 
171 }