1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package org.apache.openoffice.ooxml.framework.part;
23 
24 import java.io.InputStream;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.TreeSet;
29 import java.util.Vector;
30 
31 import javax.xml.stream.Location;
32 
33 import org.apache.openoffice.ooxml.framework.part.parser.ParserFactory;
34 import org.apache.openoffice.ooxml.parser.ElementContext;
35 import org.apache.openoffice.ooxml.parser.Parser;
36 import org.apache.openoffice.ooxml.parser.action.ActionTrigger;
37 import org.apache.openoffice.ooxml.parser.action.IAction;
38 
39 public class RelatedParts
40 {
RelatedParts( final PartName aPartName, final PartManager aPartManager)41     RelatedParts (
42         final PartName aPartName,
43         final PartManager aPartManager)
44     {
45         maIdToTargetMap = new HashMap<>();
46         maTypeToTargetsMap = new HashMap<>();
47 
48         final InputStream aStream = aPartManager.getStreamForPartName(aPartName.getRelationshipsPartName());
49         if (aStream != null)
50         {
51             final Parser aParser = ParserFactory.getParser(
52                 ContentType.Relationships,
53                 aStream,
54                 null);
55             aParser.GetActionManager().AddElementStartAction(
56                 "A_CT_Relationship",
57                 new IAction()
58                 {
59                     @Override public void Run (
60                         final ActionTrigger eTrigger,
61                         final ElementContext aContext,
62                         final String sText,
63                         final Location aStartLocation,
64                         final Location aEndLocation)
65                     {
66                         final String sId = aContext.GetAttributes().GetRawAttributeValue("A_Id");
67                         final String sType = aContext.GetAttributes().GetRawAttributeValue("A_Type");
68                         final String sTarget = aContext.GetAttributes().GetRawAttributeValue("A_Target");
69                         String sTargetMode = aContext.GetAttributes().GetRawAttributeValue("A_TargetMode");
70                         if (sTargetMode == null)
71                             sTargetMode = "Internal";
72 
73                         AddRelationship(
74                             sId,
75                             RelationshipType.CreateFromString(sType),
76                             new PartName(sTarget, aPartName, sTargetMode));
77                     }
78                 }
79             );
80             aParser.Parse();
81         }
82     }
83 
84 
85 
86 
AddRelationship( final String sId, final RelationshipType eType, final PartName aTarget)87     private void AddRelationship (
88         final String sId,
89         final RelationshipType eType,
90         final PartName aTarget)
91     {
92         maIdToTargetMap.put(sId, aTarget);
93 
94         Vector<PartName> aTargets = maTypeToTargetsMap.get(eType);
95         if (aTargets == null)
96         {
97             aTargets = new Vector<>();
98             maTypeToTargetsMap.put(eType, aTargets);
99         }
100         aTargets.add(aTarget);
101     }
102 
103 
104 
105 
GetTargetForId(final String sId)106     public PartName GetTargetForId (final String sId)
107     {
108         return maIdToTargetMap.get(sId);
109     }
110 
111 
112 
113 
GetTargetsForType(final RelationshipType eType)114     public Iterable<PartName> GetTargetsForType (final RelationshipType eType)
115     {
116         return maTypeToTargetsMap.get(eType);
117     }
118 
119 
120 
getAllTargets()121     public Iterable<PartName> getAllTargets ()
122     {
123         final Set<PartName> aAllNames = new TreeSet<>();
124         aAllNames.addAll(maIdToTargetMap.values());
125         return aAllNames;
126     }
127 
128 
129 
130 
GetSingleTargetForType(final RelationshipType eType)131     public PartName GetSingleTargetForType (final RelationshipType eType)
132     {
133         if (maTypeToTargetsMap.get(eType).size() != 1)
134         {
135             System.out.printf("there are %d targets for relationship type %s\n",
136                 maTypeToTargetsMap.get(eType).size(),
137                 eType.toString());
138             for (final PartName aName : maTypeToTargetsMap.get(eType))
139             {
140                 System.out.printf("%s\n", aName);
141             }
142             assert(false);
143         }
144         return maTypeToTargetsMap.get(eType).firstElement();
145     }
146 
147 
148 
149 
150     private final Map<String,PartName> maIdToTargetMap;
151     private final Map<RelationshipType, Vector<PartName>> maTypeToTargetsMap;
152 }
153