1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package org.apache.openoffice.ooxml.parser; 23 24 import java.util.HashMap; 25 import java.util.Map; 26 import java.util.Map.Entry; 27 import java.util.Vector; 28 29 public class NameMap 30 { NameMap(final Vector<String[]> aData)31 NameMap (final Vector<String[]> aData) 32 { 33 maNameToIdMap = new HashMap<>(); 34 maIdToNameMap = new Vector<>(); 35 36 for (final String[] aLine : aData) 37 { 38 final int nId = Integer.parseInt(aLine[1]); 39 40 maNameToIdMap.put(aLine[2], nId); 41 42 if (maIdToNameMap.size() <= nId) 43 maIdToNameMap.setSize(nId+1); 44 maIdToNameMap.set(nId, aLine[2]); 45 } 46 } 47 48 49 50 GetIdForName( final String sName)51 public int GetIdForName ( 52 final String sName) 53 { 54 if ( ! maNameToIdMap.containsKey(sName)) 55 throw new RuntimeException("token '"+sName+"' is not known"); 56 57 return maNameToIdMap.get(sName); 58 } 59 60 61 62 GetIdForOptionalName( final String sName)63 public int GetIdForOptionalName ( 64 final String sName) 65 { 66 final Integer aId = maNameToIdMap.get(sName); 67 if (aId == null) 68 return -1; 69 else 70 return aId; 71 } 72 73 74 75 GetNameForId(final int nId)76 public String GetNameForId (final int nId) 77 { 78 if (nId == -1) 79 return "<none>"; 80 else 81 return maIdToNameMap.get(nId); 82 } 83 84 85 86 GetNameCount()87 public int GetNameCount () 88 { 89 return maIdToNameMap.size(); 90 } 91 92 93 94 95 /** Return the ids of all states whose names match the given pattern. 96 */ GetMatchingStateIds(final String sPattern)97 public Vector<Integer> GetMatchingStateIds (final String sPattern) 98 { 99 final Vector<Integer> aStateIds = new Vector<>(); 100 for (final Entry<String,Integer> aEntry : maNameToIdMap.entrySet()) 101 { 102 if (aEntry.getKey().matches(sPattern)) 103 aStateIds.add(aEntry.getValue()); 104 } 105 return aStateIds; 106 } 107 108 109 110 111 private final Map<String,Integer> maNameToIdMap; 112 private final Vector<String> maIdToNameMap; 113 } 114