Skip to content

Commit

Permalink
Remove deprecated usages of HSSFCellStyle methods #71
Browse files Browse the repository at this point in the history
  • Loading branch information
jreznot committed May 22, 2018
1 parent c088a39 commit 51fa58d
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.haulmont.yarg.structure.ReportOutputType;
import com.haulmont.yarg.structure.ReportTemplate;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.ConstructorUtils;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
Expand All @@ -39,6 +40,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.google.common.base.Preconditions.checkNotNull;

public abstract class AbstractFormatter implements ReportFormatter {
public static final String SIMPLE_ALIAS_REGEXP = "\\$\\{([A-z0-9_]+?)\\}";

Expand All @@ -57,21 +60,22 @@ public abstract class AbstractFormatter implements ReportFormatter {
protected ReportTemplate reportTemplate;
protected ReportOutputType outputType;
protected OutputStream outputStream;
protected Set<ReportOutputType> supportedOutputTypes = new HashSet<ReportOutputType>();
protected Set<ReportOutputType> supportedOutputTypes = new HashSet<>();
protected DefaultFormatProvider defaultFormatProvider;

/**
* Chain of responsibility for content inliners
*/
protected List<ContentInliner> contentInliners = new ArrayList<ContentInliner>();
protected List<ContentInliner> contentInliners = new ArrayList<>();

protected AbstractFormatter(FormatterFactoryInput formatterFactoryInput) {
Preconditions.checkNotNull("\"rootBand\" parameter can not be null", formatterFactoryInput.getRootBand());
Preconditions.checkNotNull("\"reportTemplate\" parameter can not be null", formatterFactoryInput.getReportTemplate());
checkNotNull(formatterFactoryInput.getRootBand(), "\"rootBand\" parameter can not be null");
checkNotNull(formatterFactoryInput.getReportTemplate(), "\"reportTemplate\" parameter can not be null");

this.rootBand = formatterFactoryInput.getRootBand();
this.reportTemplate = formatterFactoryInput.getReportTemplate();
this.outputType = (formatterFactoryInput.getOutputType() != null) ? formatterFactoryInput.getOutputType() : reportTemplate.getOutputType();
this.outputType = (formatterFactoryInput.getOutputType() != null)
? formatterFactoryInput.getOutputType() : reportTemplate.getOutputType();
this.outputStream = formatterFactoryInput.getOutputStream();

this.contentInliners.add(new BitmapContentInliner());
Expand Down Expand Up @@ -121,12 +125,14 @@ protected String formatValue(Object value, String parameterName, String fullPara
if (formatString != null) {
if (formatString.startsWith("class:")) {
String className = formatString.replaceFirst("class:", "");
ValueFormat valueFormat;
try {
ValueFormat valueFormat = (ValueFormat) Class.forName(className).newInstance();
valueString = valueFormat.format(value);
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
Class<?> valueFormatterClass = Class.forName(className);
valueFormat = (ValueFormat) ConstructorUtils.invokeConstructor(valueFormatterClass);
} catch (ReflectiveOperationException e) {
throw new ReportingException("An error occurred while applying custom format", e);
}
valueString = valueFormat.format(value);
} else if (value == null) {
valueString = "";
} else if (value instanceof Number) {
Expand Down Expand Up @@ -185,7 +191,7 @@ protected String defaultFormat(Object value) {
}

protected String insertBandDataToString(BandData bandData, String resultStr) {
List<String> parametersToInsert = new ArrayList<String>();
List<String> parametersToInsert = new ArrayList<>();
Matcher matcher = UNIVERSAL_ALIAS_PATTERN.matcher(resultStr);
while (matcher.find()) {
parametersToInsert.add(unwrapParameterName(matcher.group()));
Expand Down Expand Up @@ -223,7 +229,7 @@ protected BandData findBandByPath(String path) {
}

protected BandPathAndParameterName separateBandNameAndParameterName(String alias) {
List<String> bandPathList = new ArrayList<String>();
List<String> bandPathList = new ArrayList<>();
String[] pathParts = alias.split("\\.");
BandData currentBand = rootBand;
for (String pathPart : pathParts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ public void renderDocument() {
protected void updateTableOfContents() {
try {
MainDocumentPart documentPart = wordprocessingMLPackage.getMainDocumentPart();
Document wmlDocumentEl = documentPart.getJaxbElement();
Document wmlDocumentEl;
try {
wmlDocumentEl = documentPart.getContents();
} catch (Docx4JException e) {
throw new RuntimeException("Unable to get document content", e);
}
Body body = wmlDocumentEl.getBody();

TocFinder finder = new TocFinder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ public class XLSFormatter extends AbstractFormatter {
protected int rowsAddedByVerticalBand = 0;
protected int rowsAddedByHorizontalBand = 0;

protected Map<String, List<SheetRange>> mergeRegionsForRangeNames = new HashMap<String, List<SheetRange>>();
protected Map<HSSFSheet, HSSFSheet> templateToResultSheetsMapping = new HashMap<HSSFSheet, HSSFSheet>();
protected Map<String, Bounds> templateBounds = new HashMap<String, Bounds>();
protected Map<String, List<SheetRange>> mergeRegionsForRangeNames = new HashMap<>();
protected Map<HSSFSheet, HSSFSheet> templateToResultSheetsMapping = new HashMap<>();
protected Map<String, Bounds> templateBounds = new HashMap<>();

protected AreaDependencyManager areaDependencyManager = new AreaDependencyManager();
protected Map<Area, List<Area>> areasDependency = areaDependencyManager.getAreasDependency();

protected List<Integer> orderedPicturesId = new ArrayList<Integer>();
protected Map<String, EscherAggregate> sheetToEscherAggregate = new HashMap<String, EscherAggregate>();
protected List<Integer> orderedPicturesId = new ArrayList<>();
protected Map<String, EscherAggregate> sheetToEscherAggregate = new HashMap<>();

protected Map<HSSFSheet, HSSFPatriarch> drawingPatriarchsMap = new HashMap<HSSFSheet, HSSFPatriarch>();
protected List<XlsHint> hints = new ArrayList<XlsHint>();
protected Map<HSSFSheet, HSSFPatriarch> drawingPatriarchsMap = new HashMap<>();
protected List<XlsHint> hints = new ArrayList<>();

protected DocumentConverter documentConverter;

Expand Down Expand Up @@ -304,7 +304,7 @@ protected void writeHorizontalBand(BandData band, HSSFSheet templateSheet, HSSFS
if (crefs != null) {
addRangeBounds(band, crefs);

ArrayList<HSSFRow> resultRows = new ArrayList<HSSFRow>();
ArrayList<HSSFRow> resultRows = new ArrayList<>();

int currentRowNum = -1;
int currentRowCount = -1;
Expand Down Expand Up @@ -388,7 +388,7 @@ protected void writeVerticalBand(BandData band, HSSFSheet templateSheet, HSSFShe
String rangeName = band.getName();
CellReference[] crefs = getRangeContent(templateWorkbook, rangeName);

Set<Integer> addedRowNumbers = new HashSet<Integer>();
Set<Integer> addedRowNumbers = new HashSet<>();

if (crefs != null) {
addRangeBounds(band, crefs);
Expand Down Expand Up @@ -501,7 +501,7 @@ protected void initMergeRegions(HSSFSheet currentSheet) {
String name = aNamedRange.getNameName();
SheetRange sheetRange = new SheetRange(mergedRegion, currentSheet.getSheetName());
if (mergeRegionsForRangeNames.get(name) == null) {
ArrayList<SheetRange> list = new ArrayList<SheetRange>();
ArrayList<SheetRange> list = new ArrayList<>();
list.add(sheetRange);
mergeRegionsForRangeNames.put(name, list);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,12 @@ protected void findVerticalDependencies() {

protected void updateOutlines() {
for (Document.SheetWrapper sheetWrapper : result.getWorksheets()) {
Worksheet resultWorksheet = sheetWrapper.getWorksheet().getJaxbElement();
Worksheet resultWorksheet;
try {
resultWorksheet = sheetWrapper.getWorksheet().getContents();
} catch (Docx4JException e) {
throw new RuntimeException("Unable to get worksheet contents");
}
Worksheet templateWorksheet = template.getSheetByName(sheetWrapper.getName());

if (templateWorksheet.getSheetFormatPr() != null) {
Expand Down Expand Up @@ -312,14 +317,20 @@ protected void updateFormulas() {

protected void updateConditionalFormatting() {
for (Document.SheetWrapper sheetWrapper : result.getWorksheets()) {
Worksheet worksheet = sheetWrapper.getWorksheet().getJaxbElement();
Worksheet worksheet;
try {
worksheet = sheetWrapper.getWorksheet().getContents();
} catch (Docx4JException e) {
throw new RuntimeException("Unable to get worksheet contents");
}

for (CTConditionalFormatting ctConditionalFormatting : worksheet.getConditionalFormatting()) {
List<String> references = new ArrayList<String>();
List<String> references = new ArrayList<>();
for (String ref : ctConditionalFormatting.getSqref()) {
Range formulaRange = Range.fromRange(sheetWrapper.getName(), ref);
for (Range templateRange : rangeDependencies.templates()) {
if (templateRange.contains(formulaRange)) {
List<Range> resultRanges = new ArrayList<Range>(rangeDependencies.resultsForTemplate(templateRange));
List<Range> resultRanges = new ArrayList<>(rangeDependencies.resultsForTemplate(templateRange));
for (Range resultRange : resultRanges) {
Offset offset = calculateOffset(templateRange, resultRange);
Range shift = formulaRange.copy().shift(offset.downOffset, offset.rightOffset);
Expand Down Expand Up @@ -360,8 +371,8 @@ protected void processOuterFormulas(int formulaCount, CTCalcChain calculationCha

for (Range templateRange : rangeDependencies.templates()) {
if (templateRange.containsAny(formulaRanges)) {
List<Range> resultRanges = new ArrayList<Range>(rangeDependencies.resultsForTemplate(templateRange));
List<Range> newRanges = new ArrayList<Range>();
List<Range> resultRanges = new ArrayList<>(rangeDependencies.resultsForTemplate(templateRange));
List<Range> newRanges = new ArrayList<>();
for (Range resultRange : resultRanges) {
BandData bandData = bandsForRanges.bandForResultRange(resultRange);
boolean hasSameFormulaBand = false;
Expand Down Expand Up @@ -464,7 +475,11 @@ protected CTCalcChain getCalculationChain() {
try {
CalcChain part = (CalcChain) result.getPackage().getParts().get(new PartName("/xl/calcChain.xml"));
if (part != null) {
calculationChain = part.getJaxbElement();
try {
calculationChain = part.getContents();
} catch (Docx4JException e) {
throw new RuntimeException("Unable to get contents of part", e);
}
calculationChain.getC().clear();
}
} catch (InvalidFormatException e) {
Expand All @@ -473,7 +488,8 @@ protected CTCalcChain getCalculationChain() {
return calculationChain;
}

protected void updateFormula(Cell cellWithFormula, Range originalFormulaRange, Range formulaRange, CTCalcChain calculationChain, int formulaCount) {
protected void updateFormula(Cell cellWithFormula, Range originalFormulaRange, Range formulaRange,
CTCalcChain calculationChain, int formulaCount) {
CTCellFormula formula = cellWithFormula.getF();
formula.setValue(formula.getValue().replace(originalFormulaRange.toRange(), formulaRange.toRange()));
if (originalFormulaRange.isOneCellRange() && formulaRange.isOneCellRange()) {
Expand Down Expand Up @@ -726,13 +742,13 @@ protected Row ensureNecessaryRowsCreated(Range templateRange, Worksheet resultSh
}

protected List<Cell> copyCells(BandData band, Range templateRange, List<Row> resultSheetRows, Row firstRow, Worksheet resultSheet) {
List<Cell> resultCells = new ArrayList<Cell>();
List<Cell> resultCells = new ArrayList<>();
for (int i = 0; i <= templateRange.getLastRow() - templateRange.getFirstRow(); i++) {
Range oneRowRange = new Range(templateRange.getSheet(),
templateRange.getFirstColumn(), templateRange.getFirstRow() + i,
templateRange.getLastColumn(), templateRange.getFirstRow() + i);
Map<CellReference, Cell> cellsForOneRowRange = template.getCellsByRange(oneRowRange);
List<Cell> templateCells = new ArrayList<Cell>(cellsForOneRowRange.values());
List<Cell> templateCells = new ArrayList<>(cellsForOneRowRange.values());
Row templateRow = !templateCells.isEmpty() ?
(Row) templateCells.get(0).getParent() :
resultSheet.getSheetData().getRow().get(oneRowRange.getFirstRow() - 1);
Expand Down Expand Up @@ -771,13 +787,10 @@ protected void createFakeTemplateCellsForEmptyOnes(Range oneRowRange,
}
}

Collections.sort(templateCells, new Comparator<Cell>() {
@Override
public int compare(Cell o1, Cell o2) {
CellReference cellReference1 = referencesToCells.inverse().get(o1);
CellReference cellReference2 = referencesToCells.inverse().get(o2);
return cellReference1.compareTo(cellReference2);
}
templateCells.sort((o1, o2) -> {
CellReference cellReference1 = referencesToCells.inverse().get(o1);
CellReference cellReference2 = referencesToCells.inverse().get(o2);
return cellReference1.compareTo(cellReference2);
});
}
}
Expand Down Expand Up @@ -846,7 +859,14 @@ protected List<Cell> copyCells(Range templateRange, BandData bandData, Row newRo

WorksheetPart worksheetPart = null;
for (Document.SheetWrapper sheetWrapper : result.getWorksheets()) {
if (sheetWrapper.getWorksheet().getJaxbElement() == resultWorksheet) {
Worksheet contents;
try {
contents = sheetWrapper.getWorksheet().getContents();
} catch (Docx4JException e) {
throw new RuntimeException("Unable to get worksheet contents", e);
}

if (contents == resultWorksheet) {
worksheetPart = sheetWrapper.getWorksheet();
}
}
Expand Down Expand Up @@ -1012,7 +1032,12 @@ protected void writeToOutputStream(SpreadsheetMLPackage mlPackage, OutputStream

protected void updateHeaderAndFooter() {
for (Document.SheetWrapper sheetWrapper : result.getWorksheets()) {
Worksheet worksheet = sheetWrapper.getWorksheet().getJaxbElement();
Worksheet worksheet;
try {
worksheet = sheetWrapper.getWorksheet().getContents();
} catch (Docx4JException e) {
throw new RuntimeException("Unable to get contents of worksheet", e);
}
if (worksheet.getHeaderFooter() != null) {
CTHeaderFooter headerFooter = worksheet.getHeaderFooter();
if (headerFooter.getOddHeader() != null) {
Expand All @@ -1037,7 +1062,7 @@ protected void updateSheetNames() {
}

protected String insertBandDataToString(String resultStr) {
List<String> parametersToInsert = new ArrayList<String>();
List<String> parametersToInsert = new ArrayList<>();
Matcher matcher = UNIVERSAL_ALIAS_PATTERN.matcher(resultStr);
while (matcher.find()) {
parametersToInsert.add(unwrapParameterName(matcher.group()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@


import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -69,7 +69,7 @@ public List<Long> findPid(String host, int port) {
List r = IOUtils.readLines(process.getInputStream());
for (Object output : r) {
NetStatInfo info = new NetStatInfo((String) output);
if (info.localPort == port && ObjectUtils.equals(host, info.localAddress))
if (info.localPort == port && Objects.equals(host, info.localAddress))
return Collections.singletonList(info.pid);
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ public void inlineToXls(HSSFPatriarch patriarch, HSSFCell resultCell, Object par
}

@Override
public void inlineToDoc(OfficeComponent officeComponent, XTextRange textRange, XText destination, Object paramValue, Matcher paramsMatcher) throws Exception {
public void inlineToDoc(OfficeComponent officeComponent, XTextRange textRange, XText destination, Object paramValue,
Matcher paramsMatcher) throws Exception {
try {
if (paramValue != null) {
Image image = new Image(paramValue, paramsMatcher);
Expand Down Expand Up @@ -322,7 +323,7 @@ protected Part resolveTextPartForDOCX(Text text, WordprocessingMLPackage wordPac
java.util.List<SectionWrapper> sectionWrappers = wordPackage.getDocumentModel().getSections();
for (SectionWrapper sw : sectionWrappers) {
HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
List<Part> parts = Arrays.<Part>asList(hfp.getFirstHeader(), hfp.getDefaultHeader(), hfp.getEvenHeader(),
List<Part> parts = Arrays.asList(hfp.getFirstHeader(), hfp.getDefaultHeader(), hfp.getEvenHeader(),
hfp.getFirstFooter(), hfp.getDefaultFooter(), hfp.getEvenFooter());
for (Part part : parts) {
TextMatchCallback callback = new TextMatchCallback(text);
Expand Down
Loading

0 comments on commit 51fa58d

Please sign in to comment.