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 
23 package com.sun.star.report.pentaho.parser;
24 
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Properties;
29 
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 
33 import org.jfree.layouting.namespace.NamespaceDefinition;
34 import org.jfree.layouting.namespace.Namespaces;
35 import org.jfree.report.JFreeReportBoot;
36 
37 import org.pentaho.reporting.libraries.base.config.DefaultConfiguration;
38 import org.pentaho.reporting.libraries.resourceloader.Resource;
39 import org.pentaho.reporting.libraries.resourceloader.ResourceException;
40 import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
41 
42 
43 public class OfficeParserUtil
44 {
45 
46     private static final Log LOGGER = LogFactory.getLog(OfficeParserUtil.class);
47     private static OfficeParserUtil instance;
48     private static final String NAMESPACES_PREFIX = "namespaces.";
49 
getInstance()50     public static synchronized OfficeParserUtil getInstance()
51     {
52         if (instance == null)
53         {
54             instance = new OfficeParserUtil();
55         }
56         return instance;
57     }
58     private final DefaultConfiguration props;
59     private final NamespaceDefinition[] namespaces;
60 
OfficeParserUtil()61     private OfficeParserUtil()
62     {
63         props = new DefaultConfiguration();
64 
65         final ResourceManager resourceManager = new ResourceManager();
66         resourceManager.registerDefaults();
67         try
68         {
69             final Resource res = resourceManager.createDirectly("res://com/sun/star/report/pentaho/parser/selectors.properties", Properties.class);
70             final Properties resProps = (Properties) res.getResource();
71             props.putAll(resProps);
72         }
73         catch (ResourceException e)
74         {
75             LOGGER.warn("Unable to load mapping rules. Parsing services may not be available.", e);
76         }
77 
78         namespaces = Namespaces.createFromConfig(JFreeReportBoot.getInstance().getGlobalConfig(),
79                 "org.jfree.report.namespaces.", resourceManager);
80     }
81 
getNamespaceDeclaration(final String uri)82     public NamespaceDefinition getNamespaceDeclaration(final String uri)
83     {
84         if (uri == null)
85         {
86             throw new NullPointerException("URI must not be null");
87         }
88 
89         for (int i = 0; i < namespaces.length; i++)
90         {
91             final NamespaceDefinition definition = namespaces[i];
92             if (uri.equals(definition.getURI()))
93             {
94                 return definition;
95             }
96         }
97         return null;
98     }
99 
getGenericFont(final String officeFont)100     public String getGenericFont(final String officeFont)
101     {
102         return props.getProperty("font-family." + officeFont.toLowerCase(), officeFont);
103     }
104 
getNamespaceURI(final String namespacePrefix)105     public String getNamespaceURI(final String namespacePrefix)
106     {
107         return props.getProperty(NAMESPACES_PREFIX + namespacePrefix);
108     }
109 
getNamespaceForStyleFamily(final String styleFamily)110     public String getNamespaceForStyleFamily(final String styleFamily)
111     {
112         return props.getProperty("style-family." + styleFamily);
113     }
114 
getNamespaces()115     public Map getNamespaces()
116     {
117         final Map map = new HashMap();
118         final Iterator keys = props.findPropertyKeys(NAMESPACES_PREFIX);
119         while (keys.hasNext())
120         {
121             final String key = (String) keys.next();
122             final String value = props.getConfigProperty(key);
123             map.put(key.substring(NAMESPACES_PREFIX.length()), value);
124         }
125         return map;
126     }
127 
getNamespacePrefix(final String namespaceURI)128     public String getNamespacePrefix(final String namespaceURI)
129     {
130         final Iterator keys = props.findPropertyKeys(NAMESPACES_PREFIX);
131         while (keys.hasNext())
132         {
133             final String key = (String) keys.next();
134             final String value = props.getConfigProperty(key);
135             if (namespaceURI.equals(value))
136             {
137                 return key.substring(NAMESPACES_PREFIX.length());
138             }
139         }
140         return null;
141     }
142 
parseStyleAttrDefinition(final String key, final String prefix, final String tagname)143     public AttributeSpecification parseStyleAttrDefinition(final String key, final String prefix, final String tagname)
144     {
145         final String configPrefix = "attr." + prefix + "." + tagname + ".";
146         final String configSuffix = key.substring(configPrefix.length());
147         final int dotPosition = configSuffix.indexOf('.');
148         if (dotPosition == -1)
149         {
150             return null;
151         }
152         final String namespaceUri = getNamespaceURI(configSuffix.substring(0, dotPosition));
153         final String attrName = configSuffix.substring(dotPosition + 1);
154         final String value = props.getProperty(key);
155         return new AttributeSpecification(namespaceUri, attrName, value);
156     }
157 
findStylesForElement(final String prefix, final String tagname)158     public Iterator findStylesForElement(final String prefix,
159             final String tagname)
160     {
161         final String configPrefix = "attr." + prefix + "." + tagname + ".";
162         return props.findPropertyKeys(configPrefix);
163     }
164 
isValidStyleElement(final String uri, final String tagName)165     public boolean isValidStyleElement(final String uri, final String tagName)
166     {
167         final String prefix = getNamespacePrefix(uri);
168         if (prefix == null)
169         {
170             return false;
171         }
172 
173         final Iterator stylesForElement = findStylesForElement(prefix, tagName);
174         return stylesForElement.hasNext();
175     }
176 
getSelectorPattern()177     public String getSelectorPattern()
178     {
179         return props.getConfigProperty("style-selector.pattern");
180     }
181 
main(final String[] args)182     public static void main(final String[] args)
183     {
184         JFreeReportBoot.getInstance().start();
185         System.out.print(OfficeParserUtil.getInstance().getNamespaces());
186     }
187 }
188