这是我们代码中的原始类,没有太多解释(但这里已经很晚了!)。这会解析元素:
import com.sun.syndication.io.ModuleGenerator;
import com.sun.syndication.io.impl.DateParser;
import com.sun.syndication.feed.module.Module;
import java.util.Collections;
import java.util.Set;
import java.util.HashSet;
import org.jdom.Element;
import org.jdom.Namespace;
/**
* Generates mp content in atom.
*/
public class ModuleGenerator implements ModuleGenerator {
private static final Namespace NAMESPACE = Namespace.getNamespace("mp", Module.URI);
private static final Set<Namespace> NAMESPACES;
static {
Set<Namespace> namespaces = new HashSet<Namespace>();
namespaces.add(NAMESPACE);
NAMESPACES = Collections.unmodifiableSet(namespaces);
}
public String getNamespaceUri() {
return Module.URI;
}
public Set<Namespace> getNamespaces() {
return NAMESPACES;
}
public void generate(Module module, Element element) {
Module myModule = (Module) module;
if (myModule.getStartDate() != null) {
Element myElement = new Element("startDate", NAMESPACE);
myElement.setText(DateParser.formatW3CDateTime(myModule.getStartDate()));
element.addContent(myElement);
}
if (myModule.getEndDate() != null) {
Element myElement = new Element("endDate", NAMESPACE);
myElement.setText(DateParser.formatW3CDateTime(myModule.getEndDate()));
element.addContent(myElement);
}
}
}
import com.sun.syndication.feed.module.Module;
import java.util.Date;
/**
* Module for mp atom extension.
*/
public interface Module extends Module {
public static final String URI = "http://www.mp.com/namespace";
public Date getStartDate();
public void setStartDate(Date date);
public Date getEndDate();
public void setEndDate(Date date);
}
public class ModuleImpl extends ModuleImpl implements Module {
private Date startDate;
private Date endDate;
public ModuleImpl() {
super(Module.class, Module.URI);
}
@Override
public Class getInterface() {
return Module.class;
}
@Override
public void copyFrom(Object obj) {
Module module = (Module) obj;
setStartDate(module.getStartDate());
setEndDate(module.getEndDate());
}
@Override
public Date getStartDate() {
return startDate;
}
@Override
public void setStartDate(Date date) {
startDate = date;
}
@Override
public Date getEndDate() {
return endDate;
}
@Override
public void setEndDate(Date date) {
endDate = date;
}
@Override
public String toString() {
return "ModuleImpl{" +
"startDate=" + startDate +
", endDate=" + endDate +
'}';
}
}
package com.mp.core.iomanagement.contentanalyzers.modules;
import java.util.Date;
import org.jdom.Element;
import org.jdom.Namespace;
import com.sun.syndication.feed.module.Module;
import com.sun.syndication.io.ModuleParser;
import com.sun.syndication.io.impl.DateParser;
/**
* Parses mp content from atom.
*/
public class ModuleParser implements ModuleParser {
public String getNamespaceUri() {
return Module.URI;
}
public Module parse(Element element) {
Namespace myNamespace = Namespace.getNamespace(Module.URI);
Module module = null;
Date start = null;
Date end = null;
final Element startChild = element.getChild("startDate", myNamespace);
if (startChild!=null) {
start = DateParser.parseDate(startChild.getText());
}
final Element endChild = element.getChild("endDate", myNamespace);
if (endChild!=null) {
end = DateParser.parseDate(endChild.getText());
}
if (start!=null || end!=null) {
module = new ModuleImpl();
module.setStartDate(start);
module.setEndDate(end);
}
return module;
}
}
罗马属性:
atom_1.0.item.ModuleParser.classes=\
com.mp.core.iomanagement.contentanalyzers.modules.ModuleParser
atom_1.0.item.ModuleGenerator.classes=\
com.mp.core.iomanagement.contentanalyzers.modules.ModuleGenerator