1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /* $Id: PDFEmbeddedFiles.java 1761021 2016-09-16 11:40:57Z ssteiner $ */
19 
20 package org.apache.fop.pdf;
21 
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.util.Map;
25 import java.util.SortedMap;
26 
27 /**
28  * Class representing an /EmbeddedFiles dictionary object (name tree).
29  */
30 public class PDFEmbeddedFiles extends PDFNameTreeNode {
31 
32     /**
33      * Create a /EmbeddedFiles dictionary.
34      */
PDFEmbeddedFiles()35     public PDFEmbeddedFiles() {
36         super();
37     }
38 
39     /** {@inheritDoc} */
writeDictionary(OutputStream out, StringBuilder textBuffer)40     protected void writeDictionary(OutputStream out, StringBuilder textBuffer) throws IOException {
41         sortNames(); //Sort the names before writing them out
42         super.writeDictionary(out, textBuffer);
43     }
44 
sortNames()45     private void sortNames() {
46         PDFArray names = getNames();
47         SortedMap map = new java.util.TreeMap();
48         int i = 0;
49         int c = names.length();
50         while (i < c) {
51             Comparable key = (Comparable)names.get(i++); //Key must be a Comparable for sorting
52             Object value = names.get(i++);
53             map.put(key, value);
54         }
55         names.clear();
56         for (Object o : map.entrySet()) {
57             Map.Entry entry = (Map.Entry) o;
58             names.add(entry.getKey());
59             names.add(entry.getValue());
60         }
61     }
62 }
63 
64