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;
@ -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;
@ -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();

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,6 +112,7 @@ 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);

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;
@ -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;
@ -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);
}
}
/**

View file

@ -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;
}