September 14, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Java Tip: Dynamically Update SimpleViewer Galleries

  • January 20, 2011
  • By Developer.com Staff
  • Send Email »
  • More Articles »

If you are a fan of the Web image viewer SimpleViewer, then you know that this application renders pictures based on an XML document named gallery.xml. In this Java tip, Anghel Leonard explains how to update the gallery.xml document dynamically. The tip is based on the following scenario:

  1. Each user should have his or her own SimpleViewer instance.
  2. The user should be able to upload pictures to his or her gallery.
  3. The SimpleViewer instance should update the gallery dynamically when a new picture is uploaded.

Here is the stateless bean Anghel wrote. It rewrites the gallery.xml file after each new picture is uploaded. The method gets (as arguments) the generic path of gallery.xml and the folder name specific to each user:

package com.gallery;

import java.io.File;
import java.io.FileOutputStream;
import javax.annotation.Resource;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.ejb.SessionContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

@Stateless
@LocalBean
@DeclareRoles({"user_role"})
public class GalleryBean {

@Resource
private SessionContext ctx;

/**
* Update the gallery.xml for SimpleViewer
* @param path the path to gallery.xml
* @param myFolder the user folder
* @throws Exception
*/
@RolesAllowed("user_role")
public int galleryUpdate(String path, String myFolder) throws Exception {
try {
if (ctx.isCallerInRole("user_role")) {
String pathImgs = path + "//images";
File dir = new File(pathImgs);

String[] children = dir.list();

if (children == null) {
return 0;
} else {
DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance();
DocumentBuilder DB = DBF.newDocumentBuilder();
DOMImplementation DOMi = DB.getDOMImplementation();
Document D = DOMi.createDocument(null, null, null);

Element root = D.createElement("simpleviewergallery");

Attr galleryStyle = D.createAttribute("galleryStyle");
galleryStyle.setValue("COMPACT");

Attr title = D.createAttribute("title");
title.setValue("");

Attr textColor = D.createAttribute("textColor");
textColor.setValue("#FFFFFF");

Attr frameColor = D.createAttribute("frameColor");
frameColor.setValue("#FFFFFF");

Attr frameWidth = D.createAttribute("frameWidth");
frameWidth.setValue("0");

Attr thumbPosition = D.createAttribute("thumbPosition");
thumbPosition.setValue("BOTTOM");

Attr thumbColumns = D.createAttribute("thumbColumns");
thumbColumns.setValue("7");

Attr thumbRows = D.createAttribute("thumbRows");
thumbRows.setValue("1");

Attr showOpenButton = D.createAttribute("showOpenButton");
showOpenButton.setValue("TRUE");

Attr showFullscreenButton = D.createAttribute("showFullscreenButton");
showFullscreenButton.setValue("FALSE");

Attr maxImageWidth = D.createAttribute("maxImageWidth");
maxImageWidth.setValue("800");

Attr maxImageHeight = D.createAttribute("maxImageHeight");
maxImageHeight.setValue("600");

Attr useFlickr = D.createAttribute("useFlickr");
useFlickr.setValue("false");

Attr flickrUserName = D.createAttribute("flickrUserName");
flickrUserName.setValue("");

Attr flickrTags = D.createAttribute("flickrTags");
flickrTags.setValue("");

Attr languageCode = D.createAttribute("languageCode");
languageCode.setValue("AUTO");

Attr languageList = D.createAttribute("languageList");
languageList.setValue("");

Attr imagePath = D.createAttribute("imagePath");
imagePath.setValue("images/");

Attr thumbPath = D.createAttribute("thumbPath");
thumbPath.setValue("images/");

root.setAttributeNode(galleryStyle);
root.setAttributeNode(title);
root.setAttributeNode(textColor);
root.setAttributeNode(frameColor);
root.setAttributeNode(frameWidth);
root.setAttributeNode(thumbPosition);
root.setAttributeNode(thumbColumns);
root.setAttributeNode(thumbRows);
root.setAttributeNode(showOpenButton);
root.setAttributeNode(showFullscreenButton);
root.setAttributeNode(maxImageWidth);
root.setAttributeNode(maxImageHeight);
root.setAttributeNode(useFlickr);
root.setAttributeNode(flickrUserName);
root.setAttributeNode(flickrTags);
root.setAttributeNode(languageCode);
root.setAttributeNode(languageList);
root.setAttributeNode(imagePath);
root.setAttributeNode(thumbPath);

D.appendChild(root);

for (int i = 0; i < children.length; i++) {
// Get filename of file or directory
String filename = children[i];

if ((filename.endsWith("jpg")) || (filename.endsWith("png"))
|| (filename.endsWith("gif")) || (filename.endsWith("JPG")) ||
(filename.endsWith("PNG")) || (filename.endsWith("GIF"))) {

Element image = D.createElement("image");

Attr imageURL = D.createAttribute("imageURL");
imageURL.setValue("../resources/scripts/web/" + myFolder +
"/images/" + filename);

Attr thumbURL = D.createAttribute("thumbURL");
thumbURL.setValue("../resources/scripts/web/" + myFolder +
"/images/" + filename);

Attr linkURL = D.createAttribute("linkURL");
linkURL.setValue("");

Attr linkTarget = D.createAttribute("linkTarget");
linkTarget.setValue("");

image.setAttributeNode(imageURL);
image.setAttributeNode(thumbURL);
image.setAttributeNode(linkURL);
image.setAttributeNode(linkTarget);

root.appendChild(image);
}
}

//serialize gallery.xml
File fileOut = new File(path + "//gallery.xml");
FileOutputStream fileOutputStream = new FileOutputStream(fileOut);

DOMImplementation DOMLS = (DOMImplementation) DOMi.getFeature("LS", "3.0");
DOMImplementationLS DOMiLS = (DOMImplementationLS) DOMLS;

LSOutput LSO = DOMiLS.createLSOutput();
LSO.setByteStream(fileOutputStream);

LSSerializer LSS = DOMiLS.createLSSerializer();
LSS.write(D, LSO);

fileOutputStream.close();

return 1;
}
}

return 0;

} catch (Exception e) {
throw e;
}
}
}

Tags: Java, XML

Originally published on http://www.developer.com.


Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel