Skip to content

Commit

Permalink
add support for AutoViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jul 21, 2013
1 parent fdd2be4 commit c9ea3d3
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.code</groupId>
<artifactId>simpleviewergallerydownloader</artifactId>
<version>0.3</version>
<version>0.4</version>
<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public Downloader(URL xmlUrl, File downloadDirectory, String username,
}

String urlPath = getGaleryPath(xmlUrl);
System.out.println("URL PATH = " + urlPath);
String imagePath = gallery.getImagePath();
List<Image> images = gallery.getImages();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -36,34 +38,67 @@ public DownloaderParseException(String msg) {
}
}

private static final String XPATH_IMAGEPATH = "/simpleviewergallery/@imagepath";
private static final String XPATH_IMAGES = "/simpleviewergallery/image";
private static final String XPATH_ROOT_SIMPLEVIEWER = "/simpleviewergallery";
private static final String XPATH_ROOT_AUTOVIEWER = "/gallery";
private static final String XPATH_IMAGEPATH = "./@imagepath";
private static final String XPATH_IMAGES = "./image";
private static final String XPATH_FILENAME = "./filename/text()";
private static final String XPATH_IMAGEURL = "./@imageurl";
private static final String XPATH_URL = "./url/text()";

private final List<XPathExpression> xPathRoots;
private final XPathExpression xPathImages;
private final XPathExpression xPathImagePath;
private final XPathExpression xPathFilename;
private final XPathExpression xPathImageURL;
private final List<XPathExpression> xPathURLs;

private final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();

private final XPathFactory xPathFactory = XPathFactory.newInstance();

public DownloaderXMLParser() throws DownloaderParseException {
XPath xPath = xPathFactory.newXPath();
final XPath xPath = xPathFactory.newXPath();

try {
xPathRoots = new ArrayList<XPathExpression>() {
private static final long serialVersionUID = 5816806284340915249L;
{
// Simpleviewer
add(xPath.compile(XPATH_ROOT_SIMPLEVIEWER));
// Autoviewer
add(xPath.compile(XPATH_ROOT_AUTOVIEWER));
}
};
xPathImages = xPath.compile(XPATH_IMAGES);
xPathImagePath = xPath.compile(XPATH_IMAGEPATH);
xPathFilename = xPath.compile(XPATH_FILENAME);
xPathImageURL = xPath.compile(XPATH_IMAGEURL);
xPathURLs = new ArrayList<XPathExpression>() {
private static final long serialVersionUID = -7787794328029149578L;
{
// Simpleviewer Gallery version 1.x
add(xPath.compile(XPATH_FILENAME));
// Simpleviewer Gallery version 2.x
add(xPath.compile(XPATH_IMAGEURL));
// Autoviewer
add(xPath.compile(XPATH_URL));
}
};
} catch (XPathExpressionException e) {
throw new DownloaderParseException(e);
}
}

private Node getFirstValidEvaluation(Node node,
List<XPathExpression> expressions) throws XPathExpressionException {
Node evaluationResult = null;
for (XPathExpression each : expressions) {
evaluationResult = (Node) each.evaluate(node, XPathConstants.NODE);
if (evaluationResult != null) {
return evaluationResult;
}
}
return null;
}

/**
* Recursively convert all tags and attributes to lower case
*/
Expand All @@ -88,50 +123,47 @@ private void prepareDocument(Document doc, Node nodeInput) {

public SimpleviewerGallery parse(InputStream is) throws DOMException,
XPathExpressionException, ParserConfigurationException,
SAXException, IOException {
SAXException, IOException, DownloaderParseException {
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();

Document doc = builder.parse(is);
prepareDocument(doc, doc);

// root node

Node rootNode = getFirstValidEvaluation(doc, xPathRoots);
if (rootNode == null) {
throw new DownloaderParseException(
"Unknown XML format: Root element has to be <simpleviewergallery> or <gallery> (case insensitive)!");
}

SimpleviewerGallery gallery = new SimpleviewerGallery();

// image path

Node imagePathNode = (Node) xPathImagePath.evaluate(doc,
Node imagePathNode = (Node) xPathImagePath.evaluate(rootNode,
XPathConstants.NODE);
String imagePath = (imagePathNode == null) ? "" : imagePathNode
.getNodeValue();
gallery.setImagePath(imagePath);

// images

NodeList imageNodes = (NodeList) xPathImages.evaluate(doc,
NodeList imageNodes = (NodeList) xPathImages.evaluate(rootNode,
XPathConstants.NODESET);

if (imageNodes == null) {
return gallery;
}

for (int i = 0; i < imageNodes.getLength(); i++) {
Node currentResult = imageNodes.item(i);
Node currentImageNode = imageNodes.item(i);
Node currentImageURLNode = getFirstValidEvaluation(
currentImageNode, xPathURLs);

Node imageURLNode = (Node) xPathImageURL.evaluate(currentResult,
XPathConstants.NODE);
Node fileNameNode = (Node) xPathFilename.evaluate(currentResult,
XPathConstants.NODE);

if (imageURLNode != null) {
// Simpleviewer Gallery version 1.x
Image image = new Image(imageURLNode.getNodeValue());
gallery.addImage(image);
} else if (fileNameNode != null) {
// Simpleviewer Gallery version 2.x
Image image = new Image(fileNameNode.getNodeValue());
if (currentImageURLNode != null) {
Image image = new Image(currentImageURLNode.getNodeValue());
gallery.addImage(image);
} else {
// Unknown version
continue;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,23 @@ public void testXMLParse7() throws DownloaderParseException {
.getImages().get(0).getImageURL());
}

/**
* http://www.photo-art.asia/mona/gallery.xml
*/
@Test
public void testXMLParse8() throws DownloaderParseException {
InputStream is = TestDownloaderXMLParser.class
.getClassLoader()
.getResourceAsStream(
"com/google/code/simpleviewergallerydownloader/test/gallery8.xml");
assertNotNull(is);
SimpleviewerGallery gallery = new DownloaderXMLParser()
.getSimpleviewerGallery(is);
assertEquals(30, gallery.getImages().size());

assertEquals("", gallery.getImagePath());
assertEquals("images/01.jpg", gallery
.getImages().get(0).getImageURL());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?xml version="1.0" encoding="UTF-8"?>
<gallery frameColor="0xFFFFFF" frameWidth="0" imagePadding="80" displayTime="6" enableRightClickOpen="false">
<image>
<url>images/01.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/02.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/03.jpg</url>
<caption/>
<width>666</width>
<height>1000</height>
</image>
<image>
<url>images/04.jpg</url>
<caption/>
<width>1659</width>
<height>1000</height>
</image><image>
<url>images/05.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/06.jpg</url>
<caption/>
<width>666</width>
<height>1000</height>
</image>
<image>
<url>images/07.jpg</url>
<caption/>
<width>666</width>
<height>1000</height>
</image>
<image>
<url>images/08.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/09.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/10.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/11.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/12.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/13.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/14.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/15.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/16.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/17.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/18.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/19.jpg</url>
<caption/>
<width>666</width>
<height>1000</height>
</image>
<image>
<url>images/20.jpg</url>
<caption/>
<width>666</width>
<height>1000</height>
</image>
<image>
<url>images/21.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/22.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/23.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/24.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image><image>
<url>images/25.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/26.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/27.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/28.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/29.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
<image>
<url>images/30.jpg</url>
<caption/>
<width>1500</width>
<height>1000</height>
</image>
</gallery>

0 comments on commit c9ea3d3

Please sign in to comment.