|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.apache.poi.xssf.usermodel.XSSFRelation.NS_SPREADSHEETML; |
|
|
import static org.apache.poi.xssf.usermodel.XSSFRelation.NS_SPREADSHEETML; |
|
|
|
|
|
|
|
|
|
|
|
import javax.xml.parsers.ParserConfigurationException; |
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
import java.io.InputStream; |
|
|
import java.io.InputStream; |
|
|
import java.io.PushbackInputStream; |
|
|
import java.io.PushbackInputStream; |
|
|
import java.util.ArrayList; |
|
|
import java.util.ArrayList; |
|
|
|
|
|
import java.util.HashMap; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
import javax.xml.parsers.ParserConfigurationException; |
|
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
import org.apache.poi.openxml4j.opc.OPCPackage; |
|
|
import org.apache.poi.openxml4j.opc.OPCPackage; |
|
|
import org.apache.poi.openxml4j.opc.PackagePart; |
|
|
import org.apache.poi.openxml4j.opc.PackagePart; |
|
|
|
|
|
|
|
|
*/ |
|
|
*/ |
|
|
private List<String> strings; |
|
|
private List<String> strings; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Map of phonetic strings (if they exist) indexed |
|
|
|
|
|
* with the integer matching the index in strings |
|
|
|
|
|
*/ |
|
|
|
|
|
private Map<Integer, String> phoneticStrings; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @param pkg The {@link OPCPackage} to use as basis for the shared-strings table. |
|
|
* @param pkg The {@link OPCPackage} to use as basis for the shared-strings table. |
|
|
* @throws IOException If reading the data from the package fails. |
|
|
* @throws IOException If reading the data from the package fails. |
|
|
|
|
|
|
|
|
return strings.get(idx); |
|
|
return strings.get(idx); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Return the phonetic string at a given index. |
|
|
|
|
|
* Returns <code>null</code> if no phonetic string |
|
|
|
|
|
* exists at that index. |
|
|
|
|
|
* @param idx |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
public String getPhoneticStringAt(int idx) { |
|
|
|
|
|
//avoid an NPE. If the parser hasn't |
|
|
|
|
|
//yet hit <sst/> phoneticStrings could be null |
|
|
|
|
|
if (phoneticStrings == null) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
return phoneticStrings.get(idx); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public List<String> getItems() { |
|
|
public List<String> getItems() { |
|
|
return strings; |
|
|
return strings; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//// ContentHandler methods //// |
|
|
//// ContentHandler methods //// |
|
|
|
|
|
|
|
|
private StringBuffer characters; |
|
|
private StringBuffer characters; |
|
|
|
|
|
private StringBuffer rphCharacters; |
|
|
private boolean tIsOpen; |
|
|
private boolean tIsOpen; |
|
|
|
|
|
private boolean inRPh; |
|
|
|
|
|
|
|
|
public void startElement(String uri, String localName, String name, |
|
|
public void startElement(String uri, String localName, String name, |
|
|
Attributes attributes) throws SAXException { |
|
|
Attributes attributes) throws SAXException { |
|
|
if (uri != null && ! uri.equals(NS_SPREADSHEETML)) { |
|
|
if (uri != null && ! uri.equals(NS_SPREADSHEETML)) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ("sst".equals(localName)) { |
|
|
if ("sst".equals(localName)) { |
|
|
String count = attributes.getValue("count"); |
|
|
String count = attributes.getValue("count"); |
|
|
if(count != null) this.count = Integer.parseInt(count); |
|
|
if(count != null) this.count = Integer.parseInt(count); |
|
|
|
|
|
|
|
|
if(uniqueCount != null) this.uniqueCount = Integer.parseInt(uniqueCount); |
|
|
if(uniqueCount != null) this.uniqueCount = Integer.parseInt(uniqueCount); |
|
|
|
|
|
|
|
|
this.strings = new ArrayList<String>(this.uniqueCount); |
|
|
this.strings = new ArrayList<String>(this.uniqueCount); |
|
|
|
|
|
|
|
|
|
|
|
this.phoneticStrings = new HashMap<Integer, String>(); |
|
|
characters = new StringBuffer(); |
|
|
characters = new StringBuffer(); |
|
|
|
|
|
rphCharacters = new StringBuffer(); |
|
|
} else if ("si".equals(localName)) { |
|
|
} else if ("si".equals(localName)) { |
|
|
characters.setLength(0); |
|
|
characters.setLength(0); |
|
|
} else if ("t".equals(localName)) { |
|
|
} else if ("t".equals(localName)) { |
|
|
tIsOpen = true; |
|
|
tIsOpen = true; |
|
|
|
|
|
} else if ("rPh".equals(localName)) { |
|
|
|
|
|
inRPh = true; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (uri != null && ! uri.equals(NS_SPREADSHEETML)) { |
|
|
if (uri != null && ! uri.equals(NS_SPREADSHEETML)) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ("si".equals(localName)) { |
|
|
if ("si".equals(localName)) { |
|
|
strings.add(characters.toString()); |
|
|
strings.add(characters.toString()); |
|
|
|
|
|
if (rphCharacters.length() > 0) { |
|
|
|
|
|
phoneticStrings.put(strings.size()-1, rphCharacters.toString()); |
|
|
|
|
|
rphCharacters.setLength(0); |
|
|
|
|
|
} |
|
|
} else if ("t".equals(localName)) { |
|
|
} else if ("t".equals(localName)) { |
|
|
tIsOpen = false; |
|
|
|
|
|
|
|
|
tIsOpen = false; |
|
|
|
|
|
} else if ("rPh".equals(localName)) { |
|
|
|
|
|
inRPh = false; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
*/ |
|
|
public void characters(char[] ch, int start, int length) |
|
|
public void characters(char[] ch, int start, int length) |
|
|
throws SAXException { |
|
|
throws SAXException { |
|
|
if (tIsOpen) |
|
|
|
|
|
characters.append(ch, start, length); |
|
|
|
|
|
|
|
|
if (tIsOpen) { |
|
|
|
|
|
if (inRPh) { |
|
|
|
|
|
rphCharacters.append(ch, start, length); |
|
|
|
|
|
} else { |
|
|
|
|
|
characters.append(ch, start, length); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |