View Javadoc
1 /* 2 $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/util/UNSPSCImporter.java,v 1.2 2001/12/21 08:54:36 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 UNSPSC. It will automatically skip the first line in the 16 input file. 17 18 @author Adrian Chong 19 */ 20 public class UNSPSCImporter 21 { 22 private StreamTokenizer tokenizer; 23 private int maxNumOfEntries; 24 private UUIDFactory uUIDFactory; 25 private static final int SEGMENT = 1; 26 private static final int FAMILY = 2; 27 private static final int CLASS = 3; 28 private static final int COMMODITY = 4; 29 private static final int BUSINESS_FUNCTION = 5; 30 31 /*** 32 Constructor. 33 @param fileName The file path and name of the UNSPSC taxonomy file. 34 @param uUIDFactory The UUIDFactory 35 */ 36 public UNSPSCImporter(String fileName, UUIDFactory uUIDFactory) throws 37 IOException 38 { 39 setFile(fileName); 40 tokenizer.eolIsSignificant(false); 41 tokenizer.slashSlashComments(true); 42 tokenizer.slashStarComments(true); 43 tokenizer.wordChars(' ', '/'); 44 tokenizer.whitespaceChars(',', ','); 45 tokenizer.parseNumbers(); 46 this.uUIDFactory = uUIDFactory; 47 } 48 49 /*** 50 Set the file path of the UNSPSC taxonomy file. 51 @param fileName The file path of the UNSPSC taxonomy file 52 */ 53 public void setFile(String fileName) throws IOException 54 { 55 tokenizer = new StreamTokenizer(new FileReader(fileName)); 56 tokenizer.eolIsSignificant(true); 57 while (tokenizer.nextToken()!=StreamTokenizer.TT_EOF && tokenizer 58 .lineno() < 2) 59 {} 60 tokenizer.eolIsSignificant(false); 61 } 62 63 /*** 64 Set the maximum number of entries should be handled. Setting it to 0 means 65 that the importer will handle unlimited number of entries. 66 @param maxNum The maximum number of entries the importer will handle 67 */ 68 public void setMaxNumOfEntries(int maxNum) 69 { 70 maxNumOfEntries = maxNum; 71 } 72 73 /*** 74 Get the ClassificationScheme for NAICS taxonomy 75 @return the ClassificationScheme 76 */ 77 public ClassificationScheme getClassificationScheme() throws IOException 78 { 79 // Create the classification scheme 80 ClassificationScheme classScheme = new ClassificationScheme(); 81 classScheme.setId("urn:uuid:" + uUIDFactory.newUUID().toString()); 82 classScheme.setIsInternal(true); 83 classScheme.setNodeType(NodeTypeType.UNIQUECODE); 84 classScheme.setDescription(getInternationalDesc 85 ("This is the classification scheme for UNSPSC")); 86 classScheme.setName(getInternationalName("UNSPSC")); 87 88 89 ClassificationNode parentSegment = null; 90 ClassificationNode parentFamily = null; 91 ClassificationNode parentClass = null; 92 ClassificationNode parentCommodity = null; 93 ClassificationNode businessFunction = null; 94 95 int count = 0; 96 while(true) 97 { 98 if (maxNumOfEntries != 0 && count > maxNumOfEntries 99 || tokenizer.nextToken()==StreamTokenizer.TT_EOF) 100 { 101 break; 102 } 103 104 int code = 0; 105 String name = null; 106 code = (int)tokenizer.nval; 107 //System.out.println("a number:" + (int)tokenizer.nval); 108 tokenizer.nextToken(); 109 name = tokenizer.sval; 110 //System.out.println(tokenizer.sval); 111 112 // Create the classification node by the type 113 String id = uUIDFactory.newUUID().toString(); 114 115 116 if (getType(code)==SEGMENT) 117 { 118 parentSegment = new ClassificationNode(); 119 parentSegment.setId("urn:uuid:" + id); 120 classScheme.addClassificationNode(parentSegment); 121 // code attribute 122 parentSegment.setCode(code + ""); 123 // name 124 parentSegment.setName(getInternationalName(name)); 125 } 126 else if (getType(code)==FAMILY) 127 { 128 parentFamily = new ClassificationNode(); 129 parentFamily.setId("urn:uuid:" + id); 130 parentSegment.addClassificationNode(parentFamily); 131 // code attribute 132 parentFamily.setCode(code + ""); 133 // name 134 parentFamily.setName(getInternationalName(name)); 135 } 136 else if (getType(code)==CLASS) 137 { 138 parentClass = new ClassificationNode(); 139 parentClass.setId("urn:uuid:" + id); 140 parentFamily.addClassificationNode(parentClass); 141 // code attribute 142 parentClass.setCode(code + ""); 143 // name 144 parentClass.setName(getInternationalName(name)); 145 } 146 else if (getType(code)==COMMODITY) 147 { 148 parentCommodity = new ClassificationNode(); 149 parentCommodity.setId("urn:uuid:" + id); 150 parentClass.addClassificationNode(parentCommodity); 151 // code attribute 152 parentCommodity.setCode(code + ""); 153 // name 154 parentCommodity.setName(getInternationalName(name)); 155 } 156 else if (getType(code)==BUSINESS_FUNCTION) 157 { 158 businessFunction = new ClassificationNode(); 159 businessFunction.setId("urn:uuid:" + id); 160 parentCommodity.addClassificationNode(businessFunction); 161 // code attribute 162 businessFunction.setCode(code + ""); 163 // name 164 businessFunction.setName(getInternationalName(name)); 165 } 166 count++; 167 } 168 return classScheme; 169 } 170 171 /*** 172 Get the type (i.e. Segment, Family, etc.) 173 */ 174 private int getType(int code) 175 { 176 String codeStr = code + ""; 177 if (!codeStr.substring(0, 2).equals("00") && codeStr.substring(2, 4) 178 .equals("00")) 179 { 180 return SEGMENT; 181 } 182 else if (!codeStr.substring(2, 4).equals("00") && 183 codeStr.substring(4, 6).equals("00")) 184 { 185 return FAMILY; 186 } 187 188 else if (!codeStr.substring(4, 6).equals("00") && 189 codeStr.substring(6, 8).equals("00")) 190 { 191 return CLASS; 192 } 193 else if (!codeStr.substring(6, 8).equals("00") && (codeStr.length() <=8 194 || codeStr.substring(8, 10).equals("00"))) 195 { 196 return COMMODITY; 197 } 198 199 else if (codeStr.length() > 8 && !codeStr.substring(8, 10).equals("00")) 200 { 201 return BUSINESS_FUNCTION; 202 } 203 else 204 { 205 throw new RuntimeException("Unknown type found in the input file"); 206 } 207 } 208 209 private Name getInternationalName(String name) 210 { 211 Name internaionalName = new Name(); 212 internaionalName.addInternationalStringTypeItem( 213 getInternationalStringTypeItem(name)); 214 return internaionalName; 215 } 216 217 private Description getInternationalDesc(String desc) 218 { 219 220 Description internaionalDesc = new Description(); 221 internaionalDesc.addInternationalStringTypeItem( 222 getInternationalStringTypeItem(desc)); 223 return internaionalDesc; 224 } 225 226 private InternationalStringTypeItem getInternationalStringTypeItem(String 227 str) 228 { 229 LocalizedString localizedStr = new LocalizedString(); 230 localizedStr.setLang(null); 231 localizedStr.setValue(str); 232 InternationalStringTypeItem inStrTypeItem = new 233 InternationalStringTypeItem(); 234 inStrTypeItem.setLocalizedString(localizedStr); 235 return inStrTypeItem; 236 } 237 }

This page was automatically generated by Maven