OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 83 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 83 Rev 180
Line 12... Line 12...
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.openoffice;
14
 package org.openconcerto.openoffice;
15
 
15
 
16
import static org.openconcerto.openoffice.ODPackage.RootElement.CONTENT;
16
import static org.openconcerto.openoffice.ODPackage.RootElement.CONTENT;
-
 
17
import static org.openconcerto.xml.Step.createAttributeStep;
-
 
18
import static org.openconcerto.xml.Step.createAttributeStepFromQualifiedName;
-
 
19
import static org.openconcerto.xml.Step.createElementStep;
-
 
20
import static org.openconcerto.xml.Step.createElementStepFromQualifiedName;
-
 
21
 
17
import org.openconcerto.openoffice.ODPackage.RootElement;
22
import org.openconcerto.openoffice.ODPackage.RootElement;
18
import org.openconcerto.openoffice.style.data.DataStyle;
23
import org.openconcerto.openoffice.style.data.DataStyle;
19
import org.openconcerto.utils.Base64;
24
import org.openconcerto.utils.Base64;
20
import org.openconcerto.utils.CollectionUtils;
25
import org.openconcerto.utils.CollectionUtils;
21
import org.openconcerto.utils.FileUtils;
26
import org.openconcerto.utils.FileUtils;
Line 26... Line 31...
26
import org.openconcerto.xml.Step;
31
import org.openconcerto.xml.Step;
27
import org.openconcerto.xml.Step.Axis;
32
import org.openconcerto.xml.Step.Axis;
28
 
33
 
29
import java.awt.Point;
34
import java.awt.Point;
30
import java.io.File;
35
import java.io.File;
-
 
36
import java.io.FileOutputStream;
31
import java.io.IOException;
37
import java.io.IOException;
32
import java.io.InputStream;
38
import java.io.InputStream;
33
import java.io.OutputStream;
39
import java.io.OutputStream;
34
import java.math.BigDecimal;
40
import java.math.BigDecimal;
35
import java.net.URI;
41
import java.net.URI;
Line 44... Line 50...
44
import java.util.ListIterator;
50
import java.util.ListIterator;
45
import java.util.Map;
51
import java.util.Map;
46
import java.util.Map.Entry;
52
import java.util.Map.Entry;
47
import java.util.Set;
53
import java.util.Set;
48
 
54
 
49
import org.apache.commons.collections.Transformer;
-
 
50
import org.jdom.Attribute;
55
import org.jdom.Attribute;
51
import org.jdom.Content;
56
import org.jdom.Content;
52
import org.jdom.DocType;
57
import org.jdom.DocType;
53
import org.jdom.Document;
58
import org.jdom.Document;
54
import org.jdom.Element;
59
import org.jdom.Element;
Line 1040... Line 1045...
1040
     * @param elem the elem to be trimmed.
1045
     * @param elem the elem to be trimmed.
1041
     * @throws JDOMException if an error occurs.
1046
     * @throws JDOMException if an error occurs.
1042
     */
1047
     */
1043
    protected final void detachDuplicate(Element elem) throws JDOMException {
1048
    protected final void detachDuplicate(Element elem) throws JDOMException {
1044
        final String singularName = elem.getName().substring(0, elem.getName().length() - 1);
1049
        final String singularName = elem.getName().substring(0, elem.getName().length() - 1);
1045
        final List thisNames = getXPath("./text:" + singularName + "s/text:" + singularName + "/@text:name").selectNodes(getChild("body"));
1050
        final SimpleXMLPath<Attribute> simplePath = SimpleXMLPath.create(createElementStep(singularName + "s", "text"), createElementStep(singularName, "text"), createAttributeStep("name", "text"));
1046
        org.apache.commons.collections.CollectionUtils.transform(thisNames, new Transformer() {
-
 
1047
            public Object transform(Object obj) {
-
 
1048
                return ((Attribute) obj).getValue();
1051
        final List<String> thisNames = simplePath.selectValues(getChild("body"));
1049
            }
-
 
1050
        });
-
 
1051
 
1052
 
1052
        final Iterator iter = elem.getChildren().iterator();
1053
        final Iterator iter = elem.getChildren().iterator();
1053
        while (iter.hasNext()) {
1054
        while (iter.hasNext()) {
1054
            final Element decl = (Element) iter.next();
1055
            final Element decl = (Element) iter.next();
1055
            if (thisNames.contains(decl.getAttributeValue("name", getVersion().getTEXT()))) {
1056
            if (thisNames.contains(decl.getAttributeValue("name", getVersion().getTEXT()))) {
Line 1200... Line 1201...
1200
    private List<String> mergeUnique(ODXMLDocument doc, String topElem, String elemToMerge, ElementTransformer addTransf) throws JDOMException {
1201
    private List<String> mergeUnique(ODXMLDocument doc, String topElem, String elemToMerge, ElementTransformer addTransf) throws JDOMException {
1201
        return this.mergeUnique(doc, topElem, elemToMerge, "style:name", addTransf);
1202
        return this.mergeUnique(doc, topElem, elemToMerge, "style:name", addTransf);
1202
    }
1203
    }
1203
 
1204
 
1204
    private List<String> mergeUnique(ODXMLDocument doc, String topElem, String elemToMerge, String attrFQName, ElementTransformer addTransf) throws JDOMException {
1205
    private List<String> mergeUnique(ODXMLDocument doc, String topElem, String elemToMerge, String attrFQName, ElementTransformer addTransf) throws JDOMException {
-
 
1206
        final Element other = doc.getChild(topElem);
-
 
1207
        if (other == null)
-
 
1208
            return Collections.emptyList();
1205
        List<String> added = new ArrayList<String>();
1209
        List<String> added = new ArrayList<String>();
1206
        Element thisParent = this.getChild(topElem, true);
1210
        Element thisParent = this.getChild(topElem, true);
1207
 
1211
 
1208
        XPath xp = this.getXPath("./" + elemToMerge + "/@" + attrFQName);
1212
        final SimpleXMLPath<Attribute> simplePath = SimpleXMLPath.create(createElementStepFromQualifiedName(elemToMerge), createAttributeStepFromQualifiedName(attrFQName));
1209
 
1213
 
1210
        // les styles de ce document
1214
        // les styles de ce document
1211
        List thisElemNames = xp.selectNodes(thisParent);
1215
        final List<String> thisElemNames = simplePath.selectValues(thisParent);
1212
        // on transforme la liste d'attributs en liste de String
-
 
1213
        org.apache.commons.collections.CollectionUtils.transform(thisElemNames, new Transformer() {
-
 
1214
            public Object transform(Object obj) {
-
 
1215
                return ((Attribute) obj).getValue();
-
 
1216
            }
-
 
1217
        });
-
 
1218
 
1216
 
1219
        // pour chaque style de l'autre document
1217
        // pour chaque style de l'autre document
1220
        Iterator otherElemNames = xp.selectNodes(doc.getChild(topElem)).iterator();
-
 
1221
        while (otherElemNames.hasNext()) {
-
 
1222
            Attribute attr = (Attribute) otherElemNames.next();
1218
        for (final Attribute attr : simplePath.selectNodes(other)) {
1223
            // on l'ajoute si non déjà dedans
1219
            // on l'ajoute si non déjà dedans
1224
            if (!thisElemNames.contains(attr.getValue())) {
1220
            if (!thisElemNames.contains(attr.getValue())) {
1225
                thisParent.addContent(addTransf.transform((Element) attr.getParent().clone()));
1221
                thisParent.addContent(addTransf.transform((Element) attr.getParent().clone()));
1226
                added.add(attr.getValue());
1222
                added.add(attr.getValue());
1227
            }
1223
            }
Line 1407... Line 1403...
1407
                final Element hrefParent = hrefAttr.getParent();
1403
                final Element hrefParent = hrefAttr.getParent();
1408
                if (!BINARY_DATA_PARENTS.contains(hrefParent.getQualifiedName()))
1404
                if (!BINARY_DATA_PARENTS.contains(hrefParent.getQualifiedName()))
1409
                    throw new IllegalStateException("Cannot convert to binary data element : " + hrefParent);
1405
                    throw new IllegalStateException("Cannot convert to binary data element : " + hrefParent);
1410
                final Element binaryData = new Element("binary-data", getPackage().getVersion().getOFFICE());
1406
                final Element binaryData = new Element("binary-data", getPackage().getVersion().getOFFICE());
1411
 
1407
 
1412
                binaryData.setText(Base64.encodeBytes(getPackage().getBinaryFile(href)));
1408
                binaryData.setText(Base64.encodeBytesBreakLines(getPackage().getBinaryFile(href)));
1413
                hrefParent.addContent(binaryData);
1409
                hrefParent.addContent(binaryData);
1414
                // If this element is present, an xlink:href attribute in its parent element
1410
                // If this element is present, an xlink:href attribute in its parent element
1415
                // shall be ignored. But LO doesn't respect that
1411
                // shall be ignored. But LO doesn't respect that
1416
                hrefAttr.detach();
1412
                hrefAttr.detach();
1417
            }
1413
            }
1418
        }
1414
        }
1419
 
1415
 
1420
        final File f = this.getPackage().getContentType().addExt(fNoExt, true);
1416
        final File f = this.getPackage().getContentType().addExt(fNoExt, true);
-
 
1417
        try (final FileOutputStream outs = new FileOutputStream(f)) {
-
 
1418
            // Use OutputStream parameter so that the encoding used for the Writer matches the
-
 
1419
            // XML declaration.
1421
        FileUtils.write(ODPackage.createOutputter().outputString(doc), f);
1420
            ODPackage.createOutputter().output(doc, outs);
-
 
1421
        }
1422
        return f;
1422
        return f;
1423
    }
1423
    }
1424
 
1424
 
1425
    private Element getPageBreak() {
1425
    private Element getPageBreak() {
1426
        if (this.pageBreak == null) {
1426
        if (this.pageBreak == null) {