001: /**
002: *
003: * Copyright 2004 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.ws.scout.util;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.StringTokenizer;
023:
024: import javax.xml.registry.JAXRException;
025: import javax.xml.registry.infomodel.ClassificationScheme;
026: import javax.xml.registry.infomodel.Concept;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.ws.scout.registry.infomodel.ClassificationSchemeImpl;
031: import org.apache.ws.scout.registry.infomodel.ConceptImpl;
032: import org.apache.ws.scout.registry.infomodel.InternationalStringImpl;
033: import org.apache.ws.scout.registry.infomodel.KeyImpl;
034:
035: /**
036: * Helper class that deals with predefined enumerations
037: *
038: * @author Anil Saldhana <anil@apache.org>
039: * @author Kurt Stam <kstam@apache.org>
040: *
041: */
042: public class EnumerationHelper {
043: private static Log log = LogFactory.getLog(EnumerationHelper.class);
044:
045: private final static String OBJECT_TYPE = "ObjectType";
046: private final static String ASSOCIATION_TYPE = "AssociationType";
047: private final static String URL_TYPE = "URLType";
048: private final static String PHONE_TYPE = "PhoneType";
049: private final static String POSTAL_ADDRESS_ATTRIBUTES_STR = "PostalAddressAttributes";
050: private final static String[] TYPES = { OBJECT_TYPE,
051: ASSOCIATION_TYPE, URL_TYPE, PHONE_TYPE,
052: POSTAL_ADDRESS_ATTRIBUTES_STR };
053:
054: private final static String[] OBJECT_TYPES = { "ExternalLink",
055: "Package", "ExternalId", "Association", "Classification",
056: "Concept", "AuditableEvent", "User", "Organization", "CPA",
057: "CPP", "Service", "ServiceBinding", "Process", "WSDL",
058: "ExtrinsicObj", "Organization", "User" };
059: private final static String[] ASSOCIATION_TYPES = { "RelatedTo",
060: "ExternallyLinks", "Contains", "Extends", "Implements",
061: "InstanceOf", "Supersedes", "Uses", "HasMember",
062: "EquivalentTo", "HasChild", "HasParent", "Replaces",
063: "ResponsibleFor", "SubmitterOf" };
064: private final static String[] URL_TYPES = { "HTTP", "HTTPS",
065: "SMTP", "FAX", "PHONE", "OTHER" };
066: private final static String[] PHONE_TYPES = { "Office", "Home",
067: "Mobile", "Beeper", "FAX" };
068: private final static String[] POSTAL_ADDRESS_ATTRIBUTES = {
069: "StreetNumber", "Street", "City", "State", "PostalCode",
070: "Country" };
071:
072: private final static ArrayList<String> TYPES_LIST = new ArrayList<String>(
073: Arrays.asList(TYPES));
074: private final static ArrayList<String> OBJECT_TYPES_LIST = new ArrayList<String>(
075: Arrays.asList(OBJECT_TYPES));
076: private final static ArrayList<String> ASSOCIATION_TYPES_LIST = new ArrayList<String>(
077: Arrays.asList(ASSOCIATION_TYPES));
078: private final static ArrayList<String> URL_TYPES_LIST = new ArrayList<String>(
079: Arrays.asList(URL_TYPES));
080: private final static ArrayList<String> PHONE_TYPES_LIST = new ArrayList<String>(
081: Arrays.asList(PHONE_TYPES));
082: private final static ArrayList<String> POSTAL_ADDRESS_ATTRIBUTES_LIST = new ArrayList<String>(
083: Arrays.asList(POSTAL_ADDRESS_ATTRIBUTES));
084:
085: private static Map<String, ArrayList<String>> typesMap = new HashMap<String, ArrayList<String>>();
086: static {
087: typesMap.put(OBJECT_TYPE, OBJECT_TYPES_LIST);
088: typesMap.put(ASSOCIATION_TYPE, ASSOCIATION_TYPES_LIST);
089: typesMap.put(URL_TYPE, URL_TYPES_LIST);
090: typesMap.put(PHONE_TYPE, PHONE_TYPES_LIST);
091: typesMap.put(POSTAL_ADDRESS_ATTRIBUTES_STR,
092: POSTAL_ADDRESS_ATTRIBUTES_LIST);
093: }
094:
095: public static Concept getConceptByPath(String path)
096: throws IllegalArgumentException, JAXRException {
097: //Lets tokenize the path
098: StringTokenizer tokenizer = new StringTokenizer(path, "/");
099: String firstToken = null;
100: String secondToken = null;
101:
102: if (tokenizer.hasMoreTokens()) {
103: firstToken = tokenizer.nextToken();
104: if (tokenizer.hasMoreTokens()) {
105: secondToken = tokenizer.nextToken();
106: if (tokenizer.hasMoreTokens()) {
107: log.warn("Looking for 2 tokens. "
108: + tokenizer.nextToken()
109: + " will be ignored");
110: }
111: } else {
112: throw new IllegalArgumentException(
113: "Expected two token separated with a forward slash (/)");
114: }
115: } else {
116: throw new IllegalArgumentException(
117: "Expected two token separated with a forward slash (/)");
118: }
119: return createConcept(firstToken, secondToken);
120: }
121:
122: /**
123: *
124: * @param firstToken
125: * @param secondToken
126: * @return Concept
127: * @throws JAXRException
128: */
129: private static Concept createConcept(String firstToken,
130: String secondToken) throws JAXRException,
131: IllegalArgumentException {
132: if (!TYPES_LIST.contains(firstToken))
133: throw new IllegalArgumentException("Exspected the path to "
134: + "start with one of " + TYPES);
135:
136: //get the predefined classificationscheme
137: ClassificationScheme cs = new ClassificationSchemeImpl(null);
138: cs.setName(new InternationalStringImpl(firstToken));
139: cs.setKey(new KeyImpl(firstToken));
140:
141: ArrayList<String> conceptStrings = typesMap.get(firstToken);
142: if (!conceptStrings.contains(secondToken))
143: throw new IllegalArgumentException("Exspected the path to "
144: + "end with one of " + conceptStrings.toArray());
145:
146: Concept concept = new ConceptImpl(null);
147: concept.setName(new InternationalStringImpl(secondToken
148: .toLowerCase()));
149: concept.setValue(secondToken);
150: concept.setKey(new KeyImpl(firstToken + "/" + secondToken));
151: ((ConceptImpl) concept)
152: .setScheme(((ClassificationSchemeImpl) cs));
153: return concept;
154: }
155: }
|