Last active
January 3, 2016 02:39
-
-
Save JonCook/8397116 to your computer and use it in GitHub Desktop.
Scala objects with to xml processing and java comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cookybear.content.asset.factory | |
import com.cookybear.content.asset.Asset | |
import com.cookybear.content.asset.Story | |
object AssetFactory { | |
def factory(node: scala.xml.Node): Asset = { | |
val trimmedNode = scala.xml.Utility.trim(node) | |
trimmedNode match { | |
case <story>{ children @ _* }</story> => Story.fromXML(trimmedNode) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Byline parseByline(Element bylineElement) { | |
Byline byline = null; | |
if (bylineElement != null) { | |
byline = new Byline(); | |
byline.setName(bylineElement.attributeValue("name")); | |
byline.setTitle(bylineElement.attributeValue("title")); | |
List<Element> persons = BYLINE_PERSON_XPATH_SELECTOR.selectNodes(bylineElement); | |
for (Element element : persons) { | |
Person person = parsePerson(element); | |
byline.getPersons().add(person); | |
} | |
} | |
return byline; | |
} | |
private Person parsePerson(Element element) { | |
Person person = new Person(); | |
person.setThumbnail(getAbsoluteImageHref(element.attributeValue("thumbnail"))); | |
person.setFunction(BYLINE_PERSON_FUNCTION_XPATH_SELECTOR.valueOf(element)); | |
person.setName(BYLINE_PERSON_NAME_XPATH_SELECTOR.valueOf(element)); | |
return person; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cookybear.content.asset | |
case class Byline(val name: String, val title: String, val persons: List[Person]) { | |
def toXML = | |
<byline name={ name } title={ title }> | |
{ | |
if (!persons.isEmpty) | |
<persons>{ | |
for (person <- persons) yield person.toXML | |
}</persons> | |
} | |
</byline> | |
} | |
object Byline { | |
def fromXML(node: scala.xml.NodeSeq): Byline = | |
new Byline( | |
name = (node \ "@name").text, | |
title = (node \ "@title").text, | |
List[Person]((node \ "person").toList map { s => Person.fromXML(s) }: _*)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cookybear.content.asset | |
import scala.xml.NodeSeq | |
class Story( | |
itemMeta: ItemMeta, | |
pageOptions: PageOptions, | |
byline: Byline, | |
body: NodeSeq, | |
media: Media, | |
relatedGroups: RelatedGroups) extends Asset(itemMeta, pageOptions) { | |
override def toXML = | |
<result> | |
{ super.toXML.child } | |
{ byline.toXML } | |
{ body } | |
{ media.toXML } | |
{ relatedGroups.toXML } | |
</result> | |
} | |
object Story { | |
def fromXML(node: NodeSeq): Story = | |
new Story( | |
ItemMeta.fromXML((node \ "itemMeta")), | |
PageOptions.fromXML((node \ "pageOptions")), | |
Byline.fromXML((node \ "byline")), | |
(node \ "body"), | |
Media.fromXML((node \ "media")), | |
RelatedGroups.fromXML(node \ "relatedGroups")) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cookybear.content.asset | |
import com.cookybear.content.XmlDataSpec | |
import org.junit.runner.RunWith | |
import org.scalatest.junit.JUnitRunner | |
@RunWith(classOf[JUnitRunner]) | |
class BylineSpec extends XmlDataSpec { | |
describe("A Byline with a list of authors") { | |
val byline = Byline.fromXML(xmlFixture("/xml/byline.xml")) | |
it("should have name and title") { | |
assert(byline.name === "By AJP Taylor & Adam Brookes & Alan Hansen") | |
assert(byline.title === "Historian") | |
} | |
it("should contain a list of people") { | |
byline.persons must have length (3) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment