/** * The low level code to insert {@code <c:chart>} tag into * {@code<a:graphicData>}. * * Here is the schema (ECMA-376): * <pre> * {@code * <complexType name="CT_GraphicalObjectData"> * <sequence> * <any minOccurs="0" maxOccurs="unbounded" processContents="strict"/> * </sequence> * <attribute name="uri" type="xsd:token"/> * </complexType> * } * </pre> */ private void appendChartElement(CTGraphicalObjectData data, String id) { String r_namespaceUri = STRelationshipId.type.getName().getNamespaceURI(); String c_namespaceUri = XSSFDrawing.NAMESPACE_C; XmlCursor cursor = data.newCursor(); cursor.toNextToken(); cursor.beginElement(new QName(c_namespaceUri, "chart", "c")); cursor.insertAttributeWithValue(new QName(r_namespaceUri, "id", "r"), id); cursor.dispose(); data.setUri(c_namespaceUri); }
private static String getUri(CTGraphicalObjectFrame shape) { final CTGraphicalObject g = shape.getGraphic(); if (g == null) { return null; } CTGraphicalObjectData gd = g.getGraphicData(); return (gd == null) ? null : gd.getUri(); }
private void copyChart(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape) { XSLFSlide slide = (XSLFSlide) getSheet(); XSLFSheet src = srcShape.getSheet(); String xpath = "declare namespace c='http://schemas.openxmlformats.org/drawingml/2006/chart' c:chart"; XmlObject[] obj = objData.selectPath(xpath); if (obj != null && obj.length == 1) { XmlCursor c = obj[0].newCursor(); try { // duplicate chart with embedded workbook QName idQualifiedName = new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id"); String id = c.getAttributeText(idQualifiedName); XSLFChart srcChart = (XSLFChart) src.getRelationById(id); XSLFChart chartCopy = slide.getSlideShow().createChart(slide); chartCopy.importContent(srcChart); chartCopy.setWorkbook(srcChart.getWorkbook()); c.setAttributeText(idQualifiedName, slide.getRelationId(chartCopy)); } catch (InvalidFormatException e) { throw new POIXMLException(e); } catch (IOException e) { throw new POIXMLException(e); } c.dispose(); } }
/*package*/ XSLFObjectShape(CTGraphicalObjectFrame shape, XSLFSheet sheet){ super(shape, sheet); CTGraphicalObjectData god = shape.getGraphic().getGraphicData(); XmlCursor xc = god.newCursor(); // select oleObj potentially under AlternateContent // usually the mc:Choice element will be selected first xc.selectPath("declare namespace p='"+PML_NS+"' .//p:oleObj"); try { if (!xc.toNextSelection()) { throw new IllegalStateException("p:oleObj element was not found in\n " + god); } XmlObject xo = xc.getObject(); // Pesky XmlBeans bug - see Bugzilla #49934 // it never happens when using the full ooxml-schemas jar but may happen with the abridged poi-ooxml-schemas if (xo instanceof XmlAnyTypeImpl){ String errStr = "Schemas (*.xsb) for CTOleObject can't be loaded - usually this happens when OSGI " + "loading is used and the thread context classloader has no reference to " + "the xmlbeans classes - use POIXMLTypeLoader.setClassLoader() to set the loader, " + "e.g. with CTOleObject.class.getClassLoader()" ; throw new IllegalStateException(errStr); } _oleObject = (CTOleObject)xo; } finally { xc.dispose(); } }
/** * Construct a new XSSFGraphicFrame object. * * @param drawing the XSSFDrawing that owns this frame * @param ctGraphicFrame the XML bean that stores this frame content */ protected XSSFGraphicFrame(XSSFDrawing drawing, CTGraphicalObjectFrame ctGraphicFrame) { this.drawing = drawing; // protected field on XSSFShape this.graphicFrame = ctGraphicFrame; // TODO: there may be a better way to delegate this CTGraphicalObjectData graphicData = graphicFrame.getGraphic().getGraphicData(); if (graphicData != null) { NodeList nodes = graphicData.getDomNode().getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { final Node node = nodes.item(i); // if the frame references a chart, associate the chart with this instance if (node.getNodeName().equals("c:chart")) { // this better succeed or the document is invalid POIXMLDocumentPart relation = drawing.getRelationById(node.getAttributes().getNamedItem("r:id").getNodeValue()); // Do XWPF charts need similar treatment? if (relation instanceof XSSFChart) { ((XSSFChart) relation).setGraphicFrame(this); } } } } }
/*package*/ XSLFTable(CTGraphicalObjectFrame shape, XSLFSheet sheet){ super(shape, sheet); CTGraphicalObjectData god = shape.getGraphic().getGraphicData(); XmlCursor xc = god.newCursor(); try { if (!xc.toChild(XSLFRelation.NS_DRAWINGML, "tbl")) { throw new IllegalStateException("a:tbl element was not found in\n " + god); } XmlObject xo = xc.getObject(); // Pesky XmlBeans bug - see Bugzilla #49934 // it never happens when using the full ooxml-schemas jar but may happen with the abridged poi-ooxml-schemas if (xo instanceof XmlAnyTypeImpl){ String errStr = "Schemas (*.xsb) for CTTable can't be loaded - usually this happens when OSGI " + "loading is used and the thread context classloader has no reference to " + "the xmlbeans classes" ; throw new IllegalStateException(errStr); } _table = (CTTable)xo; } finally { xc.dispose(); } _rows = new ArrayList<>(_table.sizeOfTrArray()); for(CTTableRow row : _table.getTrList()) { _rows.add(new XSLFTableRow(row, this)); } updateRowColIndexes(); }
/** * Construct a new XSSFGraphicFrame object. * * @param drawing the XSSFDrawing that owns this frame * @param ctGraphicFrame the XML bean that stores this frame content */ protected XSSFGraphicFrame(XSSFDrawing drawing, CTGraphicalObjectFrame ctGraphicFrame) { this.drawing = drawing; // protected field on XSSFShape this.graphicFrame = ctGraphicFrame; // TODO: there may be a better way to delegate this CTGraphicalObjectData graphicData = graphicFrame.getGraphic().getGraphicData(); if (graphicData != null) { NodeList nodes = graphicData.getDomNode().getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { final Node node = nodes.item(i); // if the frame references a chart, associate the chart with this instance if (node.getNodeName().equals("c:chart")) { // this better succeed or the document is invalid POIXMLDocumentPart relation = drawing.getRelationById(node.getAttributes().getNamedItem("r:id").getNodeValue()); // Do XWPF charts need similar treatment? if (relation instanceof XSSFChart) { ((XSSFChart) relation).setGraphicFrame(this); } } } } }
static CTGraphicalObjectFrame prototype(int shapeId){ CTGraphicalObjectFrame frame = CTGraphicalObjectFrame.Factory.newInstance(); CTGraphicalObjectFrameNonVisual nvGr = frame.addNewNvGraphicFramePr(); CTNonVisualDrawingProps cnv = nvGr.addNewCNvPr(); cnv.setName("Table " + shapeId); cnv.setId(shapeId); nvGr.addNewCNvGraphicFramePr().addNewGraphicFrameLocks().setNoGrp(true); nvGr.addNewNvPr(); frame.addNewXfrm(); CTGraphicalObjectData gr = frame.addNewGraphic().addNewGraphicData(); XmlCursor grCur = gr.newCursor(); grCur.toNextToken(); grCur.beginElement(new QName(XSLFRelation.NS_DRAWINGML, "tbl")); CTTable tbl = CTTable.Factory.newInstance(); tbl.addNewTblPr(); tbl.addNewTblGrid(); XmlCursor tblCur = tbl.newCursor(); tblCur.moveXmlContents(grCur); tblCur.dispose(); grCur.dispose(); gr.setUri(TABLE_URI); return frame; }
/*package*/ XSLFObjectShape(CTGraphicalObjectFrame shape, XSLFSheet sheet){ super(shape, sheet); CTGraphicalObjectData god = shape.getGraphic().getGraphicData(); XmlCursor xc = god.newCursor(); // select oleObj potentially under AlternateContent // usually the mc:Choice element will be selected first xc.selectPath("declare namespace p='"+PML_NS+"' .//p:oleObj"); try { if (!xc.toNextSelection()) { throw new IllegalStateException("p:oleObj element was not found in\n " + god); } XmlObject xo = xc.getObject(); // Pesky XmlBeans bug - see Bugzilla #49934 // it never happens when using the full ooxml-schemas jar but may happen with the abridged poi-ooxml-schemas if (xo instanceof XmlAnyTypeImpl){ String errStr = "Schemas (*.xsb) for CTOleObject can't be loaded - usually this happens when OSGI " + "loading is used and the thread context classloader has no reference to " + "the xmlbeans classes - use POIXMLTypeLoader.setClassLoader() to set the loader, " + "e.g. with CTOleObject.class.getClassLoader()" ; throw new IllegalStateException(errStr); } _oleObject = (CTOleObject)xo; } finally { xc.dispose(); } }
@Override void copy(XSLFShape sh){ super.copy(sh); CTGraphicalObjectData data = ((CTGraphicalObjectFrame)getXmlObject()).getGraphic().getGraphicData(); String uri = data.getUri(); if(uri.equals("http://schemas.openxmlformats.org/drawingml/2006/diagram")){ copyDiagram(data, (XSLFGraphicFrame)sh); } if(uri.equals("http://schemas.openxmlformats.org/drawingml/2006/chart")){ copyChart(data, (XSLFGraphicFrame)sh); } else { // TODO support other types of objects } }
private void copyDiagram(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape){ String xpath = "declare namespace dgm='http://schemas.openxmlformats.org/drawingml/2006/diagram' $this//dgm:relIds"; XmlObject[] obj = objData.selectPath(xpath); if(obj != null && obj.length == 1){ XmlCursor c = obj[0].newCursor(); XSLFSheet sheet = srcShape.getSheet(); try { String dm = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "dm")); PackageRelationship dmRel = sheet.getPackagePart().getRelationship(dm); PackagePart dmPart = sheet.getPackagePart().getRelatedPart(dmRel); getSheet().importPart(dmRel, dmPart); String lo = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "lo")); PackageRelationship loRel = sheet.getPackagePart().getRelationship(lo); PackagePart loPart = sheet.getPackagePart().getRelatedPart(loRel); getSheet().importPart(loRel, loPart); String qs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "qs")); PackageRelationship qsRel = sheet.getPackagePart().getRelationship(qs); PackagePart qsPart = sheet.getPackagePart().getRelatedPart(qsRel); getSheet().importPart(qsRel, qsPart); String cs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "cs")); PackageRelationship csRel = sheet.getPackagePart().getRelationship(cs); PackagePart csPart = sheet.getPackagePart().getRelatedPart(csRel); getSheet().importPart(csRel, csPart); } catch (InvalidFormatException e){ throw new POIXMLException(e); } c.dispose(); } }
/*package*/ XSLFTable(CTGraphicalObjectFrame shape, XSLFSheet sheet){ super(shape, sheet); CTGraphicalObjectData god = shape.getGraphic().getGraphicData(); XmlCursor xc = god.newCursor(); try { if (!xc.toChild(XSLFRelation.NS_DRAWINGML, "tbl")) { throw new IllegalStateException("a:tbl element was not found in\n " + god); } XmlObject xo = xc.getObject(); // Pesky XmlBeans bug - see Bugzilla #49934 // it never happens when using the full ooxml-schemas jar but may happen with the abridged poi-ooxml-schemas if (xo instanceof XmlAnyTypeImpl){ String errStr = "Schemas (*.xsb) for CTTable can't be loaded - usually this happens when OSGI " + "loading is used and the thread context classloader has no reference to " + "the xmlbeans classes" ; throw new IllegalStateException(errStr); } _table = (CTTable)xo; } finally { xc.dispose(); } _rows = new ArrayList<>(_table.sizeOfTrArray()); for(CTTableRow row : _table.getTrList()) { _rows.add(new XSLFTableRow(row, this)); } updateRowColIndexes(); }
private static String getUri(CTGraphicalObjectFrame shape) { final CTGraphicalObject g = shape.getGraphic(); if (g == null) { return null; } CTGraphicalObjectData gd = g.getGraphicData(); return (gd == null) ? null : gd.getUri(); }
/*package*/ XSLFTable(CTGraphicalObjectFrame shape, XSLFSheet sheet){ super(shape, sheet); XmlObject[] rs = shape.getGraphic().getGraphicData() .selectPath("declare namespace a='http://schemas.openxmlformats.org/drawingml/2006/main' ./a:tbl"); if (rs.length == 0) { throw new IllegalStateException("a:tbl element was not found in\n " + shape.getGraphic().getGraphicData()); } // Pesky XmlBeans bug - see Bugzilla #49934 // it never happens when using the full ooxml-schemas jar but may happen with the abridged poi-ooxml-schemas if(rs[0] instanceof XmlAnyTypeImpl){ try { rs[0] = CTTable.Factory.parse(rs[0].toString()); }catch (XmlException e){ throw new POIXMLException(e); } } _table = (CTTable) rs[0]; _rows = new ArrayList<XSLFTableRow>(_table.sizeOfTrArray()); for(CTTableRow row : _table.getTrList()) _rows.add(new XSLFTableRow(row, this)); }
/** * The low level code to insert {@code <c:chart>} tag into * {@code<a:graphicData>}. * * Here is the schema (ECMA-376): * <pre> * {@code * <complexType name="CT_GraphicalObjectData"> * <sequence> * <any minOccurs="0" maxOccurs="unbounded" processContents="strict"/> * </sequence> * <attribute name="uri" type="xsd:token"/> * </complexType> * } * </pre> */ private void appendChartElement(CTGraphicalObjectData data, String id) { String r_namespaceUri = STRelationshipId.type.getName().getNamespaceURI(); String c_namespaceUri = XSSFDrawing.NAMESPACE_C; XmlCursor cursor = data.newCursor(); cursor.toNextToken(); cursor.beginElement(new QName(c_namespaceUri, "chart", "c")); cursor.insertAttributeWithValue(new QName(r_namespaceUri, "id", "r"), id); cursor.dispose(); data.setUri(c_namespaceUri); }
if ( graphicData != null ) XmlCursor c = graphicData.newCursor(); c.selectPath( "./*" ); while ( c.toNextSelection() )
static XSLFGraphicFrame create(CTGraphicalObjectFrame shape, XSLFSheet sheet){ String uri = shape.getGraphic().getGraphicData().getUri(); if(XSLFTable.TABLE_URI.equals(uri)){ return new XSLFTable(shape, sheet); } else { return new XSLFGraphicFrame(shape, sheet); } }
private void copyChart(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape) { XSLFSlide slide = (XSLFSlide) getSheet(); XSLFSheet src = srcShape.getSheet(); String xpath = "declare namespace c='http://schemas.openxmlformats.org/drawingml/2006/chart' c:chart"; XmlObject[] obj = objData.selectPath(xpath); if (obj != null && obj.length == 1) { XmlCursor c = obj[0].newCursor(); try { // duplicate chart with embedded workbook QName idQualifiedName = new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id"); String id = c.getAttributeText(idQualifiedName); XSLFChart srcChart = (XSLFChart) src.getRelationById(id); XSLFChart chartCopy = slide.getSlideShow().createChart(slide); chartCopy.importContent(srcChart); chartCopy.setWorkbook(srcChart.getWorkbook()); c.setAttributeText(idQualifiedName, slide.getRelationId(chartCopy)); } catch (InvalidFormatException e) { throw new POIXMLException(e); } catch (IOException e) { throw new POIXMLException(e); } c.dispose(); } }
/** * The low level code to insert {@code <c:chart>} tag into * {@code<a:graphicData>}. * * Here is the schema (ECMA-376): * <pre> * {@code * <complexType name="CT_GraphicalObjectData"> * <sequence> * <any minOccurs="0" maxOccurs="unbounded" processContents="strict"/> * </sequence> * <attribute name="uri" type="xsd:token"/> * </complexType> * } * </pre> */ private void appendChartElement(CTGraphicalObjectData data, String id) { String r_namespaceUri = STRelationshipId.type.getName().getNamespaceURI(); String c_namespaceUri = XSSFDrawing.NAMESPACE_C; XmlCursor cursor = data.newCursor(); cursor.toNextToken(); cursor.beginElement(new QName(c_namespaceUri, "chart", "c")); cursor.insertAttributeWithValue(new QName(r_namespaceUri, "id", "r"), id); cursor.dispose(); data.setUri(c_namespaceUri); }