📄 slideshowprocessor.java
字号:
/* * * Copyright (C) GNU/GPL AVOIR 2008 * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation; either version 2 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */package org.avoir.realtime.plugins;import java.awt.Color;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.Writer;import java.util.ArrayList;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.avoir.realtime.common.util.XmlUtils;import org.avoir.realtime.slidebuilder.Slide;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.QName;import org.jivesoftware.openfire.PacketRouter;import org.jivesoftware.util.Base64;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xmpp.packet.IQ;import org.xmpp.packet.JID;/** * * @author developer */public class SlideShowProcessor { private AvoirRealtimePlugin pl; private ArrayList<JID> jids; private PacketRouter packetRouter; private DocumentBuilder documentBuilder; private DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); public SlideShowProcessor(AvoirRealtimePlugin pl) { this.pl = pl; jids = pl.getUsers(false); packetRouter = pl.getPacketRouter(); try { documentBuilder = documentBuilderFactory.newDocumentBuilder(); } catch (Exception ex) { ex.printStackTrace(); } } static public String getContents(File aFile) { StringBuilder contents = new StringBuilder(); try { BufferedReader input = new BufferedReader(new FileReader(aFile)); try { String line = null; while ((line = input.readLine()) != null) { contents.append(line); contents.append(System.getProperty("line.separator")); } } finally { input.close(); } } catch (IOException ex) { ex.printStackTrace(); } return contents.toString(); } public IQ saveSlideShowFile(IQ packet, Document doc) { String fileName = XmlUtils.readString(doc, "filename"); String username = XmlUtils.readString(doc, "username"); Boolean modified = new Boolean(XmlUtils.readString(doc, "modified")); String access = XmlUtils.readString(doc, "access"); String contents = packet.toXML(); String destDir = Constants.FILES_DIR + "/slideshows/" + fileName + ".ss"; new File(Constants.FILES_DIR + "/slideshows/" + username).mkdirs(); destDir = Constants.FILES_DIR + "/slideshows/" + fileName + ".ss"; if (access.equals("private")) { destDir = Constants.FILES_DIR + "/slideshows/" + username + "/" + fileName + ".ss"; } Writer output = null; try { output = new BufferedWriter(new FileWriter(destDir)); output.write(contents); } catch (IOException ex) { ex.printStackTrace(); } finally { try { output.close(); } catch (Exception ex) { ex.printStackTrace(); } } if (modified != null) { if (modified) { pl.getRoomResourceManager().increaseSlideShowRoomResourceVersion(packet, destDir); } } return pl.getDefaultPacketProcessor().getFileView(packet, "slideshows", username); } private String extractContent(String xmlContents, IQ packet, int version,String access) { StringBuilder sb = new StringBuilder(); try { Document doc = documentBuilder.parse( new ByteArrayInputStream(xmlContents.getBytes(Constants.PREFERRED_ENCODING))); String username = XmlUtils.readString(doc, "username"); NodeList slidesNodeList = doc.getElementsByTagName("slide"); String filename = XmlUtils.readString(doc, "filename"); ArrayList<Slide> slides = new ArrayList<Slide>(); for (int i = 0; i < slidesNodeList.getLength(); i++) { Node node = slidesNodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { org.w3c.dom.Element element = (org.w3c.dom.Element) node; String title = XmlUtils.readString(element, "title"); String text = XmlUtils.readString(element, "text"); int textSize = XmlUtils.readInt(element, "text-size"); NodeList colorNodeList = node.getChildNodes(); int r = 0; int g = 0; int b = 0; for (int j = 0; j < colorNodeList.getLength(); j++) { Node colorNode = slidesNodeList.item(i); if (colorNode.getNodeType() == Node.ELEMENT_NODE) { org.w3c.dom.Element colorElement = (org.w3c.dom.Element) colorNode; r = XmlUtils.readInt(colorElement, "red"); g = XmlUtils.readInt(colorElement, "green"); b = XmlUtils.readInt(colorElement, "blue"); } } Color textColor = new Color(r, g, b); String questionPath = XmlUtils.readString(element, "question-path"); String url = XmlUtils.readString(element, "url"); String imagePath = XmlUtils.readString(element, "image-path"); slides.add(new Slide(title, text, textColor, textSize, questionPath, imagePath, url, null, null, i)); } } sb.append("<filename>").append(filename).append("</filename>"); sb.append("<username>").append(username).append("</username>"); sb.append("<access>").append(access).append("</access>"); sb.append("<version>").append(version).append("</version>"); sb.append("<slides>"); for (int i = 0; i < slides.size(); i++) { Slide slide = slides.get(i); sb.append("<slide>"); sb.append("<title>").append(slide.getTitle()).append("</title>"); sb.append("<text>").append(slide.getText()).append("</text>"); sb.append("<url>").append(slide.getUrl()).append("</url>"); sb.append("<text-color>"); Color color = slide.getTextColor(); sb.append("<red>").append(color.getRed()).append("</red>"); sb.append("<blue>").append(color.getBlue()).append("</blue>"); sb.append("<green>").append(color.getGreen()).append("</green>"); sb.append("</text-color>"); sb.append("<text-size>").append(slide.getTextSize()).append("</text-size>"); String questionPath = slide.getQuestionPath(); String questionContent = null; if (questionPath != null) { if (!questionPath.equals("null") || !questionPath.isEmpty()) { try { String qnXmlContents = Util.getContents(new File(questionPath)); int startContentIndex = qnXmlContents.indexOf("<content>"); int endContentIndex = qnXmlContents.indexOf("</content>") + "</content>".length(); String qnContent = qnXmlContents.substring(startContentIndex, endContentIndex); questionContent = pl.getQuestionProcessor().extractQuestionContent(qnContent, "public", packet.getFrom(), questionPath); } catch (Exception ex) { ex.printStackTrace(); } } } sb.append("<slide" + i + "-question>").append(questionContent).append("</slide" + i + "-question>"); String imagePath = slide.getImagePath(); String imageData = Base64.encodeFromFile(imagePath); sb.append("<image-data>").append(imageData).append("</image-data>"); sb.append("</slide>"); } sb.append("</slides>"); } catch (Exception ex) { ex.printStackTrace(); } return sb.toString(); } public void broadcastSlide(IQ packet, Document xdoc) { String slideShowName = XmlUtils.readString(xdoc, "slide-show-name"); String slideTitle = XmlUtils.readString(xdoc, "slide-title"); IQ replyPacket = IQ.createResultIQ(packet); Element queryResult = DocumentHelper.createElement(QName.get("query", Constants.NAME_SPACE)); queryResult.addElement("mode").addText(Mode.BROADCAST_OUT_SLIDE); StringBuilder sb = new StringBuilder(); sb.append("<slide-show-name>").append(slideShowName).append("</slide-show-name>"); sb.append("<slide-title>").append(slideTitle).append("</slide-title>"); queryResult.addElement("content").addText(sb.toString()); replyPacket.setChildElement(queryResult); for (int i = 0; i < jids.size(); i++) { JID jid = jids.get(i); replyPacket.setTo(jid); replyPacket.setFrom(packet.getFrom()); packetRouter.route(replyPacket); } } public IQ downloadSlideShowFile(IQ packet, Document xdoc) { IQ replyPacket = IQ.createResultIQ(packet); String path = XmlUtils.readString(xdoc, "file-path"); String access = XmlUtils.readString(xdoc, "access"); int version = pl.getRoomResourceManager().getRoomResourceRoomVersion(path); String xmlContents = getContents(new File(path)); Element queryResult = DocumentHelper.createElement(QName.get("query", Constants.NAME_SPACE)); queryResult.addElement("mode").addText(Mode.DOWNLOAD_ROOM_SLIDE_SHOW); String replyContent = extractContent(xmlContents, packet, version,access); queryResult.addElement("content").addText(replyContent); replyPacket.setChildElement(queryResult); return replyPacket; } public IQ openSlideShowFile(IQ packet, Document xdoc) { IQ replyPacket = IQ.createResultIQ(packet); String fileName = XmlUtils.readString(xdoc, "filename"); String username = XmlUtils.readString(xdoc, "username"); String access = XmlUtils.readString(xdoc, "access"); String destFile = Constants.FILES_DIR + "/slideshows/" + fileName; if (access.equals("private")) { destFile = Constants.FILES_DIR + "/slideshows/" + username + "/" + fileName; } String xmlContents = getContents(new File(destFile)); Element queryResult = DocumentHelper.createElement(QName.get("query", Constants.NAME_SPACE)); queryResult.addElement("mode").addText(Mode.OPEN_SLIDE_SHOW); String replyContent = extractContent(xmlContents, packet, pl.getRoomResourceManager().getRoomResourceRoomVersion(destFile),access); queryResult.addElement("content").addText(replyContent); replyPacket.setChildElement(queryResult); return replyPacket; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -