Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create OPML 1.1 parser/generator/converter (#660) #661

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions rome-opml/src/main/java/com/rometools/opml/feed/opml/Cloud.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.rometools.opml.feed.opml;

import java.io.Serializable;

import com.rometools.rome.feed.impl.EqualsBean;
import com.rometools.rome.feed.impl.ToStringBean;

public class Cloud implements Cloneable, Serializable {

private static final long serialVersionUID = 1L;

private String domain;
private String port;
private String path;
private String registerProcedure;
private String protocol;

public Cloud() {

}

public final String getDomain() {
return this.domain;
}

public final void setDomain(final String domain) {
this.domain = domain;
}

public final String getPort() {
return this.port;
}

public final void setPort(final String port) {
this.port = port;
}

public final String getPath() {
return this.path;
}

public final void setPath(final String path) {
this.path = path;
}

public final String getRegisterProcedure() {
return this.registerProcedure;
}

public final void setRegisterProcedure(final String registerProcedure) {
this.registerProcedure = registerProcedure;
}

public final String getProtocol() {
return this.protocol;
}

public final void setProtocol(final String protocol) {
this.protocol = protocol;
}

@Override
public Object clone() {
final Cloud c = new Cloud();
c.setDomain(getDomain());
c.setPort(getPort());
c.setPath(getPath());
c.setRegisterProcedure(getRegisterProcedure());
c.setProtocol(getProtocol());
return c;
}

@Override
public boolean equals(final Object obj) {
return EqualsBean.beanEquals(Cloud.class, this, obj);
}

@Override
public int hashCode() {
return EqualsBean.beanHashCode(this);
}

@Override
public String toString() {
return ToStringBean.toString(Cloud.class, this);
}
}
16 changes: 16 additions & 0 deletions rome-opml/src/main/java/com/rometools/opml/feed/opml/Opml.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class Opml extends WireFeed {
private String ownerName;
private String title;
private int[] expansionState;
private Cloud cloud;

/**
* <dateCreated> is a date-time, indicating when the document was created.
Expand Down Expand Up @@ -317,4 +318,19 @@ public Integer getWindowTop() {
return windowTop;
}

/**
* &lt;cloud&gt; is a Cloud object, contains information about cloud (spec 1.1)
* @return the cloud element
*/
public Cloud getCloud() {
return this.cloud;
}

/**
* &lt;cloud&gt; is a Cloud object, contains information about cloud (spec 1.1)
* @param c the cloud element
*/
public void setCloud(final Cloud c) {
this.cloud = c;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
*
* 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 com.rometools.opml.feed.synd.impl;

import com.rometools.rome.feed.WireFeed;
import com.rometools.rome.feed.synd.SyndFeed;

public class ConverterForOPML11 extends ConverterForOPML10 {

/**
* Returns the type (version) of the real feed this converter handles.
* <p>
*
* @return the real feed type.
* @see WireFeed for details on the format of this string.
* <p>
*/
@Override
public String getType() {
return "opml_1.1";
}

/**
* Makes a deep copy/conversion of the values of a real feed into a SyndFeedImpl.
* <p>
* It assumes the given SyndFeedImpl has no properties set.
* <p>
*
* @param feed real feed to copy/convert.
* @param syndFeed the SyndFeedImpl that will contain the copied/converted values of the real feed.
*/
@Override
public void copyInto(final WireFeed feed, final SyndFeed syndFeed) {
super.copyInto(feed, syndFeed);
}

/**
* Creates real feed with a deep copy/conversion of the values of a SyndFeedImpl.
* <p>
*
* @param syndFeed SyndFeedImpl to copy/convert value from.
* @return a real feed with copied/converted values of the SyndFeedImpl.
*/
@Override
public WireFeed createRealFeed(final SyndFeed syndFeed) {
return super.createRealFeed(syndFeed);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
*
* 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 com.rometools.opml.io.impl;

import org.jdom2.Document;
import org.jdom2.Element;

import com.rometools.opml.feed.opml.Opml;
import com.rometools.rome.feed.WireFeed;
import com.rometools.rome.io.FeedException;

/**
* Generator for OPML 1.1 documents.
*
* @see <a href="http://dev.opml.org/spec2.html">http://dev.opml.org/spec2.html</a>
*/
public class OPML11Generator extends OPML10Generator {

public OPML11Generator() {
}

/**
* Returns the type of feed the generator creates.
*
* @return the type of feed the generator creates.
* @see WireFeed for details on the format of this string.
*/
@Override
public String getType() {
return "opml_1.1";
}

/**
* Creates an XML document (JDOM) for the given feed bean.
*
* @param feed the feed bean to generate the XML document from.
* @return the generated XML document (JDOM).
* @throws IllegalArgumentException thrown if the type of the given feed bean does not match with the type of the
* WireFeedGenerator.
* @throws FeedException thrown if the XML Document could not be created.
*/
@Override
public Document generate(final WireFeed feed) throws IllegalArgumentException, FeedException {
final Document document = super.generate(feed);
document.getRootElement().setAttribute("version", "1.1");
return document;
}

@Override
protected Element generateHead(final Opml opml) {

final Element headElement = super.generateHead(opml);

if (null != opml && null != opml.getCloud()) {
final Element cloudElement = new Element("cloud");
addNotNullAttribute(cloudElement, "domain", opml.getCloud().getDomain());
addNotNullAttribute(cloudElement, "port", opml.getCloud().getPort());
addNotNullAttribute(cloudElement, "path", opml.getCloud().getPath());
addNotNullAttribute(cloudElement, "registerProcedure", opml.getCloud().getRegisterProcedure());
addNotNullAttribute(cloudElement, "protocol", opml.getCloud().getProtocol());
headElement.addContent(cloudElement);
}

return headElement;

}

}
107 changes: 107 additions & 0 deletions rome-opml/src/main/java/com/rometools/opml/io/impl/OPML11Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Opml11Parser.java
*
* Created on April 19, 2023, 11:49 PM
*
* 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 com.rometools.opml.io.impl;

import java.util.Locale;

import org.jdom2.Document;
import org.jdom2.Element;

import com.rometools.opml.feed.opml.Cloud;
import com.rometools.opml.feed.opml.Opml;
import com.rometools.rome.feed.WireFeed;
import com.rometools.rome.io.FeedException;

public class OPML11Parser extends OPML10Parser {

public OPML11Parser() {
super("opml_1.1");
}

/**
* Inspects an XML Document (JDOM) to check if it can parse it.
* <p>
* It checks if the given document if the type of feeds the parser understands.
* <p>
*
* @param document XML Document (JDOM) to check if it can be parsed by this parser.
* @return <b>true</b> if the parser know how to parser this feed, <b>false</b> otherwise.
*/
@Override
public boolean isMyType(final Document document) {
final Element e = document.getRootElement();

return e.getName().equals("opml")
&& (null != e.getChild("head") &&
null != e.getChild("head").getChild("cloud") ||
null != e.getAttributeValue("version") &&
"1.1".equals(e.getAttributeValue("version")));
}

/**
* Parses an XML document (JDOM Document) into a feed bean.
* <p>
*
* @param document XML document (JDOM) to parse.
* @param validate indicates if the feed should be strictly validated (NOT YET IMPLEMENTED).
* @return the resulting feed bean.
* @throws IllegalArgumentException thrown if the parser cannot handle the given feed type.
* @throws FeedException thrown if a feed bean cannot be created out of the XML document (JDOM).
*/
@Override
public WireFeed parse(final Document document, final boolean validate, final Locale locale) throws IllegalArgumentException, FeedException {
Opml opml;
opml = (Opml) super.parse(document, validate, locale);
opml.setFeedType("opml_1.1");

final Element root = document.getRootElement();
final Element head = root.getChild("head");
final Element cloud = head.getChild("cloud");

if (null != cloud) {
if (null != cloud.getAttribute("domain")) {
checkNullCloud(opml);
opml.getCloud().setDomain(cloud.getAttributeValue("domain"));
}
if (null != cloud.getAttribute("port")) {
checkNullCloud(opml);
opml.getCloud().setPort(cloud.getAttributeValue("port"));
}
if (null != cloud.getAttribute("path")) {
checkNullCloud(opml);
opml.getCloud().setPath(cloud.getAttributeValue("path"));
}
if (null != cloud.getAttribute("registerProcedure")) {
checkNullCloud(opml);
opml.getCloud().setRegisterProcedure(cloud.getAttributeValue("registerProcedure"));
}
if (null != cloud.getAttribute("protocol")) {
checkNullCloud(opml);
opml.getCloud().setProtocol(cloud.getAttributeValue("protocol"));
}
}

return opml;
}

private void checkNullCloud(Opml opml) {
if (null == opml.getCloud()) {
opml.setCloud(new Cloud());
}
}
}
3 changes: 3 additions & 0 deletions rome-opml/src/main/resources/rome.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@


WireFeedGenerator.classes=com.rometools.opml.io.impl.OPML10Generator \
com.rometools.opml.io.impl.OPML11Generator \
com.rometools.opml.io.impl.OPML20Generator

WireFeedParser.classes=com.rometools.opml.io.impl.OPML10Parser \
com.rometools.opml.io.impl.OPML11Parser \
com.rometools.opml.io.impl.OPML20Parser

Converter.classes=com.rometools.opml.feed.synd.impl.ConverterForOPML10 \
com.rometools.opml.feed.synd.impl.ConverterForOPML11 \
com.rometools.opml.feed.synd.impl.ConverterForOPML20
Loading