Search in sources :

Example 1 with CTCustomGeometry2D

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;
    }
}
Also used : CTCustomGeometry2D(org.apache.poi.sl.draw.binding.CTCustomGeometry2D) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 2 with CTCustomGeometry2D

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);
}
Also used : CTCustomGeometry2D(org.apache.poi.sl.draw.binding.CTCustomGeometry2D) Test(org.junit.Test)

Example 3 with CTCustomGeometry2D

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);
}
Also used : InputStream(java.io.InputStream) CTCustomGeometry2D(org.apache.poi.sl.draw.binding.CTCustomGeometry2D) JAXBContext(javax.xml.bind.JAXBContext) EventFilter(javax.xml.stream.EventFilter) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) CustomGeometry(org.apache.poi.sl.draw.geom.CustomGeometry) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 4 with CTCustomGeometry2D

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));
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) CTCustomGeometry2D(org.apache.poi.sl.draw.binding.CTCustomGeometry2D) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) EventFilter(javax.xml.stream.EventFilter) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

CTCustomGeometry2D (org.apache.poi.sl.draw.binding.CTCustomGeometry2D)4 JAXBContext (javax.xml.bind.JAXBContext)3 Unmarshaller (javax.xml.bind.Unmarshaller)3 EventFilter (javax.xml.stream.EventFilter)2 XMLEventReader (javax.xml.stream.XMLEventReader)2 XMLInputFactory (javax.xml.stream.XMLInputFactory)2 StartElement (javax.xml.stream.events.StartElement)2 XMLEvent (javax.xml.stream.events.XMLEvent)2 InputStream (java.io.InputStream)1 JAXBException (javax.xml.bind.JAXBException)1 CustomGeometry (org.apache.poi.sl.draw.geom.CustomGeometry)1 Test (org.junit.Test)1