1*1a37d047SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*1a37d047SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*1a37d047SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*1a37d047SAndrew Rist  * distributed with this work for additional information
6*1a37d047SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*1a37d047SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*1a37d047SAndrew Rist  * "License"); you may not use this file except in compliance
9*1a37d047SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*1a37d047SAndrew Rist  *
11*1a37d047SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*1a37d047SAndrew Rist  *
13*1a37d047SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*1a37d047SAndrew Rist  * software distributed under the License is distributed on an
15*1a37d047SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1a37d047SAndrew Rist  * KIND, either express or implied.  See the License for the
17*1a37d047SAndrew Rist  * specific language governing permissions and limitations
18*1a37d047SAndrew Rist  * under the License.
19*1a37d047SAndrew Rist  *
20*1a37d047SAndrew Rist  *************************************************************/
21*1a37d047SAndrew Rist 
22*1a37d047SAndrew Rist 
23cdf0e10cSrcweir package com.sun.star.report.pentaho.styles;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import java.util.HashMap;
26cdf0e10cSrcweir import java.util.Map;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import org.pentaho.reporting.libraries.resourceloader.Resource;
29cdf0e10cSrcweir import org.pentaho.reporting.libraries.resourceloader.ResourceException;
30cdf0e10cSrcweir import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
33cdf0e10cSrcweir /**
34cdf0e10cSrcweir  * The style-mapper holds all information about the OpenOffice style mapping
35cdf0e10cSrcweir  * mechanism. OpenOffice references styles by their name and context, a style
36cdf0e10cSrcweir  * has a style-family assigned. The style family is determined by the element
37cdf0e10cSrcweir  * referencing the style, and there is no easily accessible information
38cdf0e10cSrcweir  * available on that.
39cdf0e10cSrcweir  * <p/>
40cdf0e10cSrcweir  * Therefore this mapper acts as gatekeeper for this information. The style
41cdf0e10cSrcweir  * mapping information is read from an external definition file and can be
42cdf0e10cSrcweir  * maintained externally.
43cdf0e10cSrcweir  *
44cdf0e10cSrcweir  * @author Thomas Morgner
45cdf0e10cSrcweir  * @since 11.03.2007
46cdf0e10cSrcweir  */
47cdf0e10cSrcweir public class StyleMapper
48cdf0e10cSrcweir {
49cdf0e10cSrcweir 
50cdf0e10cSrcweir     private final Map backend;
51cdf0e10cSrcweir 
StyleMapper()52cdf0e10cSrcweir     public StyleMapper()
53cdf0e10cSrcweir     {
54cdf0e10cSrcweir         this.backend = new HashMap();
55cdf0e10cSrcweir     }
56cdf0e10cSrcweir 
addMapping(final StyleMappingRule rule)57cdf0e10cSrcweir     public void addMapping(final StyleMappingRule rule)
58cdf0e10cSrcweir     {
59cdf0e10cSrcweir         backend.put(rule.getKey(), rule);
60cdf0e10cSrcweir     }
61cdf0e10cSrcweir 
isListOfStyles(final String elementNamespace, final String elementTagName, final String attributeNamespace, final String attributeName)62cdf0e10cSrcweir     public boolean isListOfStyles(final String elementNamespace,
63cdf0e10cSrcweir             final String elementTagName,
64cdf0e10cSrcweir             final String attributeNamespace,
65cdf0e10cSrcweir             final String attributeName)
66cdf0e10cSrcweir     {
67cdf0e10cSrcweir         final StyleMapperKey key = new StyleMapperKey(elementNamespace, elementTagName, attributeNamespace, attributeName);
68cdf0e10cSrcweir         final StyleMappingRule rule = (StyleMappingRule) backend.get(key);
69cdf0e10cSrcweir         return rule != null && rule.isListOfValues();
70cdf0e10cSrcweir     }
71cdf0e10cSrcweir 
getStyleFamilyFor(final String elementNamespace, final String elementTagName, final String attributeNamespace, final String attributeName)72cdf0e10cSrcweir     public String getStyleFamilyFor(final String elementNamespace,
73cdf0e10cSrcweir             final String elementTagName,
74cdf0e10cSrcweir             final String attributeNamespace,
75cdf0e10cSrcweir             final String attributeName)
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         final StyleMapperKey key = new StyleMapperKey(elementNamespace, elementTagName, attributeNamespace, attributeName);
78cdf0e10cSrcweir         final StyleMappingRule rule = (StyleMappingRule) backend.get(key);
79cdf0e10cSrcweir         if (rule == null)
80cdf0e10cSrcweir         {
81cdf0e10cSrcweir             return null;
82cdf0e10cSrcweir         }
83cdf0e10cSrcweir         return rule.getFamily();
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir 
loadInstance(final ResourceManager resourceManager)86cdf0e10cSrcweir     public static StyleMapper loadInstance(final ResourceManager resourceManager)
87cdf0e10cSrcweir             throws ResourceException
88cdf0e10cSrcweir     {
89cdf0e10cSrcweir         final Resource resource = resourceManager.createDirectly("res://com/sun/star/report/pentaho/styles/stylemapper.xml", StyleMapper.class);
90cdf0e10cSrcweir         return (StyleMapper) resource.getResource();
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir }
93