Coverage Report - org.wsmo.grounding.sawsdl.examples.SawsdlExample
 
Classes in this File Line Coverage Branch Coverage Complexity
SawsdlExample
0%
0/56
0%
0/20
0
 
 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  0
 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  0
         Grounding grounding = readGrounding(INPUT_FILE);
 52  
         
 53  
         // Printing the current state of the annotation
 54  0
         System.out.println("* * * * * * * *  INITIAL GROUNDING STATE * * * * * * ");
 55  0
         dumpGrounding(grounding);
 56  
         
 57  
         // Constructing and adding new annotation objects:
 58  0
         WsmoFactory factory = org.wsmo.factory.Factory.createWsmoFactory(null);
 59  0
         String targetNamespace = "http://www.w3.org/2002/ws/sawsdl/spec/wsdl/order#";
 60  
         
 61  0
         grounding.createElementModelRef(
 62  
                 new QName(targetNamespace, "OrderRequest"), 
 63  
                 factory.createIRI("http://www.example-one.org#elementRef"));
 64  
 
 65  0
         grounding.createAttributeModelRef(
 66  
                 new QName(targetNamespace, "quantity"), 
 67  
                 factory.createIRI("http://www.example-one.org#attrRef"));
 68  
 
 69  0
         grounding.createComplexTypeModelRef(
 70  
                 new QName(targetNamespace, "item"), 
 71  
                 factory.createIRI("http://www.example-one.org#complexTypeRef"));
 72  
 
 73  0
         grounding.createSimpleTypeModelRef(
 74  
                 new QName(targetNamespace, "confirmation"), 
 75  
                 factory.createIRI("http://www.example-one.org#simpleTypeRef"));
 76  
 
 77  0
         grounding.createOperationModelRef(
 78  
                 new QName(targetNamespace, "order"), 
 79  
                 factory.createIRI("http://www.example-one.org#operationRef"));
 80  
 
 81  0
         grounding.createFaultModelRef(
 82  
                 new QName(targetNamespace, "orderFault"), 
 83  
                 factory.createIRI("http://www.example-one.org#faultRef"));
 84  
 
 85  0
         grounding.createInterfaceCategory(
 86  
                 new QName(targetNamespace, "Order"), 
 87  
                 factory.createIRI("http://www.example-one.org#orderRef"));
 88  
 
 89  0
         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  0
         System.out.println("\n* * * * * * * *  FINAL GROUNDING STATE * * * * * * ");
 95  0
         dumpGrounding(grounding);
 96  
 
 97  
         // Saving the result to a new file
 98  0
         System.out.print("Saving to " + OUTPUT_FILE + " ...");
 99  0
         writeGrounding(OUTPUT_FILE, grounding);
 100  0
         System.out.println("Done");
 101  0
     }
 102  
     
 103  
     private static Grounding readGrounding(String inputWSDL) throws Exception {
 104  
 
 105  0
         Reader in = new InputStreamReader(
 106  
                 ClassLoader.getSystemResourceAsStream(inputWSDL));
 107  
         
 108  
         // Instanciate the default implementation of the Grounding parser
 109  0
         GroundingParser parser = Factory.createParser(null);
 110  0
         return parser.parse(in);
 111  
     }
 112  
     
 113  
     private static void writeGrounding(String outputWSDL, Grounding grounding) throws Exception {
 114  
 
 115  0
         Writer out = new OutputStreamWriter(
 116  
                 new FileOutputStream(outputWSDL));
 117  
         
 118  
         // Instanciate the default implementation of the Grounding serializer
 119  0
         GroundingSerializer serializer = Factory.createSerializer(null);
 120  
         
 121  0
         serializer.serialize(grounding, out);
 122  0
     }
 123  
 
 124  
     private static void dumpGrounding(Grounding grounding) throws Exception {
 125  0
         List<ModelRef> refs = grounding.listModelRefs();
 126  0
         System.out.println(refs.size() + " model reference(s) detected:");
 127  0
         for(ModelRef ref : refs) {
 128  
             String type;
 129  0
             if (ref instanceof AttributeModelRef) {
 130  0
                 type = "Attribute";
 131  
             }
 132  0
             else if (ref instanceof ElementModelRef) {
 133  0
                 type = "Element";
 134  
             } 
 135  0
             else if (ref instanceof ComplexTypeModelRef) {
 136  0
                 type = "ComplexType";
 137  
             } 
 138  0
             else if (ref instanceof SimpleTypeModelRef) {
 139  0
                 type = "SimpleType";
 140  
             } 
 141  0
             else if (ref instanceof InterfaceCategory) {
 142  0
                 type = "Interface";
 143  
             } 
 144  0
             else if (ref instanceof OperationModelRef) {
 145  0
                 type = "Operation";
 146  
             } 
 147  0
             else if (ref instanceof FaultModelRef) {
 148  0
                 type = "Fault";
 149  
             }
 150  
             else {
 151  0
                 type = "unknown";
 152  
             }
 153  0
             System.out.println("  " + type + " [" + ref.getSource().getLocalPart() + "  ->  " + ref.getTarget() + "]");
 154  0
         }
 155  0
         List<SchemaMapping> mappings = grounding.listSchemaMappings();
 156  
         
 157  0
         System.out.println(mappings.size() + " schema mappings detected:");
 158  0
         for(SchemaMapping ref : mappings) {
 159  
             String type;
 160  0
             if (ref instanceof LiftingSchemaMapping) {
 161  0
                 type = "  Lifting";
 162  
             }
 163  
             else {
 164  0
                 type = "  Lowering";
 165  
             } 
 166  0
             System.out.println(type + " schema mapping [" + ref.getSource() + "  ->  " + ref.getSchema() + "]");
 167  0
         }
 168  
 
 169  0
     }
 170  
 
 171  
 }