OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 73 | Rev 180 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 73 Rev 83
Line 24... Line 24...
24
 
24
 
25
import java.util.ArrayList;
25
import java.util.ArrayList;
26
import java.util.Collections;
26
import java.util.Collections;
27
import java.util.HashMap;
27
import java.util.HashMap;
28
import java.util.Iterator;
28
import java.util.Iterator;
-
 
29
import java.util.LinkedHashMap;
29
import java.util.List;
30
import java.util.List;
30
import java.util.Map;
31
import java.util.Map;
31
import java.util.Set;
32
import java.util.Set;
32
 
33
 
33
import org.jdom.Content;
34
import org.jdom.Content;
Line 68... Line 69...
68
        res.add(new Element("master-styles", ns));
69
        res.add(new Element("master-styles", ns));
69
        res.add(new Element("body", ns));
70
        res.add(new Element("body", ns));
70
        return res;
71
        return res;
71
    }
72
    }
72
 
73
 
-
 
74
    static private final int ITERATIONS_WARNING_COUNT = 2000;
73
    // namespaces for the name attributes
75
    // namespaces for the name attributes
74
    static private final Map<String, String> namePrefixes;
76
    static private final Map<String, String> namePrefixes;
75
    static {
77
    static {
76
        namePrefixes = new HashMap<String, String>();
78
        namePrefixes = new HashMap<String, String>();
77
        namePrefixes.put("table:table", "table");
79
        namePrefixes.put("table:table", "table");
Line 99... Line 101...
99
    }
101
    }
100
 
102
 
101
    private final Document content;
103
    private final Document content;
102
    private final XMLFormatVersion version;
104
    private final XMLFormatVersion version;
103
    private final ChildCreator childCreator;
105
    private final ChildCreator childCreator;
-
 
106
    private final Map<String, Integer> styleNamesLast;
104
 
107
 
105
    // before making it public, assure that content is really of version "version"
108
    // before making it public, assure that content is really of version "version"
106
    // eg by checking some namespace
109
    // eg by checking some namespace
107
    protected ODXMLDocument(final Document content, final XMLFormatVersion version) {
110
    protected ODXMLDocument(final Document content, final XMLFormatVersion version) {
108
        if (content == null)
111
        if (content == null)
109
            throw new NullPointerException("null document");
112
            throw new NullPointerException("null document");
110
        this.content = content;
113
        this.content = content;
111
        this.version = version;
114
        this.version = version;
112
        this.childCreator = new ChildCreator(this.content.getRootElement(), ELEMS_ORDER.get(this.getVersion()));
115
        this.childCreator = new ChildCreator(this.content.getRootElement(), ELEMS_ORDER.get(this.getVersion()));
-
 
116
        this.styleNamesLast = new LinkedHashMap<String, Integer>(4, 0.75f, true) {
-
 
117
            @Override
-
 
118
            protected boolean removeEldestEntry(java.util.Map.Entry<String, Integer> eldest) {
-
 
119
                return this.size() > 15;
-
 
120
            }
-
 
121
        };
113
    }
122
    }
114
 
123
 
115
    public ODXMLDocument(Document content) {
124
    public ODXMLDocument(Document content) {
116
        this(content, XMLFormatVersion.get(content.getRootElement()));
125
        this(content, XMLFormatVersion.get(content.getRootElement()));
117
    }
126
    }
Line 290... Line 299...
290
     * @param baseName the base name, e.g. "myColStyle".
299
     * @param baseName the base name, e.g. "myColStyle".
291
     * @return an unused name, e.g. "myColStyle12".
300
     * @return an unused name, e.g. "myColStyle12".
292
     * @see Style#getStyleDesc(Class, XMLVersion)
301
     * @see Style#getStyleDesc(Class, XMLVersion)
293
     */
302
     */
294
    public final String findUnusedName(final StyleDesc<?> desc, final String baseName) {
303
    public final String findUnusedName(final StyleDesc<?> desc, final String baseName) {
-
 
304
        final Integer lastI = this.styleNamesLast.get(baseName);
-
 
305
        final int offset = lastI == null ? 0 : lastI.intValue();
295
        for (int i = 0; i < 1000; i++) {
306
        int iterationCount = 0;
-
 
307
        int i = offset;
-
 
308
        String res = null;
-
 
309
        while (res == null) {
296
            final String name = baseName + i;
310
            final String name = baseName + i;
297
            final Element elem = this.getStyle(desc, name);
311
            final Element elem = this.getStyle(desc, name);
-
 
312
            iterationCount++;
-
 
313
            // don't increment i if we succeed since we don't know if the found name will indeed be
-
 
314
            // used
298
            if (elem == null)
315
            if (elem == null) {
299
                return name;
316
                res = name;
-
 
317
            } else {
-
 
318
                i++;
-
 
319
                // warn early before it takes too long
-
 
320
                if (iterationCount == ITERATIONS_WARNING_COUNT) {
-
 
321
                    Log.get().warning("After " + iterationCount + " iterations, no unused name found for " + baseName + " (" + desc + ")");
300
        }
322
                }
-
 
323
            }
-
 
324
        }
-
 
325
        this.styleNamesLast.put(baseName, i);
-
 
326
        if (iterationCount >= ITERATIONS_WARNING_COUNT) {
-
 
327
            // MAYBE trigger an optimize pass that merges equal styles.
-
 
328
            Log.get().warning(iterationCount + " iterations were needed to find an unused name for " + baseName + " (" + desc + ")");
-
 
329
        }
301
        return null;
330
        return res;
-
 
331
    }
-
 
332
 
-
 
333
    // Useful if many styles are removed (to avoid "ce12345")
-
 
334
    final void clearStyleNameCache() {
-
 
335
        this.styleNamesLast.clear();
302
    }
336
    }
303
 
337
 
304
    public final void addAutoStyle(final Element styleElem) {
338
    public final void addAutoStyle(final Element styleElem) {
305
        this.getChild("automatic-styles", true).addContent(styleElem);
339
        this.getChild("automatic-styles", true).addContent(styleElem);
306
    }
340
    }