View Javadoc
1 /* 2 * $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/util/ISO3166Importer.java,v 1.11 2002/02/25 11:49:51 skchonghk Exp $ 3 */ 4 5 package com.sun.ebxml.registry.util; 6 7 import java.io.*; 8 import java.util.*; 9 import org.oasis.ebxml.registry.bindings.rim.*; 10 import org.oasis.ebxml.registry.bindings.rim.types.*; 11 import org.oasis.ebxml.registry.bindings.query.*; 12 import org.oasis.ebxml.registry.bindings.rs.*; 13 14 /*** 15 It is the importer for ISO3166 taxonomy. The MiddleEast node/tree will be a 16 sub-node/tree of Asia node. The Australia node will be moved from Other node 17 to Australia node next to New Zealand. The remaining nodes remain the same 18 relationship as the original input ISO3166 taxonomy. 19 @author Adrian Chong 20 */ 21 22 public class ISO3166Importer 23 { 24 private StreamTokenizer tokenizer; 25 private int maxNumOfCountries; 26 private UUIDFactory uUIDFactory; 27 28 /*** 29 Constructor. 30 @param fileName The file path and name of the ISO3166 taxonomy file. 31 @param uUIDFactory The UUIDFactory 32 */ 33 public ISO3166Importer(String fileName, UUIDFactory uUIDFactory) throws 34 FileNotFoundException 35 { 36 tokenizer = new StreamTokenizer(new FileReader(fileName)); 37 tokenizer.eolIsSignificant(false); 38 tokenizer.slashSlashComments(true); 39 tokenizer.slashStarComments(true); 40 tokenizer.whitespaceChars(',', ','); 41 this.uUIDFactory = uUIDFactory; 42 } 43 44 /*** 45 Set the file path of the ISO3166 taxonomy file. 46 @param fileName The file path of the ISO3166 taxonomy file 47 */ 48 public void setFile(String fileName) throws FileNotFoundException 49 { 50 tokenizer = new StreamTokenizer(new FileReader(fileName)); 51 } 52 53 /*** 54 Set the maximum number of entries should be handled. Setting it to 0 means 55 that the importer will handle unlimited number of entries. 56 @param maxNum The maximum number of entries the importer will handle 57 */ 58 public void setMaxNumOfCountries(int maxNum) 59 { 60 maxNumOfCountries = maxNum; 61 } 62 63 /*** 64 Get the ClassificationScheme for ISO3166 taxonomy 65 @return the ClassificationScheme 66 */ 67 public ClassificationScheme getClassificationScheme() 68 throws IOException 69 { 70 int count = 0; 71 72 ClassificationScheme classScheme = new ClassificationScheme(); 73 classScheme.setId("urn:uuid:" + uUIDFactory.newUUID().toString()); 74 classScheme.setIsInternal(true); 75 classScheme.setNodeType(NodeTypeType.UNIQUECODE); 76 classScheme.setDescription(getInternationalDesc 77 ("This is the classification scheme for ISO3166")); 78 classScheme.setName(getInternationalName("ISO 3166")); 79 80 81 HashMap continentToNodeMap = new HashMap(); 82 83 while(true) 84 { 85 if (maxNumOfCountries != 0 && count > maxNumOfCountries 86 || tokenizer.nextToken()==StreamTokenizer.TT_EOF) 87 { 88 break; 89 } 90 91 //System.out.println(tokenizer.sval); 92 String code = tokenizer.sval; 93 tokenizer.nextToken(); 94 95 //System.out.println(tokenizer.sval); 96 String name = tokenizer.sval; 97 tokenizer.nextToken(); 98 99 //System.out.println(tokenizer.sval); 100 String continent = tokenizer.sval; 101 102 ClassificationNode parentClassificationNode = null; 103 104 if ((parentClassificationNode 105 = (ClassificationNode)continentToNodeMap.get(continent)) == null) 106 { 107 // Create root concept for a continent 108 String continentId = uUIDFactory.newUUID().toString(); 109 // id attribute 110 parentClassificationNode = new ClassificationNode(); 111 parentClassificationNode.setId("urn:uuid:" + continentId); 112 /* 113 Add the continent concept to the Classification Scheme, 114 and set parent attribite 115 */ 116 classScheme.addClassificationNode(parentClassificationNode); 117 // code attribute 118 parentClassificationNode.setCode(continent); 119 // name 120 parentClassificationNode.setName(getInternationalName(continent) 121 ); 122 continentToNodeMap.put(continent, parentClassificationNode); 123 } 124 125 //Generate new ClassificationNode 126 String countryId = uUIDFactory.newUUID().toString(); 127 // id attribute 128 ClassificationNode node = new ClassificationNode(); 129 node.setId("urn:uuid:" + countryId); 130 /* 131 Add the concept to the continent parent and set the parent attribute 132 */ 133 parentClassificationNode.addClassificationNode(node); 134 // code attribute 135 node.setCode(code); 136 // name attribute 137 node.setName(getInternationalName(name)); 138 count++; 139 } 140 return fixNodes(classScheme); 141 } 142 143 144 /*** 145 Make the MiddleEast node/tree be a sub-node/tree of Asia node. 146 Move Australia from Other node to Australia node next to New Zealand. 147 */ 148 private ClassificationScheme fixNodes(ClassificationScheme classScheme) { 149 150 ClassificationNode [] nodes = classScheme.getClassificationNode(); 151 ClassificationNode asiaNode = null; 152 ClassificationNode middleEastNode = null; 153 ClassificationNode otherNode = null; 154 ClassificationNode australiaNode = null; 155 156 for (int i=0; i < nodes.length; i++) { 157 if (nodes[i].getCode().equalsIgnoreCase("Asia")) { 158 asiaNode = nodes[i]; 159 } 160 else if (nodes[i].getCode().equalsIgnoreCase("Other")) { 161 otherNode = nodes[i]; 162 } 163 else if (nodes[i].getCode().equalsIgnoreCase("Middle East")) { 164 middleEastNode = nodes[i]; 165 } 166 else if (nodes[i].getCode().equalsIgnoreCase("Australia")) { 167 australiaNode = nodes[i]; 168 } 169 } 170 171 ClassificationNode australiaInOtherNode = null; 172 ClassificationNode [] otherNodeChildren 173 = otherNode.getClassificationNode(); 174 for (int i=0; i < otherNodeChildren.length; i++) { 175 if (otherNodeChildren[i].getCode().equalsIgnoreCase("AU")) { 176 australiaInOtherNode = otherNodeChildren[i]; 177 } 178 } 179 180 181 if (middleEastNode != null && asiaNode != null) { 182 classScheme.removeClassificationNode(middleEastNode); 183 asiaNode.addClassificationNode(middleEastNode); 184 } 185 186 if (otherNode != null && australiaNode != null && australiaInOtherNode 187 != null) { 188 otherNode.removeClassificationNode(australiaInOtherNode); 189 australiaNode.addClassificationNode(australiaInOtherNode); 190 } 191 return classScheme; 192 } 193 194 private Name getInternationalName(String name) 195 { 196 197 Name internaionalName = new Name(); 198 internaionalName.addInternationalStringTypeItem( 199 getInternationalStringTypeItem(name)); 200 return internaionalName; 201 } 202 203 private Description getInternationalDesc(String desc) 204 { 205 206 Description internaionalDesc = new Description(); 207 internaionalDesc.addInternationalStringTypeItem( 208 getInternationalStringTypeItem(desc)); 209 return internaionalDesc; 210 } 211 212 private InternationalStringTypeItem getInternationalStringTypeItem(String 213 str) 214 { 215 LocalizedString localizedStr = new LocalizedString(); 216 localizedStr.setLang(null); 217 localizedStr.setValue(str); 218 InternationalStringTypeItem inStrTypeItem = new 219 InternationalStringTypeItem(); 220 inStrTypeItem.setLocalizedString(localizedStr); 221 return inStrTypeItem; 222 } 223 224 public static void main(String [] args) throws Exception { 225 if (args.length < 2) { 226 System.out.println("usage: [input file path] [output file path]"); 227 return; 228 } 229 230 UUIDFactory uUIDFactory = UUIDFactory.getInstance(); 231 ISO3166Importer importer = new ISO3166Importer(args[0] 232 , uUIDFactory); 233 ClassificationScheme scheme = importer.getClassificationScheme(); 234 FileWriter outputWriter = new FileWriter(args[1]); 235 scheme.marshal(outputWriter); 236 } 237 } 238 239

This page was automatically generated by Maven