Commit bda144a3 authored by Liang Ding's avatar Liang Ding

🐛 #12448 修复 Sitemap

parent d1532c4e
...@@ -17,21 +17,10 @@ package org.b3log.solo.model.feed.atom; ...@@ -17,21 +17,10 @@ package org.b3log.solo.model.feed.atom;
import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.time.DateFormatUtils; import org.apache.commons.lang.time.DateFormatUtils;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger; import org.b3log.latke.logging.Logger;
import org.b3log.solo.processor.FeedProcessor; import org.b3log.solo.processor.FeedProcessor;
import org.w3c.dom.Document; import org.b3log.solo.util.XMLs;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
...@@ -181,32 +170,6 @@ public final class Feed { ...@@ -181,32 +170,6 @@ public final class Feed {
*/ */
private List<Entry> entries = new ArrayList<>(); private List<Entry> entries = new ArrayList<>();
/**
* Returns pretty print of the specified xml string.
*
* @param xml the specified xml string
* @return the pretty print of the specified xml string
* @throws Exception exception
*/
private static String format(final String xml) {
try {
final DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final Document doc = db.parse(new InputSource(new StringReader(xml)));
final Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
final StreamResult result = new StreamResult(new StringWriter());
final DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
return result.getWriter().toString();
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "format pretty XML failed", e);
return xml;
}
}
/** /**
* Gets the id. * Gets the id.
* *
...@@ -362,6 +325,6 @@ public final class Feed { ...@@ -362,6 +325,6 @@ public final class Feed {
stringBuilder.append(END_FEED_ELEMENT); stringBuilder.append(END_FEED_ELEMENT);
return format(stringBuilder.toString()); return XMLs.format(stringBuilder.toString());
} }
} }
...@@ -17,21 +17,10 @@ package org.b3log.solo.model.feed.rss; ...@@ -17,21 +17,10 @@ package org.b3log.solo.model.feed.rss;
import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.time.DateFormatUtils; import org.apache.commons.lang.time.DateFormatUtils;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger; import org.b3log.latke.logging.Logger;
import org.b3log.solo.processor.FeedProcessor; import org.b3log.solo.processor.FeedProcessor;
import org.w3c.dom.Document; import org.b3log.solo.util.XMLs;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
...@@ -182,32 +171,6 @@ public final class Channel { ...@@ -182,32 +171,6 @@ public final class Channel {
*/ */
private List<Item> items = new ArrayList<>(); private List<Item> items = new ArrayList<>();
/**
* Returns pretty print of the specified xml string.
*
* @param xml the specified xml string
* @return the pretty print of the specified xml string
* @throws Exception exception
*/
private static String format(final String xml) {
try {
final DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final Document doc = db.parse(new InputSource(new StringReader(xml)));
final Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
final StreamResult result = new StreamResult(new StringWriter());
final DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
return result.getWriter().toString();
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "format pretty XML failed", e);
return xml;
}
}
/** /**
* Gets the atom link. * Gets the atom link.
* *
...@@ -364,7 +327,7 @@ public final class Channel { ...@@ -364,7 +327,7 @@ public final class Channel {
stringBuilder.append(END_DESCRIPTION_ELEMENT); stringBuilder.append(END_DESCRIPTION_ELEMENT);
stringBuilder.append(START_GENERATOR_ELEMENT); stringBuilder.append(START_GENERATOR_ELEMENT);
stringBuilder.append(generator); stringBuilder.append(StringEscapeUtils.escapeXml(generator));
stringBuilder.append(END_GENERATOR_ELEMENT); stringBuilder.append(END_GENERATOR_ELEMENT);
stringBuilder.append(START_LAST_BUILD_DATE_ELEMENT); stringBuilder.append(START_LAST_BUILD_DATE_ELEMENT);
...@@ -372,7 +335,7 @@ public final class Channel { ...@@ -372,7 +335,7 @@ public final class Channel {
stringBuilder.append(END_LAST_BUILD_DATE_ELEMENT); stringBuilder.append(END_LAST_BUILD_DATE_ELEMENT);
stringBuilder.append(START_LANGUAGE_ELEMENT); stringBuilder.append(START_LANGUAGE_ELEMENT);
stringBuilder.append(language); stringBuilder.append(StringEscapeUtils.escapeXml(language));
stringBuilder.append(END_LANGUAGE_ELEMENT); stringBuilder.append(END_LANGUAGE_ELEMENT);
for (final Item item : items) { for (final Item item : items) {
...@@ -381,6 +344,6 @@ public final class Channel { ...@@ -381,6 +344,6 @@ public final class Channel {
stringBuilder.append(END); stringBuilder.append(END);
return format(stringBuilder.toString()); return XMLs.format(stringBuilder.toString());
} }
} }
...@@ -261,7 +261,7 @@ public final class Item { ...@@ -261,7 +261,7 @@ public final class Item {
stringBuilder.append(END_DESCRIPTION_ELEMENT); stringBuilder.append(END_DESCRIPTION_ELEMENT);
stringBuilder.append(START_AUTHOR_ELEMENT); stringBuilder.append(START_AUTHOR_ELEMENT);
stringBuilder.append(author); stringBuilder.append(StringEscapeUtils.escapeXml(author));
stringBuilder.append(END_AUTHOR_ELEMENT); stringBuilder.append(END_AUTHOR_ELEMENT);
stringBuilder.append(START_GUID_ELEMENT); stringBuilder.append(START_GUID_ELEMENT);
......
...@@ -15,15 +15,14 @@ ...@@ -15,15 +15,14 @@
*/ */
package org.b3log.solo.model.sitemap; package org.b3log.solo.model.sitemap;
import org.apache.commons.lang.StringEscapeUtils;
import org.b3log.latke.util.Strings; import org.b3log.latke.util.Strings;
/** /**
* Sitemap URL. * Sitemap URL.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.0.0, Sep 22, 2011 * @version 1.1.1.0, May 18, 2018
* @since 0.3.1 * @since 0.3.1
*/ */
public final class URL { public final class URL {
...@@ -70,7 +69,7 @@ public final class URL { ...@@ -70,7 +69,7 @@ public final class URL {
/** /**
* Gets the last modified. * Gets the last modified.
* *
* @return last modified * @return last modified
*/ */
public String getLastMod() { public String getLastMod() {
...@@ -79,7 +78,7 @@ public final class URL { ...@@ -79,7 +78,7 @@ public final class URL {
/** /**
* Sets the last modified with the specified last modified. * Sets the last modified with the specified last modified.
* *
* @param lastMod the specified modified * @param lastMod the specified modified
*/ */
public void setLastMod(final String lastMod) { public void setLastMod(final String lastMod) {
...@@ -88,7 +87,7 @@ public final class URL { ...@@ -88,7 +87,7 @@ public final class URL {
/** /**
* Gets the loc. * Gets the loc.
* *
* @return loc * @return loc
*/ */
public String getLoc() { public String getLoc() {
...@@ -97,7 +96,7 @@ public final class URL { ...@@ -97,7 +96,7 @@ public final class URL {
/** /**
* Sets the loc with the specified loc. * Sets the loc with the specified loc.
* *
* @param loc the specified loc * @param loc the specified loc
*/ */
public void setLoc(final String loc) { public void setLoc(final String loc) {
...@@ -111,7 +110,7 @@ public final class URL { ...@@ -111,7 +110,7 @@ public final class URL {
stringBuilder.append(START_URL_ELEMENT); stringBuilder.append(START_URL_ELEMENT);
stringBuilder.append(START_LOC_ELEMENT); stringBuilder.append(START_LOC_ELEMENT);
stringBuilder.append(loc); stringBuilder.append(StringEscapeUtils.escapeXml(loc));
stringBuilder.append(END_LOC_ELEMENT); stringBuilder.append(END_LOC_ELEMENT);
if (!Strings.isEmptyOrNull(lastMod)) { if (!Strings.isEmptyOrNull(lastMod)) {
......
...@@ -42,6 +42,7 @@ import org.b3log.solo.repository.PageRepository; ...@@ -42,6 +42,7 @@ import org.b3log.solo.repository.PageRepository;
import org.b3log.solo.repository.TagRepository; import org.b3log.solo.repository.TagRepository;
import org.b3log.solo.repository.impl.ArticleRepositoryImpl; import org.b3log.solo.repository.impl.ArticleRepositoryImpl;
import org.b3log.solo.service.PreferenceQueryService; import org.b3log.solo.service.PreferenceQueryService;
import org.b3log.solo.util.XMLs;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -54,7 +55,7 @@ import java.util.Date; ...@@ -54,7 +55,7 @@ import java.util.Date;
* Site map (sitemap) processor. * Site map (sitemap) processor.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.1.0, Mar 10, 2018 * @version 1.0.2.0, Mar 18, 2018
* @since 0.3.1 * @since 0.3.1
*/ */
@RequestProcessor @RequestProcessor
...@@ -115,9 +116,10 @@ public class SitemapProcessor { ...@@ -115,9 +116,10 @@ public class SitemapProcessor {
addArchives(sitemap); addArchives(sitemap);
LOGGER.log(Level.INFO, "Generating sitemap...."); LOGGER.log(Level.INFO, "Generating sitemap....");
final String content = sitemap.toString(); String content = sitemap.toString();
content = XMLs.format(content);
LOGGER.log(Level.INFO, "Generated sitemap"); LOGGER.log(Level.INFO, "Generated sitemap");
LOGGER.info(content);
renderer.setContent(content); renderer.setContent(content);
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.log(Level.ERROR, "Get blog article feed error", e); LOGGER.log(Level.ERROR, "Get blog article feed error", e);
......
/*
* Copyright (c) 2010-2018, b3log.org & hacpai.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.b3log.solo.util;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringReader;
import java.io.StringWriter;
/**
* XML utilities.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.6, May 18, 2018
* @since 2.9.1
*/
public final class XMLs {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(XMLs.class);
/**
* Returns pretty print of the specified xml string.
*
* @param xml the specified xml string
* @return the pretty print of the specified xml string
*/
public static String format(final String xml) {
try {
final DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final Document doc = db.parse(new InputSource(new StringReader(xml)));
final Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
final StreamResult result = new StreamResult(new StringWriter());
final DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
return result.getWriter().toString();
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "format pretty XML failed", e);
return xml;
}
}
/**
* Private constructor.
*/
private XMLs() {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment