Home > Back-end >  There is a program, one of the function is not called, but also carried out, why?
There is a program, one of the function is not called, but also carried out, why?

Time:05-08

Same function in the following program and had not been called, but the execution, and delete this function doesn't work, why?
Thank you
================

Import the Java. IO. InputStream;
Import the Java. Util. Iterator;
Import the Java. Util. LinkedHashMap;
Import the Java. Util. The Map;

Import javax.mail, XML parsers. A ParserConfigurationException;

The import org, apache poi. Openxml4j. Opc. OPCPackage;
The import org, apache poi. Openxml4j. Opc. PackageAccess;
The import org. Apache. Poi. Util. XMLHelper;
The import org, apache poi. XSSF. Eventusermodel. XSSFReader;
The import org, apache poi. XSSF. Model. SharedStringsTable;
The import org. XML. Sax. Attributes;
The import org. XML.. Sax ContentHandler.
The import org. XML. Sax. InputSource;
The import org. XML. Sax. SAXException;
The import org. XML. Sax. XMLReader;
The import org. XML. Sax. Helpers. DefaultHandler;

/* *
* XSSF and SAX Event (API), basic example.
* See {@ link XLSX2CSV} for a fuller example of doing
* XSLX processing with the XSSF Event code.
*/
@ SuppressWarnings ({" Java: S106 ", "Java: S4823", "Java: S1192"})
Public class test {
Public void processFirstSheet (String filename) throws the Exception {
Try (OPCPackage PKG=OPCPackage. Open (filename, PackageAccess. READ)) {
XSSFReader r=new XSSFReader (PKG);
R. gutierrez etSharedStringsTable SharedStringsTable SST=();

The XMLReader parser=fetchSheetParser (SST);

//process the first sheet
Try (InputStream r. gutierrez etSheetsData sheet=(). The next ()) {
InputSource sheetSource=new InputSource (sheet);
Parser. Parse (sheetSource);
}
}
}

Public XMLReader fetchSheetParser SharedStringsTable (SST) throws SAXException, a ParserConfigurationException {
The XMLReader parser=XMLHelper. NewXMLReader ();
ContentHandler handler=new SheetHandler (SST);
Parser. SetContentHandler (handler);
Return parser.
}

/* *
* See org. XML. Sax. Helpers. DefaultHandler javadocs
*/
Private static class SheetHandler extends DefaultHandler {
Private final SharedStringsTable SST;
Private String lastContents;
Private Boolean nextIsString;
Private Boolean inlineStr;
Private final LruCache LruCache=new LruCache<> (50);

Private static class LruCache Extends LinkedHashMap {
Private final int maxEntries;

Public LruCache (final int maxEntries) {
Super (maxEntries + 1 and 1.0 f, true);
Enclosing maxEntries=maxEntries;
}

@ Override
Protected Boolean removeEldestEntry (final Map. Entry Eldest) {
Return super size () & gt; MaxEntries;
}
}

Private SheetHandler SharedStringsTable (SST) {
This. SST=SST;
}

@ Override
Public void startElement (String uri, String localName, String name,
The Attributes Attributes) throws SAXException {
//c=& gt; The cell
If (name. Equals (" c ")) {
//Print the cell reference
System. The out. Print (attributes. GetValue (" r ") + "-");
//Figure out if the value is an index in the SST
String cellType=attributes. The getValue (" t ");
NextIsString=cellType!=null & amp; & CellType. Equals (" s ");
InlineStr=cellType!=null & amp; & InlineStr cellType. Equals (" ");
}
//the Clear contents cache
LastContents="";
}

//@ Override
Public void same (String uri, String localName, String name) throws SAXException
{
If (nextIsString & amp; & ! LastContents. The trim (). The isEmpty ())
{
The Integer independence idx=Integer. The valueOf (lastContents);
LastContents=lruCache. Get (independence idx);
If (lastContents==null & amp; & ! LruCache. Either containsKey (independence idx))
{
LastContents=SST. GetItemAt (independence idx). Get string ();
LruCache. Put (independence idx lastContents);
}
NextIsString=false;
}
If (name. Equals (" v ") | | (inlineStr & amp; & Name. The equals (" c ")))
{
System. The out. Println (lastContents);
}
}

@ Override
Public void characters (char [] ch, int start, int length) throws SAXException {//NOSONAR
LastContents +=new String (ch, start and length).
}
}

Public static void main (String [] args) throws the Exception {
The test howto=new test ();
Howto. ProcessFirstSheet (args [0]);
}
}
  • Related