| 1 |
|
package yarfraw.mapping.backward.impl; |
| 2 |
|
|
| 3 |
|
import java.util.ArrayList; |
| 4 |
|
import java.util.List; |
| 5 |
|
|
| 6 |
|
import org.apache.commons.lang.builder.ToStringBuilder; |
| 7 |
|
import org.apache.commons.logging.Log; |
| 8 |
|
import org.apache.commons.logging.LogFactory; |
| 9 |
|
import org.w3c.dom.Element; |
| 10 |
|
|
| 11 |
|
import yarfraw.core.datamodel.Generator; |
| 12 |
|
import yarfraw.core.datamodel.ItemEntry; |
| 13 |
|
import yarfraw.core.datamodel.Link; |
| 14 |
|
import yarfraw.core.datamodel.Person; |
| 15 |
|
import yarfraw.generated.atom03.elements.EntryType; |
| 16 |
|
import yarfraw.generated.atom03.elements.GeneratorType; |
| 17 |
|
import yarfraw.generated.atom03.elements.LinkType; |
| 18 |
|
import yarfraw.generated.atom03.elements.PersonType; |
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
0 |
class Atom03MappingUtils{ |
| 26 |
|
|
| 27 |
3 |
private static final Log LOG = LogFactory.getLog(Atom03MappingUtils.class); |
| 28 |
|
|
| 29 |
|
public static ItemEntry toItemEntry(EntryType entry){ |
| 30 |
78 |
if(entry == null){ |
| 31 |
0 |
return null; |
| 32 |
|
} |
| 33 |
78 |
ItemEntry ret = new ItemEntry(); |
| 34 |
|
|
| 35 |
78 |
if(entry.getAuthor() != null){ |
| 36 |
0 |
ret.addAuthorOrCreator(toPerson(entry.getAuthor())); |
| 37 |
|
} |
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
78 |
if(entry.getContributor() != null){ |
| 42 |
78 |
for(PersonType p : entry.getContributor()){ |
| 43 |
0 |
ret.addContributor(toPerson(p)); |
| 44 |
|
} |
| 45 |
|
} |
| 46 |
|
|
| 47 |
78 |
ret.setPubDate(entry.getCreated()); |
| 48 |
78 |
ret.setUid(entry.getId()); |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
78 |
ret.setLang(entry.getLang()); |
| 53 |
78 |
if(entry.getLink() != null){ |
| 54 |
78 |
ret.addLink(toLink(entry.getLink())); |
| 55 |
|
} |
| 56 |
78 |
ret.setUpdatedDate(entry.getModified()); |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
78 |
if(entry.getOtherAttributes() != null){ |
| 61 |
78 |
ret.getOtherAttributes().putAll(entry.getOtherAttributes()); |
| 62 |
|
} |
| 63 |
78 |
ret.getOtherElements().addAll(getElements(entry.getAny())); |
| 64 |
78 |
return ret; |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
public static Person toPerson(PersonType person){ |
| 68 |
3 |
if(person == null){ |
| 69 |
0 |
return null; |
| 70 |
|
} |
| 71 |
3 |
Person ret = |
| 72 |
|
new Person() |
| 73 |
|
.setEmailOrText(person.getEmail()) |
| 74 |
|
.setName(person.getName()) |
| 75 |
|
.setUri(person.getUrl()); |
| 76 |
|
|
| 77 |
3 |
ret.setEmailOrText(person.getEmail()); |
| 78 |
3 |
ret.setName(person.getName()); |
| 79 |
3 |
ret.getOtherElements().addAll(getElements(person.getAny())); |
| 80 |
3 |
return ret; |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
public static List<Element> getElements(List<Object> any){ |
| 84 |
84 |
List<Element> ret = new ArrayList<Element>(); |
| 85 |
84 |
if(any != null){ |
| 86 |
84 |
for(Object o : any){ |
| 87 |
318 |
if(o == null){ |
| 88 |
0 |
continue; |
| 89 |
|
} |
| 90 |
318 |
if (o instanceof Element) { |
| 91 |
318 |
Element e = (Element) o; |
| 92 |
318 |
ret.add(e); |
| 93 |
318 |
}else{ |
| 94 |
0 |
LOG.warn("Unexpected object: "+ToStringBuilder.reflectionToString(o)+" this should not happen!"); |
| 95 |
|
} |
| 96 |
|
} |
| 97 |
|
} |
| 98 |
84 |
return ret; |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
public static Generator toGenerator(GeneratorType gen){ |
| 102 |
3 |
if(gen == null){ |
| 103 |
0 |
return null; |
| 104 |
|
} |
| 105 |
3 |
Generator ret = new Generator(gen.getValue()) |
| 106 |
|
.setUri(gen.getUrl()) |
| 107 |
|
.setVersion(gen.getVersion()); |
| 108 |
3 |
return ret; |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
public static Link toLink(LinkType lnk){ |
| 112 |
81 |
if(lnk == null){ |
| 113 |
0 |
return null; |
| 114 |
|
} |
| 115 |
81 |
Link ret = new Link() |
| 116 |
|
.setHref(lnk.getHref()) |
| 117 |
|
.setRel(lnk.getRel()) |
| 118 |
|
.setTitle(lnk.getTitle()) |
| 119 |
|
.setType(lnk.getType()); |
| 120 |
81 |
if(lnk.getOtherAttributes() != null){ |
| 121 |
81 |
ret.getOtherAttributes().putAll(lnk.getOtherAttributes()); |
| 122 |
|
} |
| 123 |
81 |
return ret; |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
} |