summaryrefslogtreecommitdiff
path: root/ooxml/source/framework/JavaPartManager/src/org/apache/openoffice/ooxml/framework/part/RelatedParts.java
blob: 22cd218c9ab8564c5b42f7953b4b31235a6b89a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package org.apache.openoffice.ooxml.framework.part;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;

import javax.xml.stream.Location;

import org.apache.openoffice.ooxml.framework.part.parser.ParserFactory;
import org.apache.openoffice.ooxml.parser.ElementContext;
import org.apache.openoffice.ooxml.parser.Parser;
import org.apache.openoffice.ooxml.parser.action.ActionTrigger;
import org.apache.openoffice.ooxml.parser.action.IAction;

public class RelatedParts
{
    RelatedParts (
        final PartName aPartName,
        final PartManager aPartManager)
    {
        maIdToTargetMap = new HashMap<>();
        maTypeToTargetsMap = new HashMap<>();

        final InputStream aStream = aPartManager.getStreamForPartName(aPartName.getRelationshipsPartName());
        if (aStream != null)
        {
            final Parser aParser = ParserFactory.getParser(
                ContentType.Relationships,
                aStream,
                null);
            aParser.GetActionManager().AddElementStartAction(
                "A_CT_Relationship",
                new IAction()
                {
                    @Override public void Run (
                        final ActionTrigger eTrigger,
                        final ElementContext aContext,
                        final String sText,
                        final Location aStartLocation,
                        final Location aEndLocation)
                    {
                        final String sId = aContext.GetAttributes().GetRawAttributeValue("A_Id");
                        final String sType = aContext.GetAttributes().GetRawAttributeValue("A_Type");
                        final String sTarget = aContext.GetAttributes().GetRawAttributeValue("A_Target");
                        String sTargetMode = aContext.GetAttributes().GetRawAttributeValue("A_TargetMode");
                        if (sTargetMode == null)
                            sTargetMode = "Internal";

                        AddRelationship(
                            sId,
                            RelationshipType.CreateFromString(sType),
                            new PartName(sTarget, aPartName, sTargetMode));
                    }
                }
            );
            aParser.Parse();
        }
    }




    private void AddRelationship (
        final String sId,
        final RelationshipType eType,
        final PartName aTarget)
    {
        maIdToTargetMap.put(sId, aTarget);

        Vector<PartName> aTargets = maTypeToTargetsMap.get(eType);
        if (aTargets == null)
        {
            aTargets = new Vector<>();
            maTypeToTargetsMap.put(eType, aTargets);
        }
        aTargets.add(aTarget);
    }




    public PartName GetTargetForId (final String sId)
    {
        return maIdToTargetMap.get(sId);
    }




    public Iterable<PartName> GetTargetsForType (final RelationshipType eType)
    {
        return maTypeToTargetsMap.get(eType);
    }



    public Iterable<PartName> getAllTargets ()
    {
        final Set<PartName> aAllNames = new TreeSet<>();
        aAllNames.addAll(maIdToTargetMap.values());
        return aAllNames;
    }




    public PartName GetSingleTargetForType (final RelationshipType eType)
    {
        if (maTypeToTargetsMap.get(eType).size() != 1)
        {
            System.out.printf("there are %d targets for relationship type %s\n",
                maTypeToTargetsMap.get(eType).size(),
                eType.toString());
            for (final PartName aName : maTypeToTargetsMap.get(eType))
            {
                System.out.printf("%s\n", aName);
            }
            assert(false);
        }
        return maTypeToTargetsMap.get(eType).firstElement();
    }




    private final Map<String,PartName> maIdToTargetMap;
    private final Map<RelationshipType, Vector<PartName>> maTypeToTargetsMap;
}