XmlResourceBundleLoader.java
001 /*
002  * Copyright 2008-2017 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package org.codehaus.griffon.runtime.util;
017 
018 import griffon.core.resources.ResourceHandler;
019 import griffon.util.AbstractMapResourceBundle;
020 import griffon.util.ResourceBundleReader;
021 import org.w3c.dom.Document;
022 import org.w3c.dom.Node;
023 import org.w3c.dom.NodeList;
024 
025 import javax.annotation.Nonnull;
026 import javax.inject.Inject;
027 import javax.inject.Named;
028 import javax.xml.parsers.DocumentBuilder;
029 import javax.xml.parsers.DocumentBuilderFactory;
030 import javax.xml.parsers.ParserConfigurationException;
031 import java.net.URL;
032 import java.util.ArrayList;
033 import java.util.Collection;
034 import java.util.LinkedHashMap;
035 import java.util.List;
036 import java.util.Map;
037 import java.util.ResourceBundle;
038 
039 import static griffon.util.GriffonNameUtils.requireNonBlank;
040 import static java.util.Objects.requireNonNull;
041 
042 /**
043  @author Andres Almiray
044  @since 2.11.0
045  */
046 @Named("xml")
047 public class XmlResourceBundleLoader extends AbstractResourceBundleLoader {
048     protected static final String XML_SUFFIX = ".xml";
049 
050     protected final ResourceBundleReader resourceBundleReader;
051 
052     @Inject
053     public XmlResourceBundleLoader(@Nonnull ResourceHandler resourceHandler,
054                                    @Nonnull ResourceBundleReader resourceBundleReader) {
055         super(resourceHandler);
056         this.resourceBundleReader = requireNonNull(resourceBundleReader, "Argument 'resourceBundleReader' must not be null");
057     }
058 
059     @Nonnull
060     @Override
061     public Collection<ResourceBundle> load(@Nonnull String name) {
062         requireNonBlank(name, ERROR_FILENAME_BLANK);
063         List<ResourceBundle> bundles = new ArrayList<>();
064         List<URL> resources = getResources(name, XML_SUFFIX);
065 
066         if (resources != null) {
067             DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
068             DocumentBuilder documentBuilder = null;
069 
070             try {
071                 documentBuilder = documentBuilderFactory.newDocumentBuilder();
072             catch (ParserConfigurationException e) {
073                 throw new IllegalStateException("Can not read " + name + XML_SUFFIX, e);
074             }
075 
076             for (URL resource : resources) {
077                 if (null == resource) { continue}
078                 try {
079                     Document document = documentBuilder.parse(resource.openStream());
080                     ResourceBundle bundle = resourceBundleReader.read(toResourceBundle(document));
081                     bundles.add(bundle);
082                 catch (Exception e) {
083                     // ignore
084                 }
085             }
086         }
087 
088         return bundles;
089     }
090 
091     @Nonnull
092     private ResourceBundle toResourceBundle(@Nonnull Document document) {
093         document.getDocumentElement().normalize();
094 
095         final Map<String, Object> map = new LinkedHashMap<>();
096         traverseNodes(map, document.getDocumentElement().getChildNodes());
097 
098         return new AbstractMapResourceBundle() {
099             @Override
100             protected void initialize(@Nonnull Map<String, Object> entries) {
101                 entries.putAll(map);
102             }
103         };
104     }
105 
106     private void traverseNodes(@Nonnull Map<String, Object> accumulator, @Nonnull NodeList nodes) {
107         for (int index = 0; index < nodes.getLength(); index++) {
108             Node item = nodes.item(index);
109             if (item.getNodeType() != Node.ELEMENT_NODE) {
110                 continue;
111             }
112 
113             String key = item.getNodeName();
114             if (item.hasChildNodes()) {
115                 if (item.getChildNodes().getLength() == 1) {
116                     accumulator.put(key, item.getTextContent().trim());
117                 else {
118                     Map<String, Object> map = new LinkedHashMap<>();
119                     traverseNodes(map, item.getChildNodes());
120                     accumulator.put(key, map);
121                 }
122             else {
123                 accumulator.put(key, "");
124             }
125         }
126     }
127 }