code cleaning according to code review recommendations (closes #93)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1822224 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Alain Béarez 2018-01-25 19:59:52 +00:00
parent e2fe5c190a
commit f4a5c3289d
8 changed files with 146 additions and 60 deletions

View File

@ -46,14 +46,9 @@ import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.Beta; import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.apache.poi.xddf.usermodel.XDDFShapeProperties; import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
import org.apache.poi.xslf.usermodel.XSLFChart;
import org.apache.poi.xslf.usermodel.XSLFFactory;
import org.apache.poi.xslf.usermodel.XSLFRelation;
import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFFactory;
import org.apache.poi.xwpf.usermodel.XWPFRelation;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions; import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarChart; import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarChart;
@ -403,7 +398,7 @@ public abstract class XDDFChart extends POIXMLDocumentPart {
* @since POI 4.0.0 * @since POI 4.0.0
*/ */
private PackagePart createWorksheetPart(POIXMLRelation chartRelation, POIXMLRelation chartWorkbookRelation, POIXMLFactory chartFactory) throws InvalidFormatException { private PackagePart createWorksheetPart(POIXMLRelation chartRelation, POIXMLRelation chartWorkbookRelation, POIXMLFactory chartFactory) throws InvalidFormatException {
PackageRelationship xlsx = createRelationshipInChart(XSLFRelation.WORKBOOK_RELATIONSHIP, XSLFFactory.getInstance(), chartIndex); PackageRelationship xlsx = createRelationshipInChart(chartWorkbookRelation, chartFactory, chartIndex);
this.setExternalId(xlsx.getId()); this.setExternalId(xlsx.getId());
return getTargetPart(xlsx); return getTargetPart(xlsx);
} }
@ -419,25 +414,41 @@ public abstract class XDDFChart extends POIXMLDocumentPart {
public void saveWorkbook(XSSFWorkbook workbook) throws IOException, InvalidFormatException { public void saveWorkbook(XSSFWorkbook workbook) throws IOException, InvalidFormatException {
PackagePart worksheetPart = getWorksheetPart(true); PackagePart worksheetPart = getWorksheetPart(true);
if (worksheetPart == null) { if (worksheetPart == null) {
POIXMLRelation chartRelation = null; POIXMLRelation chartRelation = getChartRelation();
POIXMLRelation chartWorkbookRelation = null; POIXMLRelation chartWorkbookRelation = getChartWorkbookRelation();
POIXMLFactory chartFactory = null; POIXMLFactory chartFactory = getChartFactory();
if (this instanceof XSLFChart) { if (chartRelation != null
chartRelation = XSLFRelation.CHART; && chartWorkbookRelation != null
chartWorkbookRelation = XSLFRelation.WORKBOOK_RELATIONSHIP; && chartFactory != null) {
chartFactory = XSLFFactory.getInstance(); worksheetPart = createWorksheetPart(chartRelation, chartWorkbookRelation, chartFactory);
} else {
chartRelation = XWPFRelation.CHART;
chartRelation = XWPFRelation.WORKBOOK_RELATIONSHIP;
chartFactory = XWPFFactory.getInstance();
} }
worksheetPart = createWorksheetPart(chartRelation, chartWorkbookRelation, chartFactory);
} }
try (OutputStream xlsOut = worksheetPart.getOutputStream()) { try (OutputStream xlsOut = worksheetPart.getOutputStream()) {
workbook.write(xlsOut); workbook.write(xlsOut);
} }
} }
/**
*
* @return the chart relation in the implementing subclass.
* @since POI 4.0.0
*/
protected abstract POIXMLRelation getChartRelation();
/**
*
* @return the chart workbook relation in the implementing subclass.
* @since POI 4.0.0
*/
protected abstract POIXMLRelation getChartWorkbookRelation();
/**
*
* @return the chart factory in the implementing subclass.
* @since POI 4.0.0
*/
protected abstract POIXMLFactory getChartFactory();
/** /**
* this method writes the data into sheet * this method writes the data into sheet
* *

View File

@ -21,12 +21,11 @@ package org.apache.poi.xslf.usermodel;
import java.io.IOException; import java.io.IOException;
import org.apache.poi.POIXMLDocument; import org.apache.poi.POIXMLFactory;
import org.apache.poi.POIXMLRelation; import org.apache.poi.POIXMLRelation;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.util.Beta; import org.apache.poi.util.Beta;
import org.apache.poi.xddf.usermodel.chart.XDDFChart; import org.apache.poi.xddf.usermodel.chart.XDDFChart;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTTitle; import org.openxmlformats.schemas.drawingml.x2006.chart.CTTitle;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
@ -55,6 +54,21 @@ public final class XSLFChart extends XDDFChart {
super(part); super(part);
} }
@Override
protected POIXMLRelation getChartRelation() {
return XSLFRelation.CHART;
}
@Override
protected POIXMLRelation getChartWorkbookRelation() {
return XSLFRelation.WORKBOOK;
}
@Override
protected POIXMLFactory getChartFactory() {
return XSLFFactory.getInstance();
}
public XSLFTextShape getTitle() { public XSLFTextShape getTitle() {
if (!chart.isSetTitle()) { if (!chart.isSetTitle()) {
chart.addNewTitle(); chart.addNewTitle();

View File

@ -139,7 +139,7 @@ public class XSLFRelation extends POIXMLRelation {
null null
); );
public static final XSLFRelation WORKBOOK_RELATIONSHIP = new XSLFRelation( public static final XSLFRelation WORKBOOK = new XSLFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
POIXMLDocument.PACK_OBJECT_REL_TYPE, POIXMLDocument.PACK_OBJECT_REL_TYPE,
"/ppt/embeddings/Microsoft_Excel_Worksheet#.xlsx", "/ppt/embeddings/Microsoft_Excel_Worksheet#.xlsx",

View File

@ -26,6 +26,8 @@ import java.util.List;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import org.apache.poi.POIXMLFactory;
import org.apache.poi.POIXMLRelation;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.ss.usermodel.Chart; import org.apache.poi.ss.usermodel.Chart;
import org.apache.poi.ss.usermodel.charts.ChartAxis; import org.apache.poi.ss.usermodel.charts.ChartAxis;
@ -96,6 +98,21 @@ public final class XSSFChart extends XDDFChart implements Chart, ChartAxisFactor
super(part); super(part);
} }
@Override
protected POIXMLRelation getChartRelation() {
return null;
}
@Override
protected POIXMLRelation getChartWorkbookRelation() {
return null;
}
@Override
protected POIXMLFactory getChartFactory() {
return null;
}
/** /**
* Construct a new CTChartSpace bean. By default, it's just an empty placeholder for chart objects. * Construct a new CTChartSpace bean. By default, it's just an empty placeholder for chart objects.
*/ */

View File

@ -21,6 +21,9 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.apache.poi.POIXMLException; import org.apache.poi.POIXMLException;
import org.apache.poi.POIXMLFactory;
import org.apache.poi.POIXMLRelation;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.util.Beta; import org.apache.poi.util.Beta;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
@ -72,6 +75,21 @@ public class XWPFChart extends XDDFChart {
super(part); super(part);
} }
@Override
protected POIXMLRelation getChartRelation() {
return XWPFRelation.CHART;
}
@Override
protected POIXMLRelation getChartWorkbookRelation() {
return XWPFRelation.WORKBOOK;
}
@Override
protected POIXMLFactory getChartFactory() {
return XWPFFactory.getInstance();
}
public Long getChecksum() { public Long getChecksum() {
if (this.checksum == null) { if (this.checksum == null) {
InputStream is = null; InputStream is = null;
@ -129,13 +147,19 @@ public class XWPFChart extends XDDFChart {
} }
/** /**
* initialize in line object * Attach this chart known by its relation id to the given text run.
* *
* @param inline this object is used to adjust the margin and dimension of chart * @param chartRelId the relation id of this chart in its parent document.
* @param run the text run to which this chart will be inlined.
* @throws InvalidFormatException
* @throws IOException
* @since POI 4.0.0 * @since POI 4.0.0
*/ */
protected void setAttachTo(CTInline ctInline) { protected void attach(String chartRelId, XWPFRun run)
this.ctInline = ctInline; throws InvalidFormatException, IOException {
ctInline = run.addChart(chartRelId);
ctInline.addNewExtent();
setChartBoundingBox(DEFAULT_WIDTH, DEFAULT_HEIGHT);
} }
/** /**

View File

@ -63,7 +63,6 @@ import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions; import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComment; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComment;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
@ -1638,28 +1637,17 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
public XWPFChart createChart(int width, int height) throws InvalidFormatException, IOException { public XWPFChart createChart(int width, int height) throws InvalidFormatException, IOException {
//get chart number //get chart number
int chartNumber = getPackagePart().getPackage(). int chartNumber = getNextPartNumber(XWPFRelation.CHART, charts.size() + 1);
getPartsByContentType(XWPFRelation.CHART.getContentType()).size() + 1;
//create relationship in document for new chart //create relationship in document for new chart
RelationPart rp = createRelationship( RelationPart rp = createRelationship(
XWPFRelation.CHART, XWPFFactory.getInstance(), chartNumber, false); XWPFRelation.CHART, XWPFFactory.getInstance(), chartNumber, false);
//get chart relationship id // initialize xwpfchart object
String chartId = rp.getRelationship().getId();
//create paragraph and run object
XWPFRun xRun = this.createParagraph().createRun();
CTInline inline = xRun.addChart(width, height, chartId);
//get package part of xwpfchart object
XWPFChart xwpfChart = rp.getDocumentPart(); XWPFChart xwpfChart = rp.getDocumentPart();
xwpfChart.setChartIndex(chartNumber); xwpfChart.setChartIndex(chartNumber);
xwpfChart.attach(rp.getRelationship().getId(), createParagraph().createRun());
//set in line object into xwpfchart object xwpfChart.setChartBoundingBox(width, height);
xwpfChart.setAttachTo(inline);
//add chart object to chart list //add chart object to chart list
charts.add(xwpfChart); charts.add(xwpfChart);

View File

@ -115,7 +115,7 @@ public final class XWPFRelation extends POIXMLRelation {
null null
); );
public static final XWPFRelation WORKBOOK_RELATIONSHIP = new XWPFRelation( public static final XWPFRelation WORKBOOK = new XWPFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
POIXMLDocument.PACK_OBJECT_REL_TYPE, POIXMLDocument.PACK_OBJECT_REL_TYPE,
"/word/embeddings/Microsoft_Excel_Worksheet#.xlsx", "/word/embeddings/Microsoft_Excel_Worksheet#.xlsx",

View File

@ -40,7 +40,6 @@ import org.apache.xmlbeans.XmlString;
import org.apache.xmlbeans.XmlToken; import org.apache.xmlbeans.XmlToken;
import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl; import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart; import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTRelId;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip; import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties; import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject; import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
@ -134,8 +133,9 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
NodeList kids = t.getDomNode().getChildNodes(); NodeList kids = t.getDomNode().getChildNodes();
for (int n = 0; n < kids.getLength(); n++) { for (int n = 0; n < kids.getLength(); n++) {
if (kids.item(n) instanceof Text) { if (kids.item(n) instanceof Text) {
if (text.length() > 0) if (text.length() > 0) {
text.append("\n"); text.append("\n");
}
text.append(kids.item(n).getNodeValue()); text.append(kids.item(n).getNodeValue());
} }
} }
@ -157,6 +157,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
/** /**
* @deprecated Use {@link XWPFRun#XWPFRun(CTR, IRunBody)} * @deprecated Use {@link XWPFRun#XWPFRun(CTR, IRunBody)}
*/ */
@Deprecated
public XWPFRun(CTR r, XWPFParagraph p) { public XWPFRun(CTR r, XWPFParagraph p) {
this(r, (IRunBody) p); this(r, (IRunBody) p);
} }
@ -219,9 +220,11 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* *
* @deprecated use {@link XWPFRun#getParent()} instead * @deprecated use {@link XWPFRun#getParent()} instead
*/ */
@Deprecated
public XWPFParagraph getParagraph() { public XWPFParagraph getParagraph() {
if (parent instanceof XWPFParagraph) if (parent instanceof XWPFParagraph) {
return (XWPFParagraph) parent; return (XWPFParagraph) parent;
}
return null; return null;
} }
@ -240,8 +243,9 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* For isBold, isItalic etc * For isBold, isItalic etc
*/ */
private static boolean isCTOnOff(CTOnOff onoff) { private static boolean isCTOnOff(CTOnOff onoff) {
if (!onoff.isSetVal()) if (!onoff.isSetVal()) {
return true; return true;
}
final STOnOff.Enum val = onoff.getVal(); final STOnOff.Enum val = onoff.getVal();
return ( return (
(STOnOff.TRUE == val) || (STOnOff.TRUE == val) ||
@ -267,6 +271,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* *
* @return <code>true</code> if the bold property is applied * @return <code>true</code> if the bold property is applied
*/ */
@Override
public boolean isBold() { public boolean isBold() {
CTRPr pr = run.getRPr(); CTRPr pr = run.getRPr();
return pr != null && pr.isSetB() && isCTOnOff(pr.getB()); return pr != null && pr.isSetB() && isCTOnOff(pr.getB());
@ -296,6 +301,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* @param value <code>true</code> if the bold property is applied to * @param value <code>true</code> if the bold property is applied to
* this run * this run
*/ */
@Override
public void setBold(boolean value) { public void setBold(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff bold = pr.isSetB() ? pr.getB() : pr.addNewB(); CTOnOff bold = pr.isSetB() ? pr.getB() : pr.addNewB();
@ -361,8 +367,9 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* @param pos - position in the text array (NB: 0 based) * @param pos - position in the text array (NB: 0 based)
*/ */
public void setText(String value, int pos) { public void setText(String value, int pos) {
if (pos > run.sizeOfTArray()) if (pos > run.sizeOfTArray()) {
throw new ArrayIndexOutOfBoundsException("Value too large for the parameter position in XWPFRun.setText(String value,int pos)"); throw new ArrayIndexOutOfBoundsException("Value too large for the parameter position in XWPFRun.setText(String value,int pos)");
}
CTText t = (pos < run.sizeOfTArray() && pos >= 0) ? run.getTArray(pos) : run.addNewT(); CTText t = (pos < run.sizeOfTArray() && pos >= 0) ? run.getTArray(pos) : run.addNewT();
t.setStringValue(value); t.setStringValue(value);
preserveSpaces(t); preserveSpaces(t);
@ -374,6 +381,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* *
* @return <code>true</code> if the italic property is applied * @return <code>true</code> if the italic property is applied
*/ */
@Override
public boolean isItalic() { public boolean isItalic() {
CTRPr pr = run.getRPr(); CTRPr pr = run.getRPr();
return pr != null && pr.isSetI() && isCTOnOff(pr.getI()); return pr != null && pr.isSetI() && isCTOnOff(pr.getI());
@ -404,6 +412,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* @param value <code>true</code> if the italic property is applied to * @param value <code>true</code> if the italic property is applied to
* this run * this run
*/ */
@Override
public void setItalic(boolean value) { public void setItalic(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff italic = pr.isSetI() ? pr.getI() : pr.addNewI(); CTOnOff italic = pr.isSetI() ? pr.getI() : pr.addNewI();
@ -451,6 +460,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* *
* @return <code>true</code> if the strike property is applied * @return <code>true</code> if the strike property is applied
*/ */
@Override
public boolean isStrikeThrough() { public boolean isStrikeThrough() {
CTRPr pr = run.getRPr(); CTRPr pr = run.getRPr();
return pr != null && pr.isSetStrike() && isCTOnOff(pr.getStrike()); return pr != null && pr.isSetStrike() && isCTOnOff(pr.getStrike());
@ -480,6 +490,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* @param value <code>true</code> if the strike property is applied to * @param value <code>true</code> if the strike property is applied to
* this run * this run
*/ */
@Override
public void setStrikeThrough(boolean value) { public void setStrikeThrough(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff strike = pr.isSetStrike() ? pr.getStrike() : pr.addNewStrike(); CTOnOff strike = pr.isSetStrike() ? pr.getStrike() : pr.addNewStrike();
@ -502,6 +513,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* *
* @return <code>true</code> if the double strike property is applied * @return <code>true</code> if the double strike property is applied
*/ */
@Override
public boolean isDoubleStrikeThrough() { public boolean isDoubleStrikeThrough() {
CTRPr pr = run.getRPr(); CTRPr pr = run.getRPr();
return pr != null && pr.isSetDstrike() && isCTOnOff(pr.getDstrike()); return pr != null && pr.isSetDstrike() && isCTOnOff(pr.getDstrike());
@ -513,61 +525,72 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* *
* @see #setStrikeThrough(boolean) for the rules about this * @see #setStrikeThrough(boolean) for the rules about this
*/ */
@Override
public void setDoubleStrikethrough(boolean value) { public void setDoubleStrikethrough(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff dstrike = pr.isSetDstrike() ? pr.getDstrike() : pr.addNewDstrike(); CTOnOff dstrike = pr.isSetDstrike() ? pr.getDstrike() : pr.addNewDstrike();
dstrike.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); dstrike.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);
} }
@Override
public boolean isSmallCaps() { public boolean isSmallCaps() {
CTRPr pr = run.getRPr(); CTRPr pr = run.getRPr();
return pr != null && pr.isSetSmallCaps() && isCTOnOff(pr.getSmallCaps()); return pr != null && pr.isSetSmallCaps() && isCTOnOff(pr.getSmallCaps());
} }
@Override
public void setSmallCaps(boolean value) { public void setSmallCaps(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff caps = pr.isSetSmallCaps() ? pr.getSmallCaps() : pr.addNewSmallCaps(); CTOnOff caps = pr.isSetSmallCaps() ? pr.getSmallCaps() : pr.addNewSmallCaps();
caps.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); caps.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);
} }
@Override
public boolean isCapitalized() { public boolean isCapitalized() {
CTRPr pr = run.getRPr(); CTRPr pr = run.getRPr();
return pr != null && pr.isSetCaps() && isCTOnOff(pr.getCaps()); return pr != null && pr.isSetCaps() && isCTOnOff(pr.getCaps());
} }
@Override
public void setCapitalized(boolean value) { public void setCapitalized(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff caps = pr.isSetCaps() ? pr.getCaps() : pr.addNewCaps(); CTOnOff caps = pr.isSetCaps() ? pr.getCaps() : pr.addNewCaps();
caps.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); caps.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);
} }
@Override
public boolean isShadowed() { public boolean isShadowed() {
CTRPr pr = run.getRPr(); CTRPr pr = run.getRPr();
return pr != null && pr.isSetShadow() && isCTOnOff(pr.getShadow()); return pr != null && pr.isSetShadow() && isCTOnOff(pr.getShadow());
} }
@Override
public void setShadow(boolean value) { public void setShadow(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff shadow = pr.isSetShadow() ? pr.getShadow() : pr.addNewShadow(); CTOnOff shadow = pr.isSetShadow() ? pr.getShadow() : pr.addNewShadow();
shadow.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); shadow.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);
} }
@Override
public boolean isImprinted() { public boolean isImprinted() {
CTRPr pr = run.getRPr(); CTRPr pr = run.getRPr();
return pr != null && pr.isSetImprint() && isCTOnOff(pr.getImprint()); return pr != null && pr.isSetImprint() && isCTOnOff(pr.getImprint());
} }
@Override
public void setImprinted(boolean value) { public void setImprinted(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff imprinted = pr.isSetImprint() ? pr.getImprint() : pr.addNewImprint(); CTOnOff imprinted = pr.isSetImprint() ? pr.getImprint() : pr.addNewImprint();
imprinted.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); imprinted.setVal(value ? STOnOff.TRUE : STOnOff.FALSE);
} }
@Override
public boolean isEmbossed() { public boolean isEmbossed() {
CTRPr pr = run.getRPr(); CTRPr pr = run.getRPr();
return pr != null && pr.isSetEmboss() && isCTOnOff(pr.getEmboss()); return pr != null && pr.isSetEmboss() && isCTOnOff(pr.getEmboss());
} }
@Override
public void setEmbossed(boolean value) { public void setEmbossed(boolean value) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTOnOff emboss = pr.isSetEmboss() ? pr.getEmboss() : pr.addNewEmboss(); CTOnOff emboss = pr.isSetEmboss() ? pr.getEmboss() : pr.addNewEmboss();
@ -610,37 +633,46 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
ctValign.setVal(STVerticalAlignRun.Enum.forInt(valign.getValue())); ctValign.setVal(STVerticalAlignRun.Enum.forInt(valign.getValue()));
} }
@Override
public int getKerning() { public int getKerning() {
CTRPr pr = run.getRPr(); CTRPr pr = run.getRPr();
if (pr == null || !pr.isSetKern()) if (pr == null || !pr.isSetKern()) {
return 0; return 0;
}
return pr.getKern().getVal().intValue(); return pr.getKern().getVal().intValue();
} }
@Override
public void setKerning(int kern) { public void setKerning(int kern) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTHpsMeasure kernmes = pr.isSetKern() ? pr.getKern() : pr.addNewKern(); CTHpsMeasure kernmes = pr.isSetKern() ? pr.getKern() : pr.addNewKern();
kernmes.setVal(BigInteger.valueOf(kern)); kernmes.setVal(BigInteger.valueOf(kern));
} }
@Override
public boolean isHighlighted() { public boolean isHighlighted() {
CTRPr pr = run.getRPr(); CTRPr pr = run.getRPr();
if (pr == null || !pr.isSetHighlight()) if (pr == null || !pr.isSetHighlight()) {
return false; return false;
if (pr.getHighlight().getVal() == STHighlightColor.NONE) }
if (pr.getHighlight().getVal() == STHighlightColor.NONE) {
return false; return false;
}
return true; return true;
} }
// TODO Provide a wrapper round STHighlightColor, then expose getter/setter // TODO Provide a wrapper round STHighlightColor, then expose getter/setter
// for the highlight colour. Ideally also then add to CharacterRun interface // for the highlight colour. Ideally also then add to CharacterRun interface
@Override
public int getCharacterSpacing() { public int getCharacterSpacing() {
CTRPr pr = run.getRPr(); CTRPr pr = run.getRPr();
if (pr == null || !pr.isSetSpacing()) if (pr == null || !pr.isSetSpacing()) {
return 0; return 0;
}
return pr.getSpacing().getVal().intValue(); return pr.getSpacing().getVal().intValue();
} }
@Override
public void setCharacterSpacing(int twips) { public void setCharacterSpacing(int twips) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTSignedTwipsMeasure spc = pr.isSetSpacing() ? pr.getSpacing() : pr.addNewSpacing(); CTSignedTwipsMeasure spc = pr.isSetSpacing() ? pr.getSpacing() : pr.addNewSpacing();
@ -675,6 +707,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
/** /**
* Alias for {@link #getFontFamily()} * Alias for {@link #getFontFamily()}
*/ */
@Override
public String getFontName() { public String getFontName() {
return getFontFamily(); return getFontFamily();
} }
@ -688,7 +721,9 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
*/ */
public String getFontFamily(FontCharRange fcr) { public String getFontFamily(FontCharRange fcr) {
CTRPr pr = run.getRPr(); CTRPr pr = run.getRPr();
if (pr == null || !pr.isSetRFonts()) return null; if (pr == null || !pr.isSetRFonts()) {
return null;
}
CTFonts fonts = pr.getRFonts(); CTFonts fonts = pr.getRFonts();
switch (fcr == null ? FontCharRange.ascii : fcr) { switch (fcr == null ? FontCharRange.ascii : fcr) {
@ -752,6 +787,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* *
* @return value representing the font size * @return value representing the font size
*/ */
@Override
public int getFontSize() { public int getFontSize() {
CTRPr pr = run.getRPr(); CTRPr pr = run.getRPr();
return (pr != null && pr.isSetSz()) ? pr.getSz().getVal().divide(new BigInteger("2")).intValue() : -1; return (pr != null && pr.isSetSz()) ? pr.getSz().getVal().divide(new BigInteger("2")).intValue() : -1;
@ -769,6 +805,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* *
* @param size The font size as number of point measurements. * @param size The font size as number of point measurements.
*/ */
@Override
public void setFontSize(int size) { public void setFontSize(int size) {
BigInteger bint = new BigInteger("" + size); BigInteger bint = new BigInteger("" + size);
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
@ -1030,15 +1067,13 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
/** /**
* this method add chart template into document * this method add chart template into document
* *
* @param width set width of chart object
* @param height set height of chart object
* @param chartRelId relation id of chart in document relation file * @param chartRelId relation id of chart in document relation file
* @throws InvalidFormatException * @throws InvalidFormatException
* @throws IOException * @throws IOException
* @since POI 4.0.0 * @since POI 4.0.0
*/ */
@Internal @Internal
public CTInline addChart(int width, int height, String chartRelId) public CTInline addChart(String chartRelId)
throws InvalidFormatException, IOException { throws InvalidFormatException, IOException {
try { try {
CTInline inline = run.addNewDrawing().addNewInline(); CTInline inline = run.addNewDrawing().addNewInline();
@ -1069,11 +1104,6 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
//This name is not visible in Word anywhere. //This name is not visible in Word anywhere.
docPr.setName("chart " + id); docPr.setName("chart " + id);
CTPositiveSize2D extent = inline.addNewExtent();
//set hegiht and width of drawaing object;
extent.setCx(width);
extent.setCy(height);
return inline; return inline;
} catch (XmlException e) { } catch (XmlException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
@ -1095,6 +1125,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
/** /**
* Returns the string version of the text and the phonetic string * Returns the string version of the text and the phonetic string
*/ */
@Override
public String toString() { public String toString() {
String phonetic = getPhonetic(); String phonetic = getPhonetic();
if (phonetic.length() > 0) { if (phonetic.length() > 0) {
@ -1108,6 +1139,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
* Returns the string version of the text, with tabs and * Returns the string version of the text, with tabs and
* carriage returns in place of their xml equivalents. * carriage returns in place of their xml equivalents.
*/ */
@Override
public String text() { public String text() {
StringBuilder text = new StringBuilder(64); StringBuilder text = new StringBuilder(64);