This project has retired. For details please refer to its Attic page.
Coverage Report
Coverage Report - org.apache.ws.scout.util.EnumerationHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
EnumerationHelper
87%
41/47
50%
5/10
5.5
 
 1  
 /**
 2  
  *
 3  
  * Copyright 2004 The Apache Software Foundation
 4  
  *
 5  
  *  Licensed under the Apache License, Version 2.0 (the "License");
 6  
  *  you may not use this file except in compliance with the License.
 7  
  *  You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  *  Unless required by applicable law or agreed to in writing, software
 12  
  *  distributed under the License is distributed on an "AS IS" BASIS,
 13  
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  *  See the License for the specific language governing permissions and
 15  
  *  limitations under the License.
 16  
  */
 17  
 package org.apache.ws.scout.util;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Arrays;
 21  
 import java.util.HashMap;
 22  
 import java.util.Map;
 23  
 import java.util.StringTokenizer;
 24  
 
 25  
 import javax.xml.registry.JAXRException;
 26  
 import javax.xml.registry.infomodel.ClassificationScheme;
 27  
 import javax.xml.registry.infomodel.Concept;
 28  
 
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 import org.apache.ws.scout.registry.infomodel.ClassificationSchemeImpl;
 32  
 import org.apache.ws.scout.registry.infomodel.ConceptImpl;
 33  
 import org.apache.ws.scout.registry.infomodel.InternationalStringImpl;
 34  
 import org.apache.ws.scout.registry.infomodel.KeyImpl;
 35  
 
 36  
 /**
 37  
  * Helper class that deals with predefined enumerations
 38  
  *
 39  
  * @author Anil Saldhana  <anil@apache.org>
 40  
  * @author Kurt Stam <kstam@apache.org>
 41  
  * 
 42  
  */
 43  0
 public class EnumerationHelper
 44  
 {
 45  2
     private static Log log = LogFactory.getLog(EnumerationHelper.class);
 46  
    
 47  
 
 48  
     private final static String UDDI_ORG_TYPES                      = "uuid:C1ACF26D-9672-4404-9D70-39B756E62AB4"; 
 49  
     private final static String OBJECT_TYPE                   = "ObjectType";
 50  
     private final static String ASSOCIATION_TYPE              = "AssociationType";
 51  
     private final static String URL_TYPE                      = "URLType";
 52  
     private final static String PHONE_TYPE                    = "PhoneType";
 53  
     private final static String POSTAL_ADDRESS_ATTRIBUTES_STR = "PostalAddressAttributes";
 54  2
     private final static String[] TYPES = {OBJECT_TYPE, ASSOCIATION_TYPE, URL_TYPE, PHONE_TYPE, POSTAL_ADDRESS_ATTRIBUTES_STR};
 55  
     
 56  2
     private final static String[] OBJECT_TYPES = {
 57  
          "ExternalLink","Package","ExternalId","Association","Classification","Concept",
 58  
          "AuditableEvent","User","Organization","CPA","CPP","Service","ServiceBinding","Process","WSDL",
 59  
          "ExtrinsicObj","Organization","User"};
 60  2
     private final static String[] ASSOCIATION_TYPES = {
 61  
          "RelatedTo","ExternallyLinks","Contains","Extends","Implements",
 62  
          "InstanceOf","Supersedes","Uses","HasMember","EquivalentTo","HasChild","HasParent","Replaces",
 63  
          "ResponsibleFor","SubmitterOf"};
 64  2
     private final static String[] URL_TYPES = {
 65  
          "HTTP","HTTPS","SMTP","FAX","PHONE","OTHER"};
 66  2
     private final static String[] PHONE_TYPES = {
 67  
          "Office","Home","Mobile","Beeper","FAX"};
 68  2
     private final static String[] POSTAL_ADDRESS_ATTRIBUTES = {
 69  
          "StreetNumber","Street","City","State","PostalCode","Country"};
 70  
     
 71  2
     private final static ArrayList<String> TYPES_LIST                     = new ArrayList<String>(Arrays.asList(TYPES));
 72  2
     private final static ArrayList<String> OBJECT_TYPES_LIST              = new ArrayList<String>(Arrays.asList(OBJECT_TYPES));
 73  2
     private final static ArrayList<String> ASSOCIATION_TYPES_LIST         = new ArrayList<String>(Arrays.asList(ASSOCIATION_TYPES));
 74  2
     private final static ArrayList<String> URL_TYPES_LIST                 = new ArrayList<String>(Arrays.asList(URL_TYPES));
 75  2
     private final static ArrayList<String> PHONE_TYPES_LIST               = new ArrayList<String>(Arrays.asList(PHONE_TYPES));
 76  2
     private final static ArrayList<String> POSTAL_ADDRESS_ATTRIBUTES_LIST = new ArrayList<String>(Arrays.asList(POSTAL_ADDRESS_ATTRIBUTES));
 77  
 
 78  2
     private static Map<String,ArrayList<String>> typesMap = new HashMap<String,ArrayList<String>>();
 79  
     static {
 80  2
         typesMap.put(OBJECT_TYPE                  ,OBJECT_TYPES_LIST);
 81  2
         typesMap.put(ASSOCIATION_TYPE             ,ASSOCIATION_TYPES_LIST);
 82  2
         typesMap.put(URL_TYPE                     , URL_TYPES_LIST);
 83  2
         typesMap.put(PHONE_TYPE                   , PHONE_TYPES_LIST);
 84  2
         typesMap.put(POSTAL_ADDRESS_ATTRIBUTES_STR, POSTAL_ADDRESS_ATTRIBUTES_LIST);
 85  2
     }
 86  
     
 87  
     public static Concept getConceptByPath( String path)
 88  
     throws IllegalArgumentException, JAXRException
 89  
     {
 90  
         //Lets tokenize the path
 91  8
         StringTokenizer tokenizer = new StringTokenizer(path,"/");
 92  8
         String firstToken = null;
 93  8
         String secondToken = null;
 94  
         
 95  8
         if(tokenizer.hasMoreTokens())
 96  
         {
 97  8
            firstToken = tokenizer.nextToken();
 98  8
            if (tokenizer.hasMoreTokens()) {
 99  8
                secondToken = tokenizer.nextToken();
 100  8
                if (tokenizer.hasMoreTokens()) {
 101  0
                    log.warn("Looking for 2 tokens. " + tokenizer.nextToken() + " will be ignored");
 102  
                }
 103  
            } else {
 104  0
                throw new IllegalArgumentException("Expected two token separated with a forward slash (/)");
 105  
            }
 106  
         } else {
 107  0
             throw new IllegalArgumentException("Expected two token separated with a forward slash (/)");
 108  
         }
 109  8
         return createConcept(firstToken, secondToken) ;
 110  
     }
 111  
 
 112  
     /**
 113  
      * 
 114  
      * @param firstToken
 115  
      * @param secondToken
 116  
      * @return Concept
 117  
      * @throws JAXRException
 118  
      */
 119  
     private static  Concept createConcept(String firstToken, String secondToken)
 120  
             throws JAXRException, IllegalArgumentException
 121  
     {
 122  8
         if (!TYPES_LIST.contains(firstToken)) throw new IllegalArgumentException("Expected the path to " +
 123  0
                 "start with one of " + Arrays.toString(TYPES));
 124  
         
 125  
         //get the predefined classificationscheme
 126  8
         ClassificationScheme cs = new ClassificationSchemeImpl(null);
 127  8
         cs.setName(new InternationalStringImpl(firstToken));
 128  8
         cs.setKey(new KeyImpl(firstToken));
 129  
 
 130  8
         ArrayList<String> conceptStrings = typesMap.get(firstToken);
 131  8
         if (!conceptStrings.contains(secondToken)) throw new IllegalArgumentException("Expected the path to " +
 132  0
                 "end with one of " + Arrays.toString(conceptStrings.toArray()));
 133  
                 
 134  8
         Concept concept = new ConceptImpl(null);
 135  8
         concept.setName(new InternationalStringImpl(secondToken.toLowerCase()));
 136  8
         concept.setValue(secondToken);
 137  8
         concept.setKey(new KeyImpl(UDDI_ORG_TYPES));
 138  8
         ((ConceptImpl)concept).setScheme(((ClassificationSchemeImpl)cs));
 139  8
         return concept;
 140  
     }
 141  
 }