Refactored some code

This commit is contained in:
Patrick Gotthard 2014-04-13 20:08:21 +02:00
parent d4b34a05cd
commit 3a31c851b6
8 changed files with 147 additions and 145 deletions

View file

@ -24,7 +24,6 @@ import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -39,9 +38,9 @@ import org.slf4j.LoggerFactory;
* It works on all read/write properties, recursively. It support all primitive types, Strings,
* Collections, Cloneable objects and multi-dimensional arrays of any of them.
* <p>
*
*
* @author Alejandro Abdelnur
*
*
*/
public class CloneableBean implements Serializable, Cloneable {
@ -72,7 +71,7 @@ public class CloneableBean implements Serializable, Cloneable {
* <p>
* To be used by classes extending CloneableBean only.
* <p>
*
*
*/
protected CloneableBean() {
obj = this;
@ -86,21 +85,21 @@ public class CloneableBean implements Serializable, Cloneable {
* <code>
* public class Foo implements Cloneable {
* private CloneableBean cloneableBean;
*
*
* public Foo() {
* cloneableBean = new CloneableBean(this);
* }
*
*
* public Object clone() throws CloneNotSupportedException {
* return cloneableBean.beanClone();
* }
*
*
* }
* </code>
* <p>
*
*
* @param obj object bean to clone.
*
*
*/
public CloneableBean(final Object obj) {
this(obj, null);
@ -115,10 +114,10 @@ public class CloneableBean implements Serializable, Cloneable {
* and SyndEntry beans have convenience properties, publishedDate, author, copyright and
* categories all of them mapped to properties in the DC Module.
* <p>
*
*
* @param obj object bean to clone.
* @param ignoreProperties properties to ignore when cloning.
*
*
*/
public CloneableBean(final Object obj, final Set<String> ignoreProperties) {
this.obj = obj;
@ -134,12 +133,12 @@ public class CloneableBean implements Serializable, Cloneable {
* <p>
* To be used by classes extending CloneableBean. Although it works also for classes using
* CloneableBean in a delegation pattern, for correctness those classes should use the
*
*
* @see #beanClone() beanClone method.
* <p>
* @return a clone of the object bean.
* @throws CloneNotSupportedException thrown if the object bean could not be cloned.
*
*
*/
@Override
public Object clone() throws CloneNotSupportedException {
@ -150,12 +149,12 @@ public class CloneableBean implements Serializable, Cloneable {
* Makes a deep bean clone of the object passed in the constructor.
* <p>
* To be used by classes using CloneableBean in a delegation pattern,
*
*
* @see #CloneableBean(Object) constructor.
*
*
* @return a clone of the object bean.
* @throws CloneNotSupportedException thrown if the object bean could not be cloned.
*
*
*/
public Object beanClone() throws CloneNotSupportedException {
@ -238,23 +237,20 @@ public class CloneableBean implements Serializable, Cloneable {
private <T> Collection<T> cloneCollection(final Collection<T> collection) throws Exception {
@SuppressWarnings("unchecked")
final Class<Collection<T>> mClass = (Class<Collection<T>>) collection.getClass();
final Collection<T> newColl = mClass.newInstance();
final Iterator<T> i = collection.iterator();
while (i.hasNext()) {
newColl.add(doClone(i.next()));
final Collection<T> newCollection = collection.getClass().newInstance();
for (final T item : collection) {
newCollection.add(doClone(item));
}
return newColl;
return newCollection;
}
private <S, T> Map<S, T> cloneMap(final Map<S, T> map) throws Exception {
private <K, V> Map<K, V> cloneMap(final Map<K, V> map) throws Exception {
@SuppressWarnings("unchecked")
final Class<Map<S, T>> mClass = (Class<Map<S, T>>) map.getClass();
final Map<S, T> newMap = mClass.newInstance();
final Iterator<Entry<S, T>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
final Map.Entry<S, T> entry = entries.next();
newMap.put(doClone(entry.getKey()), doClone(entry.getValue()));
final Map<K, V> newMap = map.getClass().newInstance();
for (final Entry<K, V> entry : map.entrySet()) {
final K clonedKey = doClone(entry.getKey());
final V clonedValue = doClone(entry.getValue());
newMap.put(clonedKey, clonedValue);
}
return newMap;
}

View file

@ -24,7 +24,6 @@ import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@ -130,7 +129,7 @@ public class CopyFromHelper {
} else { // it goes CopyFrom
if (value instanceof CopyFrom) {
final CopyFrom source = (CopyFrom) value;
CopyFrom target = (CopyFrom) createInstance(source.getInterface());
CopyFrom target = createInstance(source.getInterface());
if (target == null) {
target = (CopyFrom) value.getClass().newInstance();
}
@ -157,32 +156,40 @@ public class CopyFromHelper {
}
private <T> Collection<T> doCopyCollection(final Collection<T> collection, final Class<?> baseInterface) throws Exception {
// expecting SETs or LISTs only, going default implementation of them
final Collection<T> newColl;
// expecting a set or a list
final Collection<T> newCollection;
if (collection instanceof Set) {
newColl = new LinkedHashSet<T>();
newCollection = new LinkedHashSet<T>();
} else {
newColl = new ArrayList<T>();
newCollection = new ArrayList<T>();
}
final Iterator<T> i = collection.iterator();
while (i.hasNext()) {
newColl.add(this.<T> doCopy(i.next(), baseInterface));
for (final T item : collection) {
final T copied = doCopy(item, baseInterface);
newCollection.add(copied);
}
return newColl;
return newCollection;
}
private <S, T> Map<S, T> doCopyMap(final Map<S, T> map, final Class<?> baseInterface) throws Exception {
final Map<S, T> newMap = new HashMap<S, T>();
final Iterator<Entry<S, T>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
final Map.Entry<S, T> entry = entries.next();
newMap.put(entry.getKey(), doCopy(entry.getValue(), baseInterface));
for (final Entry<S, T> entry : map.entrySet()) {
// TODO mustn't the key be copied too?
final T copiedValue = doCopy(entry.getValue(), baseInterface);
newMap.put(entry.getKey(), copiedValue);
}
return newMap;
}
private boolean isBasicType(final Class<?> vClass) {
return BASIC_TYPES.contains(vClass);
private boolean isBasicType(final Class<?> type) {
return BASIC_TYPES.contains(type);
}
}

View file

@ -17,7 +17,6 @@
package com.sun.syndication.io.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
@ -257,18 +256,17 @@ public class Atom03Parser extends BaseWireFeedParser {
value = Base64.decode(e.getText());
} else if (mode.equals(Content.XML)) {
final XMLOutputter outputter = new XMLOutputter();
final List<org.jdom2.Content> eContent = e.getContent();
final Iterator<org.jdom2.Content> i = eContent.iterator();
while (i.hasNext()) {
final org.jdom2.Content c = i.next();
if (c instanceof Element) {
final Element eC = (Element) c;
if (eC.getNamespace().equals(getAtomNamespace())) {
((Element) c).setNamespace(Namespace.NO_NAMESPACE);
final List<org.jdom2.Content> contents = e.getContent();
for (final org.jdom2.Content content : contents) {
if (content instanceof Element) {
final Element element = (Element) content;
if (element.getNamespace().equals(getAtomNamespace())) {
element.setNamespace(Namespace.NO_NAMESPACE);
}
}
}
value = outputter.outputString(eContent);
value = outputter.outputString(contents);
}
final Content content = new Content();

View file

@ -20,7 +20,6 @@ import java.io.IOException;
import java.io.Reader;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;
@ -49,7 +48,7 @@ import com.sun.syndication.io.WireFeedOutput;
/**
* Parser for Atom 1.0
*
*
* @author Dave Johnson
*/
public class Atom10Parser extends BaseWireFeedParser {
@ -336,18 +335,16 @@ public class Atom10Parser extends BaseWireFeedParser {
if (type.equals(Content.XHTML) || type.indexOf("/xml") != -1 || type.indexOf("+xml") != -1) {
// XHTML content needs special handling
final XMLOutputter outputter = new XMLOutputter();
final List<org.jdom2.Content> eContent = e.getContent();
final Iterator<org.jdom2.Content> i = eContent.iterator();
while (i.hasNext()) {
final org.jdom2.Content c = i.next();
if (c instanceof Element) {
final Element eC = (Element) c;
if (eC.getNamespace().equals(getAtomNamespace())) {
((Element) c).setNamespace(Namespace.NO_NAMESPACE);
final List<org.jdom2.Content> contents = e.getContent();
for (final org.jdom2.Content content : contents) {
if (content instanceof Element) {
final Element element = (Element) content;
if (element.getNamespace().equals(getAtomNamespace())) {
element.setNamespace(Namespace.NO_NAMESPACE);
}
}
}
value = outputter.outputString(eContent);
value = outputter.outputString(contents);
} else {
// Everything else comes in verbatim
value = e.getText();
@ -503,7 +500,7 @@ public class Atom10Parser extends BaseWireFeedParser {
/**
* Resolve URI via base URL and parent element. Resolve URI based considering xml:base and
* baseURI.
*
*
* @param baseURI Base URI used to fetch the XML document
* @param parent Parent element from which to consider xml:base
* @param url URL to be resolved
@ -568,7 +565,7 @@ public class Atom10Parser extends BaseWireFeedParser {
/**
* Find base URI of feed considering relative URIs.
*
*
* @param root Root element of feed.
*/
private String findBaseURI(final Element root) throws MalformedURLException {
@ -589,7 +586,7 @@ public class Atom10Parser extends BaseWireFeedParser {
/**
* Return URL string of Atom link element under parent element. Link with no rel attribute is
* considered to be rel="alternate"
*
*
* @param parent Consider only children of this parent element
* @param rel Consider only links with this relationship
*/
@ -613,7 +610,7 @@ public class Atom10Parser extends BaseWireFeedParser {
/**
* Form URI by combining base with append portion and giving special consideration to append
* portions that begin with ".."
*
*
* @param base Base of URI, may end with trailing slash
* @param append String to append, may begin with slash or ".."
*/

View file

@ -2,7 +2,6 @@ package com.sun.syndication.io.impl;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
@ -40,25 +39,31 @@ public abstract class BaseWireFeedGenerator implements WireFeedGenerator {
private final Namespace[] allModuleNamespaces;
protected BaseWireFeedGenerator(final String type) {
this.type = type;
feedModuleGenerators = new ModuleGenerators(type + FEED_MODULE_GENERATORS_POSFIX_KEY, this);
itemModuleGenerators = new ModuleGenerators(type + ITEM_MODULE_GENERATORS_POSFIX_KEY, this);
personModuleGenerators = new ModuleGenerators(type + PERSON_MODULE_GENERATORS_POSFIX_KEY, this);
final Set<Namespace> allModuleNamespaces = new HashSet<Namespace>();
Iterator<Namespace> i = feedModuleGenerators.getAllNamespaces().iterator();
while (i.hasNext()) {
allModuleNamespaces.add(i.next());
for (final Namespace namespace : feedModuleGenerators.getAllNamespaces()) {
allModuleNamespaces.add(namespace);
}
i = itemModuleGenerators.getAllNamespaces().iterator();
while (i.hasNext()) {
allModuleNamespaces.add(i.next());
for (final Namespace namespace : itemModuleGenerators.getAllNamespaces()) {
allModuleNamespaces.add(namespace);
}
i = personModuleGenerators.getAllNamespaces().iterator();
while (i.hasNext()) {
allModuleNamespaces.add(i.next());
for (final Namespace namespace : personModuleGenerators.getAllNamespaces()) {
allModuleNamespaces.add(namespace);
}
this.allModuleNamespaces = new Namespace[allModuleNamespaces.size()];
allModuleNamespaces.toArray(this.allModuleNamespaces);
}
@Override
@ -84,16 +89,14 @@ public abstract class BaseWireFeedGenerator implements WireFeedGenerator {
personModuleGenerators.generateModules(modules, person);
}
protected void generateForeignMarkup(final Element e, final List<Element> foreignMarkup) {
if (foreignMarkup != null) {
final Iterator<Element> elems = foreignMarkup.iterator();
while (elems.hasNext()) {
final Element elem = elems.next();
final Parent parent = elem.getParent();
protected void generateForeignMarkup(final Element element, final List<Element> foreignElements) {
if (foreignElements != null) {
for (final Element foreignElement : foreignElements) {
final Parent parent = foreignElement.getParent();
if (parent != null) {
parent.removeContent(elem);
parent.removeContent(foreignElement);
}
e.addContent(elem);
element.addContent(foreignElement);
}
}
}
@ -109,14 +112,15 @@ public abstract class BaseWireFeedGenerator implements WireFeedGenerator {
* at generate() time, to make sure their namespace declarations are present.
*/
protected static void purgeUnusedNamespaceDeclarations(final Element root) {
final Set<String> usedPrefixes = new HashSet<String>();
collectUsedPrefixes(root, usedPrefixes);
final List<Namespace> list = root.getAdditionalNamespaces();
final List<Namespace> additionalNamespaces = new ArrayList<Namespace>();
additionalNamespaces.addAll(list); // the duplication will prevent a
// ConcurrentModificationException
// below
// ConcurrentModificationException
// below
for (int i = 0; i < additionalNamespaces.size(); i++) {
final Namespace ns = additionalNamespaces.get(i);
@ -135,7 +139,7 @@ public abstract class BaseWireFeedGenerator implements WireFeedGenerator {
final List<Element> kids = el.getChildren();
for (int i = 0; i < kids.size(); i++) {
collectUsedPrefixes(kids.get(i), collector); // recursion
// - worth it
// - worth it
}
}

View file

@ -1,7 +1,6 @@
package com.sun.syndication.io.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
@ -24,19 +23,19 @@ import com.sun.syndication.io.WireFeedParser;
public abstract class BaseWireFeedParser implements WireFeedParser {
/**
* [TYPE].feed.ModuleParser.classes= [className] ...
*
*
*/
private static final String FEED_MODULE_PARSERS_POSFIX_KEY = ".feed.ModuleParser.classes";
/**
* [TYPE].item.ModuleParser.classes= [className] ...
*
*
*/
private static final String ITEM_MODULE_PARSERS_POSFIX_KEY = ".item.ModuleParser.classes";
/**
* [TYPE].person.ModuleParser.classes= [className] ...
*
*
*/
private static final String PERSON_MODULE_PARSERS_POSFIX_KEY = ".person.ModuleParser.classes";
@ -57,11 +56,11 @@ public abstract class BaseWireFeedParser implements WireFeedParser {
/**
* Returns the type of feed the parser handles.
* <p>
*
*
* @see WireFeed for details on the format of this string.
* <p>
* @return the type of feed the parser handles.
*
*
*/
@Override
public String getType() {
@ -80,29 +79,25 @@ public abstract class BaseWireFeedParser implements WireFeedParser {
return personModuleParsers.parseModules(itemElement, locale);
}
protected List<Element> extractForeignMarkup(final Element e, final Extendable ext, final Namespace basens) {
final ArrayList<Element> foreignMarkup = new ArrayList<Element>();
final Iterator<Element> children = e.getChildren().iterator();
while (children.hasNext()) {
final Element elem = children.next();
if (
// if elemet not in the RSS namespace
!basens.equals(elem.getNamespace())
// and elem was not handled by a module
&& null == ext.getModule(elem.getNamespaceURI())) {
protected List<Element> extractForeignMarkup(final Element e, final Extendable ext, final Namespace namespace) {
// save it as foreign markup,
// but we can't detach it while we're iterating
foreignMarkup.add(elem.clone());
final ArrayList<Element> foreignElements = new ArrayList<Element>();
for (final Element element : e.getChildren()) {
if (!namespace.equals(element.getNamespace()) && ext.getModule(element.getNamespaceURI()) == null) {
// if element not in the RSS namespace and elem was not handled by a module save it
// as foreign markup but we can't detach it while we're iterating
foreignElements.add(element.clone());
}
}
// Now we can detach the foreign markup elements
final Iterator<Element> fm = foreignMarkup.iterator();
while (fm.hasNext()) {
final Element elem = fm.next();
elem.detach();
// now we can detach the foreign markup elements
for (final Element foreignElement : foreignElements) {
foreignElement.detach();
}
return foreignMarkup;
return foreignElements;
}
protected Attribute getAttribute(final Element e, final String attributeName) {

View file

@ -17,6 +17,7 @@
package com.sun.syndication.io.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
@ -31,9 +32,9 @@ import com.sun.syndication.io.WireFeedParser;
/**
* <p>
*
*
* @author Alejandro Abdelnur
*
*
*/
public abstract class PluginManager<T> {
private final String[] propertyValues;
@ -46,9 +47,9 @@ public abstract class PluginManager<T> {
/**
* Creates a PluginManager
* <p>
*
*
* @param propertyKey property key defining the plugins classes
*
*
*/
protected PluginManager(final String propertyKey) {
this(propertyKey, null, null);
@ -102,28 +103,30 @@ public abstract class PluginManager<T> {
}
pluginsMap.put(getKey(plugin), plugin);
pluginsList.add(plugin); // to preserve the order of
// definition
// in the rome.properties files
}
Iterator<T> i = pluginsMap.values().iterator();
while (i.hasNext()) {
finalPluginsList.add(i.next()); // to remove overridden plugin
// impls
// to preserve the order of definition in the rome.properties files
pluginsList.add(plugin);
}
i = pluginsList.iterator();
while (i.hasNext()) {
final Object plugin = i.next();
final Collection<T> plugins = pluginsMap.values();
for (final T plugin : plugins) {
// to remove overridden plugin impls
finalPluginsList.add(plugin);
}
final Iterator<T> iterator = pluginsList.iterator();
while (iterator.hasNext()) {
final T plugin = iterator.next();
if (!finalPluginsList.contains(plugin)) {
i.remove();
iterator.remove();
}
}
} catch (final Exception ex) {
throw new RuntimeException("could not instantiate plugin " + className, ex);
} catch (final ExceptionInInitializerError er) {
throw new RuntimeException("could not instantiate plugin " + className, er);
}
}
/**
@ -132,11 +135,11 @@ public abstract class PluginManager<T> {
* load classes (instead of Class.forName). This is designed to improve OSGi compatibility.
* Further information can be found in https://rome.dev.java.net/issues/show_bug.cgi?id=118
* <p>
*
*
* @return array containing the classes defined in the properties files.
* @throws java.lang.ClassNotFoundException thrown if one of the classes defined in the
* properties file cannot be loaded and hard failure is ON.
*
*
*/
@SuppressWarnings("unchecked")
private Class<T>[] getClasses() throws ClassNotFoundException {

View file

@ -102,7 +102,7 @@ public class RSS090Parser extends BaseWireFeedParser {
* <P>
* This implementation returns the EMTPY namespace.
* <p>
*
*
* @return returns the EMPTY namespace.
*/
protected Namespace getRSSNamespace() {
@ -115,7 +115,7 @@ public class RSS090Parser extends BaseWireFeedParser {
* <P>
* This implementation returns the EMTPY namespace.
* <p>
*
*
* @return returns the EMPTY namespace.
*/
protected Namespace getRDFNamespace() {
@ -127,7 +127,7 @@ public class RSS090Parser extends BaseWireFeedParser {
* <P>
* This implementation returns the EMTPY namespace.
* <p>
*
*
* @return returns the EMPTY namespace.
*/
protected Namespace getContentNamespace() {
@ -142,7 +142,7 @@ public class RSS090Parser extends BaseWireFeedParser {
* different RSS version may have this information in different parts of the XML tree (no
* assumptions made thanks to the specs variaty)
* <p/>
*
*
* @param rssRoot the root element of the RSS document to parse.
* @return the parsed Channel bean.
*/
@ -227,7 +227,7 @@ public class RSS090Parser extends BaseWireFeedParser {
* <p/>
* It reads title and url out of the 'image' element.
* <p/>
*
*
* @param rssRoot the root element of the RSS document to parse for image information.
* @return the parsed image bean.
*/
@ -260,7 +260,7 @@ public class RSS090Parser extends BaseWireFeedParser {
* parseItem() for each item element. The resulting RSSItem of each item element is stored in a
* list.
* <p/>
*
*
* @param rssRoot the root element of the RSS document to parse for all items information.
* @return a list with all the parsed RSSItem beans.
*/
@ -280,7 +280,7 @@ public class RSS090Parser extends BaseWireFeedParser {
* <p/>
* It reads title and link out of the 'item' element.
* <p/>
*
*
* @param rssRoot the root element of the RSS document in case it's needed for context.
* @param eItem the item element to parse.
* @return the parsed RSSItem bean.
@ -307,14 +307,16 @@ public class RSS090Parser extends BaseWireFeedParser {
// used
final Iterator<Element> iterator = foreignMarkup.iterator();
while (iterator.hasNext()) {
final Element ie = iterator.next();
if (getContentNamespace().equals(ie.getNamespace()) && ie.getName().equals("encoded")) {
final Element element = iterator.next();
if (getContentNamespace().equals(element.getNamespace()) && element.getName().equals("encoded")) {
iterator.remove();
}
}
if (foreignMarkup.size() > 0) {
if (!foreignMarkup.isEmpty()) {
item.setForeignMarkup(foreignMarkup);
}
return item;
}
@ -323,7 +325,7 @@ public class RSS090Parser extends BaseWireFeedParser {
* <p/>
* It reads title, description, name and link out of the 'textinput' or 'textInput' element.
* <p/>
*
*
* @param rssRoot the root element of the RSS document to parse for text-input information.
* @return the parsed RSSTextInput bean.
*/