001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatamapping.util;
016    
017    import com.liferay.portal.kernel.util.LocaleUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.xml.Attribute;
022    import com.liferay.portal.kernel.xml.Document;
023    import com.liferay.portal.kernel.xml.DocumentException;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.util.xml.XMLFormatter;
027    
028    import java.io.IOException;
029    
030    import java.util.Locale;
031    
032    /**
033     * @author Bruno Basto
034     * @author Brian Wing Shun Chan
035     */
036    public class DDMXMLImpl implements DDMXML {
037    
038            public String formatXML(Document document) throws IOException {
039                    return document.formattedString(_XML_INDENT);
040            }
041    
042            public String formatXML(String xml) throws DocumentException, IOException {
043    
044                    // This is only supposed to format your xml, however, it will also
045                    // unwantingly change © and other characters like it into their
046                    // respective readable versions
047    
048                    xml = StringUtil.replace(xml, "&#", "[$SPECIAL_CHARACTER$]");
049    
050                    try {
051                            xml = XMLFormatter.toString(xml, _XML_INDENT);
052                    }
053                    catch (org.dom4j.DocumentException de) {
054                            throw new DocumentException(de.getMessage());
055                    }
056    
057                    xml = StringUtil.replace(xml, "[$SPECIAL_CHARACTER$]", "&#");
058    
059                    return xml;
060            }
061    
062            public String updateXMLDefaultLocale(
063                            String xml, Locale contentDefaultLocale,
064                            Locale contentNewDefaultLocale)
065                    throws DocumentException, IOException {
066    
067                    if (LocaleUtil.equals(contentDefaultLocale, contentNewDefaultLocale)) {
068                            return xml;
069                    }
070    
071                    Document document = SAXReaderUtil.read(xml);
072    
073                    Element rootElement = document.getRootElement();
074    
075                    Attribute availableLocalesAttribute = rootElement.attribute(
076                            _AVAILABLE_LOCALES);
077    
078                    String contentNewDefaultLanguageId = LocaleUtil.toLanguageId(
079                            contentNewDefaultLocale);
080    
081                    String availableLocalesAttributeValue =
082                            availableLocalesAttribute.getValue();
083    
084                    if (!availableLocalesAttributeValue.contains(
085                                    contentNewDefaultLanguageId)) {
086    
087                            StringBundler sb = new StringBundler(3);
088    
089                            sb.append(availableLocalesAttribute.getValue());
090                            sb.append(StringPool.COMMA);
091                            sb.append(contentNewDefaultLanguageId);
092    
093                            availableLocalesAttribute.setValue(sb.toString());
094                    }
095    
096                    Attribute defaultLocaleAttribute = rootElement.attribute(
097                            _DEFAULT_LOCALE);
098    
099                    defaultLocaleAttribute.setValue(contentNewDefaultLanguageId);
100    
101                    fixElementsDefaultLocale(
102                            rootElement, contentDefaultLocale, contentNewDefaultLocale);
103    
104                    return document.formattedString();
105            }
106    
107            protected void fixElementsDefaultLocale(
108                    Element element, Locale contentDefaultLocale,
109                    Locale contentNewDefaultLocale) {
110    
111                    for (Element dynamicElementElement :
112                                    element.elements(_DYNAMIC_ELEMENT)) {
113    
114                            Element importMetaDataElement =
115                                    (Element)dynamicElementElement.selectSingleNode(
116                                            "meta-data[@locale='" + contentNewDefaultLocale.toString() +
117                                                    "']");
118    
119                            if (importMetaDataElement == null) {
120                                    Element metaDataElement =
121                                            (Element)dynamicElementElement.selectSingleNode(
122                                                    "meta-data[@locale='" +
123                                                            contentDefaultLocale.toString() + "']");
124    
125                                    Element copiedMetadataElement = metaDataElement.createCopy();
126    
127                                    Attribute localeAttribute = copiedMetadataElement.attribute(
128                                            _LOCALE);
129    
130                                    String contentNewDefaultLanguageId = LocaleUtil.toLanguageId(
131                                            contentNewDefaultLocale);
132    
133                                    localeAttribute.setValue(contentNewDefaultLanguageId);
134    
135                                    dynamicElementElement.add(copiedMetadataElement);
136                            }
137    
138                            fixElementsDefaultLocale(
139                                    dynamicElementElement, contentDefaultLocale,
140                                    contentNewDefaultLocale);
141                    }
142            }
143    
144            private static final String _AVAILABLE_LOCALES = "available-locales";
145    
146            private static final String _DEFAULT_LOCALE = "default-locale";
147    
148            private static final String _DYNAMIC_ELEMENT = "dynamic-element";
149    
150            private static final String _LOCALE = "locale";
151    
152            private static final String _XML_INDENT = "  ";
153    
154    }