use of org.apache.poi.sl.draw.binding.CTCustomGeometry2D in project poi by apache.
the class PresetGeometries method convertCustomGeometry.
/**
* Convert a single CustomGeometry object, i.e. from xmlbeans
*/
public static CustomGeometry convertCustomGeometry(XMLStreamReader staxReader) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(BINDING_PACKAGE);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<CTCustomGeometry2D> el = unmarshaller.unmarshal(staxReader, CTCustomGeometry2D.class);
return new CustomGeometry(el.getValue());
} catch (JAXBException e) {
LOG.log(POILogger.ERROR, "Unable to parse single custom geometry", e);
return null;
}
}
use of org.apache.poi.sl.draw.binding.CTCustomGeometry2D in project poi by apache.
the class TestFormulaParser method testParse.
@Test
public void testParse() {
Formula[] ops = { new Guide("adj1", "val 100"), new Guide("adj2", "val 200"), new Guide("adj3", "val -1"), // a1 = 100*2 / 200
new Guide("a1", "*/ adj1 2 adj2"), // a2 = 200 + a1 - 100
new Guide("a2", "+- adj2 a1 adj1"), // a3 = (100 + 200) / 200
new Guide("a3", "+/ adj1 adj2 adj2"), // a4 = adj3 > 0 ? adj1 : adj2
new Guide("a4", "?: adj3 adj1 adj2"), new Guide("a5", "abs -2") };
CustomGeometry geom = new CustomGeometry(new CTCustomGeometry2D());
Context ctx = new Context(geom, null, null);
for (Formula fmla : ops) {
ctx.evaluate(fmla);
}
assertEquals(100.0, ctx.getValue("adj1"), 0.0);
assertEquals(200.0, ctx.getValue("adj2"), 0.0);
assertEquals(1.0, ctx.getValue("a1"), 0.0);
assertEquals(101.0, ctx.getValue("a2"), 0.0);
assertEquals(1.5, ctx.getValue("a3"), 0.0);
assertEquals(200.0, ctx.getValue("a4"), 0.0);
assertEquals(2.0, ctx.getValue("a5"), 0.0);
}
use of org.apache.poi.sl.draw.binding.CTCustomGeometry2D in project poi by apache.
the class DrawSimpleShape method getCustomGeometry.
protected static CustomGeometry getCustomGeometry(String name, Graphics2D graphics) {
@SuppressWarnings("unchecked") Map<String, CustomGeometry> presets = (graphics == null) ? null : (Map<String, CustomGeometry>) graphics.getRenderingHint(Drawable.PRESET_GEOMETRY_CACHE);
if (presets == null) {
presets = new HashMap<String, CustomGeometry>();
if (graphics != null) {
graphics.setRenderingHint(Drawable.PRESET_GEOMETRY_CACHE, presets);
}
String packageName = "org.apache.poi.sl.draw.binding";
InputStream presetIS = Drawable.class.getResourceAsStream("presetShapeDefinitions.xml");
// StAX:
EventFilter startElementFilter = new EventFilter() {
@Override
public boolean accept(XMLEvent event) {
return event.isStartElement();
}
};
try {
XMLInputFactory staxFactory = XMLInputFactory.newInstance();
XMLEventReader staxReader = staxFactory.createXMLEventReader(presetIS);
XMLEventReader staxFiltRd = staxFactory.createFilteredReader(staxReader, startElementFilter);
// Ignore StartElement:
staxFiltRd.nextEvent();
// JAXB:
JAXBContext jaxbContext = JAXBContext.newInstance(packageName);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
while (staxFiltRd.peek() != null) {
StartElement evRoot = (StartElement) staxFiltRd.peek();
String cusName = evRoot.getName().getLocalPart();
// XMLEvent ev = staxReader.nextEvent();
JAXBElement<org.apache.poi.sl.draw.binding.CTCustomGeometry2D> el = unmarshaller.unmarshal(staxReader, CTCustomGeometry2D.class);
CTCustomGeometry2D cusGeom = el.getValue();
presets.put(cusName, new CustomGeometry(cusGeom));
}
staxFiltRd.close();
staxReader.close();
} catch (Exception e) {
throw new RuntimeException("Unable to load preset geometries.", e);
} finally {
IOUtils.closeQuietly(presetIS);
}
}
return presets.get(name);
}
use of org.apache.poi.sl.draw.binding.CTCustomGeometry2D in project poi by apache.
the class PresetGeometries method init.
@SuppressWarnings("unused")
public void init(InputStream is) throws XMLStreamException, JAXBException {
// StAX:
EventFilter startElementFilter = new EventFilter() {
@Override
public boolean accept(XMLEvent event) {
return event.isStartElement();
}
};
XMLInputFactory staxFactory = XMLInputFactory.newFactory();
XMLEventReader staxReader = staxFactory.createXMLEventReader(is);
XMLEventReader staxFiltRd = staxFactory.createFilteredReader(staxReader, startElementFilter);
// ignore StartElement:
/* XMLEvent evDoc = */
staxFiltRd.nextEvent();
// JAXB:
JAXBContext jaxbContext = JAXBContext.newInstance(BINDING_PACKAGE);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
long cntElem = 0;
while (staxFiltRd.peek() != null) {
StartElement evRoot = (StartElement) staxFiltRd.peek();
String name = evRoot.getName().getLocalPart();
JAXBElement<CTCustomGeometry2D> el = unmarshaller.unmarshal(staxReader, CTCustomGeometry2D.class);
CTCustomGeometry2D cus = el.getValue();
cntElem++;
if (containsKey(name)) {
LOG.log(POILogger.WARN, "Duplicate definition of " + name);
}
put(name, new CustomGeometry(cus));
}
}
Aggregations