Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (c) 2019 MovieLabs
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.movielabs.mddflib.status.offer;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.XMLHelper;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.xpath.XPathFactory;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import com.movielabs.mddf.MddfContext;
import com.movielabs.mddf.MddfContext.FILE_FMT;
import com.movielabs.mddflib.avails.xml.Pedigree;
import com.movielabs.mddflib.avails.xml.RowDataSrc;
import com.movielabs.mddflib.avails.xml.streaming.XlsxDataTermination;
import com.movielabs.mddflib.avails.xml.AbstractXmlBuilder;
import com.movielabs.mddflib.avails.xml.AvailsSheet.Version;
import com.movielabs.mddflib.avails.xml.AvailsWrkBook.RESULT_STATUS;
import com.movielabs.mddflib.logging.LogMgmt;
import com.movielabs.mddflib.util.xml.FormatConverter;
import com.movielabs.mddflib.util.xml.MddfTarget;
import com.movielabs.mddflib.util.xml.SchemaWrapper;
/**
* Converts an OfferStatus file using the XLSX format to an XML DOM
* representation using the XSSFReader API. This is an event-driven
* approach to SAX processing that results in a greatly reduced memory
* footprint.
*
* @author L. Levin, Critical Architectures LLC
*
*/
public class StreamingOStatusBuilder extends AbstractXmlBuilder {
/**
* ContentHandler that allows termination of the ingest process by the
* StreamingXmlBuilder at any time. Termination is initiated by the
* handler throwing a XlsxDataTermination exception. The builder
* signals to the handler it should initiate termination by setting the field
* terminateNow to true.
*
* @author L. Levin, Critical Architectures LLC
*
*/
public class AvailSheetXMLHandler extends XSSFSheetXMLHandler implements ContentHandler {
/**
* @param styles
* @param comments
* @param strings
* @param sheetContentsHandler
* @param dataFormatter
* @param formulasNotResults
*/
public AvailSheetXMLHandler(StylesTable styles, CommentsTable comments, ReadOnlySharedStringsTable strings,
SheetContentsHandler sheetContentsHandler, DataFormatter dataFormatter, boolean formulasNotResults) {
super(styles, comments, strings, sheetContentsHandler, dataFormatter, formulasNotResults);
}
/*
* (non-Javadoc)
*
* @see
* org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler#startElement(java.lang
* .String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
public void endElement(String uri, String localName, String qName) throws SAXException {
if (terminateNow) {
throw new XlsxDataTermination("Ran out of data on row " + lastDataRow);
}
super.endElement(uri, localName, qName);
}
}
/**
* A SheetContentsHandler that uses the XSSF Event SAX helpers to do
* most of the work of parsing the Sheet XML. Data values are accumulated on a
* row-by-row basis. When the end of a row is signaled by the invoking of
* endRow(), the accumulated data is passed to
* StreamingXmlBuilder.processRow().
*/
private class ParseByRow implements SheetContentsHandler {
private boolean haveFirstRow = false;
private boolean firstCellOfRow;
private int currentRow = -1;
private int currentCol = -1;
private ArrayList rowContentsAsList;
private String[] rowContentsAsArray;
private int curCell = 0;
private boolean rowHasData;
ParseByRow() {
rowContentsAsList = new ArrayList();
}
@Override
public void startRow(int rowNum) {
// Prepare for this row
firstCellOfRow = true;
currentCol = -1;
currentRow = rowNum; // need if creating missing cellReference
curCell = 0;
if (haveFirstRow) {
rowContentsAsArray = new String[rowContentsAsList.size()];
} else {
rowContentsAsArray = null;
}
rowHasData = false;
}
@Override
public void endRow(int rowNum) {
if (rowNum == 0) {
rowContentsAsArray = new String[rowContentsAsList.size()];
rowContentsAsArray = rowContentsAsList.toArray(rowContentsAsArray);
haveFirstRow = true;
}
try {
processRow(rowContentsAsArray, rowNum, rowHasData);
} catch (XlsxDataTermination e) {
// System.out.println("Empty rows; ronNum=" + rowNum);
}
}
@Override
public void cell(String cellReference, String formattedValue, XSSFComment comment) {
// gracefully handle missing CellRef here in a similar way as XSSFCell does
if (cellReference == null) {
cellReference = new CellAddress(currentRow, currentCol).formatAsString();
}
// Did we miss any cells?
int thisCol = (new CellReference(cellReference)).getCol();
int missedCols = thisCol - currentCol - 1;
for (int i = 0; i < missedCols; i++) {
addDataToRow("");
}
currentCol = thisCol;
addDataToRow(formattedValue);
}
private void addDataToRow(String data) {
if (!haveFirstRow) {
rowContentsAsList.add(data);
} else if (curCell >= rowContentsAsArray.length) {
// error
String colID = LogMgmt.mapColNum(curCell);
int rowID = currentRow + 1;
String msg = "Ignoring cell " + colID + " in row " + rowID + ": out of bounds";
logger.log(LogMgmt.LEV_WARN, LogMgmt.TAG_XLATE, msg, null, moduleId);
return;
} else {
rowContentsAsArray[curCell++] = data;
}
if (data != null && !data.isEmpty()) {
rowHasData = true;
}
}
@Override
public void headerFooter(String text, boolean isHeader, String tagName) {
// TODO Auto-generated method stub
}
}
// ==============================================
/**
* FOR TESTING ONLY!!
*
* @param args
*/
public static void main(String[] args) {
}
public static final String moduleId = "OStatusBldr";
/**
* Number of contiguous empty rows that will result in termination of input
* processing.
*/
public static final int TERMINATION_THRESHOLD = 5;
private LogMgmt logger;
private Version templateVersion;
private File curSrcXslxFile;
private String shortDesc;
private String xsdVersion;
private String mdMecVer;
private String mdVer;
private Namespace availsNSpace;
private Namespace mdNSpace;
private Namespace mdMecNSpace;
private SchemaWrapper availsSchema;
private SchemaWrapper mdSchema;
private SchemaWrapper mdMecSchema;
private HashMap headerMap;
private Map