Page 1 of 1

Preset Shapes Definitions

PostPosted: Fri Jun 06, 2014 5:35 pm
by camallere_h
Hi, I am new to Docx4j and I am trying to retreive information of shapes from docx file. I have a question about shapes, is there a preset shapes definitions for docx4j?
I have seen this http://svn.apache.org/repos/asf/openoffice/trunk/main/oox/source/export/presetShapeDefinitions.xml and have seen the same file being used on apache poi.
That implementation used that same file to retreive the guides, handles and commands automatically. Does docx4j have that shapeDefinitions concept?

Re: Preset Shapes Definitions

PostPosted: Sat Jun 07, 2014 9:10 pm
by jason
http://blogs.msdn.com/b/openspecificati ... ingml.aspx explains where that file comes from.

There is no reason we couldn't add the file to docx4j - and use it - but that has not been done yet.

If you do work in this area and would like to contribute something (on the basis of the ASLv2 license), we'd welcome that.

Re: Preset Shapes Definitions

PostPosted: Mon Jun 09, 2014 5:29 pm
by camallere_h
Thanks for the reply and information, very much appreciate it. That implementation is a very good addition to this library. :D
I want to try and develop that kind of implementation but can you point me to the right direction?
This is what I have right now:
1. One implementation using Apache poi used xml beans to convert the presetShapesDefinitions.xml to their corresponding classes
2. Also using Apache poi implementation I saw that the name of the shape is retreived by the following manner
Code: Select all
    int typeOfShape = ctShapePropertiesObject.getPrstGeom().getPrst().intValue();     //where ctShapeproperties is an object of CTShapeProperties
    String shapeName = STShapeType.Enum.forInt(typeOfShape ).toString();      // the shapeName corresponds to the names inside presetShapesDefinitions.xml


Do you know how to implement or do you have any hints/ideas how can I get those 2 above?

Re: Preset Shapes Definitions

PostPosted: Tue Jun 10, 2014 7:12 am
by jason
Here is a starting point:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
/*
 * Inspired/converted from org.apache.poi.xslf.model.geom.PresetGeometries
 *  which is
 *  
 *  ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */


import java.io.InputStream;
import java.util.LinkedHashMap;

import javax.xml.parsers.DocumentBuilderFactory;

import org.docx4j.XmlUtils;
import org.docx4j.dml.CTCustomGeometry2D;
import org.docx4j.jaxb.Context;
import org.docx4j.utils.ResourceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class PresetGeometries  extends LinkedHashMap<String, CTCustomGeometry2D> {
       
        // based on https://svn.apache.org/repos/asf/poi/tr ... tries.java
        // by Yegor Kozlov
       
        private static Logger log = LoggerFactory.getLogger(PresetGeometries.class);   
       
    private static PresetGeometries _inst;
   
    public static PresetGeometries getInstance(){
        if(_inst == null) _inst = new PresetGeometries();

        return _inst;
    }
   
    private PresetGeometries(){
        try {
            InputStream is =
                    ResourceUtils.getResource("presetShapeDefinitions.xml");
            read(is);
        } catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    private void read(InputStream is) throws Exception {
       
        DocumentBuilderFactory dbf = XmlUtils.getDocumentBuilderFactory();
                dbf.setNamespaceAware(true);

            Document domDoc = dbf.newDocumentBuilder().parse(is);
           
            Element presetShapeDefinitons = domDoc.getDocumentElement();
        NodeList nodes = presetShapeDefinitons.getChildNodes();

        for (int i = 0; i < nodes.getLength(); i++) {
               
           Node node = nodes.item(i);
           
           if (!(node instanceof Element)) continue;
           
           String name = node.getLocalName();
           CTCustomGeometry2D geom = (CTCustomGeometry2D)XmlUtils.unmarshal(node, Context.jc, CTCustomGeometry2D.class);

           if(containsKey(name)) {
               log.warn("Duplicate definition of " + name) ;
           }
           put(name, geom);
           
           System.out.println(name);
           
        }          
               
       
    }  
   
        public static void main(String[] args) throws Exception {

                getInstance();
               
        }

}

 
Parsed in 0.020 seconds, using GeSHi 1.0.8.4

Re: Preset Shapes Definitions

PostPosted: Tue Jun 10, 2014 2:02 pm
by camallere_h
Wow thank you very much for that code!!! :D Thank you very much, that really did it. Before I was thinking of traversing each node in the presetShapesDefinition.xml but I was not confident about that. Also I did not know this code is so wonderful
Code: Select all
CTCustomGeometry2D geom = (CTCustomGeometry2D)XmlUtils.unmarshal(node, Context.jc, CTCustomGeometry2D.class);

the conversion to docx4j classes was my problem before but that did it. Thank you :-)

Is it also possible to have a CTShaeProperties object from CTShape/CTShapeType? I want to check the name of the shape.