%@ page import="org.w3c.dom.*,java.io.*,org.apache.xerces.parsers.*,java.util.*,java.awt.*,java.awt.image.*,java.net.*" errorPage="error.jsp" autoFlush="true" buffer="256kb" contentType="text/html; charset=windows-1251" %>
Ергаки - Галерея
<%!
String error, basedir, maxwidth; int spacing; long lastModified;
Gallery[] galleries;
Hashtable galleriesByName;
class Picture {String src, title, description; int width = -1, height = -1, slideWidth = -1, slideHeight = -1;}
class Gallery {String name,title,description,dir,slidesdir,image,label; Picture[] pictures;}
private String getAttr(Node g, String name, String defaultValue) {
Node n = g.getAttributes().getNamedItem(name);
return n == null ? defaultValue : n.getNodeValue();
}
public static String toHtml(String str) {
if(str == null) return "null";
StringBuffer buf = new StringBuffer("");
char ch;
for(int curChar = 0; curChar < str.length(); curChar++) {
ch = str.charAt(curChar);
switch(ch) {
case '"' : buf.append("""); break;
case '<' : buf.append("<"); break;
case '>' : buf.append(">"); break;
case '&' : buf.append("&"); break;
default : buf.append(ch); break;
}
}
return buf.toString();
}
public void jspInit() {
try {
error = ""; basedir = null; maxwidth = null; spacing = 0; galleriesByName = new Hashtable(); galleries = null;
lastModified = new File("/hosts/ergaki/htdocs/galleries.xml").lastModified();
// parse
DOMParser parser = new DOMParser();
parser.parse("/hosts/ergaki/htdocs/galleries.xml");
Document document = parser.getDocument();
// parse galleries attributes
Node g = document.getElementsByTagName("galleries").item(0);
basedir = getAttr(g,"basedir","."); if (!basedir.endsWith("/") && !basedir.equals("")) basedir = basedir + "/";
maxwidth = getAttr(g,"maxwidth","700");
spacing = str2int(getAttr(g,"spacing","10"),10);
NodeList galleriesList = g.getChildNodes();
// parse galleries
galleries = new Gallery[galleriesList.getLength()];
for (int i = 0; i < galleries.length; i++) {
Node gallery = galleriesList.item(i);
if (gallery.getNodeName().equals("gallery")) {
// parse gallery attributes
galleries[i] = new Gallery();
galleries[i].name = getAttr(gallery,"name",null);
if (galleries[i].name == null) throw new IllegalArgumentException("gallery.name is null");
galleriesByName.put(galleries[i].name,galleries[i]);
galleries[i].title = getAttr(gallery,"title",null);
galleries[i].description = getAttr(gallery,"description",null);
galleries[i].dir = getAttr(gallery,"dir",".");
if (!galleries[i].dir.endsWith("/") && !galleries[i].dir.equals("")) galleries[i].dir = galleries[i].dir + "/";
galleries[i].slidesdir = getAttr(gallery,"slidesdir",".");
if (!galleries[i].slidesdir.endsWith("/") && !galleries[i].slidesdir.equals("")) galleries[i].slidesdir = galleries[i].slidesdir + "/";
galleries[i].label = getAttr(gallery,"label",null);
galleries[i].image = getAttr(gallery,"image",null);
// parse pictures
NodeList picturesList = gallery.getChildNodes();
Picture[] pictures = (galleries[i].pictures = new Picture[picturesList.getLength()]);
for (int j = 0; j < pictures.length; j++) {
Node picture = picturesList.item(j);
if (picture.getNodeName().equals("picture")) {
// parse picture
pictures[j] = new Picture();
pictures[j].src = picture.getFirstChild().getNodeValue();
pictures[j].title = getAttr(picture,"title",null);
pictures[j].description = getAttr(picture,"description",null);
}
}
}
}
}
catch (Exception e) {error = e.toString();}
if (galleries != null) {
for (int i = 0; i < galleries.length; i++)
if (galleries[i] != null) {
galleries[i].pictures = removeEmpty(galleries[i].pictures);
// defineDimensions(galleries[i]);
}
galleries = removeEmpty(galleries);
}
}
Picture[] removeEmpty(Picture[] o) {
int N = 0;
for (int i = 0; i < o.length; i++)
if (o[i] != null) N++;
Picture[] p = new Picture[N];
int n = 0;
for (int i = 0; i < o.length; i++)
if (o[i] != null) p[n++] = o[i];
return p;
}
Gallery[] removeEmpty(Gallery[] o) {
int N = 0;
for (int i = 0; i < o.length; i++)
if (o[i] != null) N++;
Gallery[] p = new Gallery[N];
int n = 0;
for (int i = 0; i < o.length; i++)
if (o[i] != null) p[n++] = o[i];
return p;
}
static int str2int(String s, int def) {
try {return Integer.parseInt(s);} catch(Exception e) {return def;}
}
void fail(String msg) throws Exception {
throw new Exception(msg);
}
String cc(String s) {
return s;
/*try {
return new String(s.getBytes("windows-1251"));
}
catch (UnsupportedEncodingException e) {
return s;
}*/
}
String menu;
String menu() {
if (menu != null) return menu;
String r = "\n";
return menu;
}
class MyImageObserver implements ImageObserver {
int width = -1, height = -1;
public synchronized boolean imageUpdate(Image img, int infoflags, int x, int y, int awidth, int aheight) {
if ((infoflags & WIDTH) != 0) width = awidth;
if ((infoflags & HEIGHT) != 0) height = aheight;
if ((infoflags & ERROR) != 0) {width = -2; height = -2;}
if (width != -1 && height != -1) {notifyAll(); return false;}
return true;
}
}
Dimension getSize(Image image) {
MyImageObserver obs = new MyImageObserver();
int width = image.getWidth(obs), height = image.getHeight(obs);
synchronized(obs) {
if (obs.height == -1 || obs.width == -1) {
try {obs.wait();} catch(Exception e) {}
}
}
return new Dimension(obs.width,obs.height);
}
void defineDimensions2(Gallery g) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
for (int i = 0; i < g.pictures.length; i++) {
Picture p = g.pictures[i];
Dimension dim = getSize(toolkit.getImage(basedir + g.dir + p.src));
p.width = (int)dim.getWidth(); p.height = (int)dim.getHeight();
dim = getSize(toolkit.getImage(basedir + g.slidesdir + p.src));
p.slideWidth = (int)dim.getWidth(); p.slideHeight = (int)dim.getHeight();
}
}
Dimension getSize(String file) {
Dimension d = new Dimension(-1,-1);
try {
if (file.endsWith(".gif")) {
RandomAccessFile f = new RandomAccessFile(file, "r");
f.seek(6);
d.setSize(f.readUnsignedShort(),f.readUnsignedShort());
f.close();
}
else {
RandomAccessFile f = new RandomAccessFile(file, "r");
int s = f.readUnsignedShort();
if (s != 0xFFD8) throw new Exception();
for (;;) {
s = f.readUnsignedShort();
if (s == 0xFFC0) break;
s = f.readUnsignedShort();
f.seek(f.getFilePointer() + s);
}
f.seek(f.getFilePointer() + 3);
int height = f.readUnsignedShort(), width = f.readUnsignedShort();
d.setSize(width,height);
}
}
catch (Exception e) {d.setSize(-1,-1);}
return d;
}
void defineDimensions(Gallery g) {
for (int i = 0; i < g.pictures.length; i++) {
Picture p = g.pictures[i];
Dimension d = getSize("/hosts/ergaki/htdocs" + basedir + g.dir + p.src);
p.width = (int)d.getWidth(); p.height = (int)d.getHeight();
d = getSize(basedir + g.slidesdir + p.src);
p.slideWidth = (int)d.getWidth(); p.slideHeight = (int)d.getHeight();
}
}
static String getResource(String url) throws IOException {
InputStream in = new URL(url).openStream();
byte[] buf = new byte[65535];
int len;
StringBuffer sb = new StringBuffer();
while ((len = in.read(buf)) > 0)
sb.append(new String(buf,0,len,"windows-1251"));
in.close();
return sb.toString();
}
%>
<%=getResource("http://ergaki.krasu.ru/header.inc")%>
<%
request.setCharacterEncoding("windows-1251");
if (lastModified != new File("/hosts/ergaki/htdocs/galleries.xml").lastModified()) jspInit();
if (error != null && !error.equals("")) fail("error: " + error);
String param_gallery = request.getParameter("g"), param_picture = request.getParameter("p");
if (param_gallery != null && galleriesByName.get(param_gallery) != null) {
Gallery gallery = (Gallery)(galleriesByName.get(param_gallery));
int pic = str2int(param_picture,-1);
if (param_picture != null && pic >= 0 && pic < gallery.pictures.length) {
Picture picture = gallery.pictures[pic];
// вывод картинки picture
String label = (gallery.label == null ? (gallery.title == null ? "" : "" + cc(gallery.title) + " ") : " ");
String backward = (pic > 0 ? " " : "");
String forward = (pic < gallery.pictures.length - 1 ? " " : "");
String description = (picture.description == null ? "" : "" + cc(picture.description) + "
");
%><%=label%><%=backward%><%=picture.title==null?"":"" + cc(picture.title) + " "%><%=description%> <%=forward%>
<%
// конец вывода картинки
%><%=menu()%><%
}
else {
// вывод галереи gallery
String label = (gallery.label == null ? (gallery.title == null ? "" : "" + cc(gallery.title) + " ") : " ");
%><%=label%> <%=cc(gallery.description)%>
<%
%><%=menu()%><%
// конец вывод галереи
}
}
else {
// вывод списка галерей galleries
%> В этом разделе собрана коллекция фотографий озер, водопадов, скал и цветов Ергаков. Если у вас есть красивые или просто интересные фотографии Ергаков - присылайте их нам и мы разместим их в этом разделе.
<%
Random random = new Random();
for (int i = 0; i < galleries.length; i++) {
if (i % 3 == 0 && i > 0) {%>
<%}
String image = (galleries[i].image == null ? basedir + galleries[i].slidesdir + galleries[i].pictures[random.nextInt(galleries[i].pictures.length)].src : galleries[i].image);
String label = (galleries[i].label == null ? (galleries[i].title == null ? "" : "" + cc(galleries[i].title) + " ") : " ");
%><%=label%> border=0 src="<%=image%>"> <%
}
%>
<%
// конец вывод списка галерей
}
%>
<%=getResource("http://ergaki.krasu.ru/tail.inc")%>