📄 xmlbuilder.java
字号:
/** * $RCSfile: XMLBuilder.java,v $ * $Revision: 1.5.2.1 $ * $Date: 2000/12/29 17:04:55 $ * * Copyright (C) 2000 CoolServlets.com. All rights reserved. * * =================================================================== * The Apache Software License, Version 1.1 * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by * CoolServlets.com (http://www.coolservlets.com)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Jive" and "CoolServlets.com" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please * contact webmaster@coolservlets.com. * * 5. Products derived from this software may not be called "Jive", * nor may "Jive" appear in their name, without prior written * permission of CoolServlets.com. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of CoolServlets.com. For more information * on CoolServlets.com, please see <http://www.coolservlets.com>. */package com.coolservlets.forum.xml;import java.util.*;import java.text.*;import java.io.*;import com.coolservlets.forum.*;import com.coolservlets.util.*;/** * Utility class for converting Jive objects to XML, and converting XML back * to Jive objects. */public class XMLBuilder { /** * Jive DTD version number. */ private static final String VERSION = "1.0"; /** * A simple date formatter that will convert Date objects to the Jive * date format. For example: 1978/11/15 12:00:00.00 PST */ private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SS z"); /** * Creates a full, validating XML document with the contents of a single * forum. */ public static void jiveToXML(Forum forum, ProfileManager manager, Writer out) throws UnauthorizedException, IOException { out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); out.write("<!DOCTYPE Jive SYSTEM \"jive.dtd\">"); out.write("<Jive version=\"" + VERSION + "\" exportDate=\"" + formatter.format(new Date()) + "\">" ); out.write("<ForumList>"); objectToXML(forum, manager, out); out.write("</ForumList></Jive>"); } /** * Creates a full, validating XML document with the contents of the entire * Jive system. */ public static void jiveToXML(ForumFactory factory, Writer out) throws UnauthorizedException, IOException { if (factory == null) { return; } out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); out.write("<!DOCTYPE Jive SYSTEM \"jive.dtd\">"); out.write("<Jive version=\"" + VERSION + "\" exportDate=\"" + formatter.format(new Date()) + "\">" ); //Properties Enumeration enum = PropertyManager.propertyNames(); if (enum.hasMoreElements()) { out.write("<PropertyList>"); while (enum.hasMoreElements()) { String propName = (String)enum.nextElement(); String propValue = StringUtils.escapeForXML(PropertyManager.getProperty(propName)); StringBuffer propBuff = new StringBuffer(); propBuff.append("<Property name=\"").append(propName).append("\" "); propBuff.append("value=\"").append(propValue).append("\"/>"); out.write(propBuff.toString()); } out.write("</PropertyList>"); } ProfileManager profileManager = factory.getProfileManager(); //User list Iterator iter = profileManager.users(); out.write("<UserList>"); while (iter.hasNext()) { User user = (User)iter.next(); objectToXML(user, out); } out.write("</UserList>"); //Group list iter = profileManager.groups(); out.write("<GroupList>"); while (iter.hasNext()) { Group group = (Group)iter.next(); objectToXML(group, out); } out.write("</GroupList>"); // Forum List out.write("<ForumList>"); iter = factory.forums(); while (iter.hasNext()) { Forum forum = (Forum) iter.next(); objectToXML(forum, factory.getProfileManager(), out); } out.write("</ForumList>"); //Permissions List //User List User specialUser = profileManager.getSpecialUser(); //Iterate through each permission type for users out.write("<UserPermissionList>"); int [] permTypes = new int [] { ForumPermissions.READ, ForumPermissions.FORUM_ADMIN, ForumPermissions.MODERATOR, ForumPermissions.CREATE_THREAD, ForumPermissions.CREATE_MESSAGE }; String [] permNames = new String [] { "READ", "FORUM_ADMIN", "MODERATOR", "CREATE_THREAD", "CREATE_MESSAGE" }; for (int i=0; i<permNames.length; i++) { int [] userList = factory.usersWithPermission(permTypes[i]); for (int j=0; j<userList.length; j++) { try { User user = profileManager.getUser(userList[j]); if (user.isAnonymous()) { out.write("<UserPermission usertype=\"GUEST\""); out.write(" permission=\"" + permNames[i] + "\"/>"); } else if (user.getID() == specialUser.getID()) { out.write("<UserPermission usertype=\"ANYUSER\""); out.write(" permission=\"" + permNames[i] + "\"/>"); } else { out.write("<UserPermission usertype=\"USER\" username=\""); out.write(user.getUsername()); out.write("\" permission=\"" + permNames[i] + "\"/>"); } } catch (UserNotFoundException unfe) { } } } out.write("</UserPermissionList>"); //Group List out.write("<GroupPermissionList>"); for (int i=0; i<permNames.length; i++) { int [] groupList = factory.groupsWithPermission(permTypes[i]); for (int j=0; j<groupList.length; j++) { try { Group group = profileManager.getGroup(groupList[j]); out.write("<GroupPermission groupname=\"" + group.getName() + "\""); out.write(" permission=\"" + permNames[i] + "\"/>"); } catch (GroupNotFoundException unfe) { } } } out.write("</GroupPermissionList>"); out.write("</Jive>"); } /** * Outputs a User object as an XML snippet. * * @param user the User to turn into XML. * @param out a Writer to write the XML out to. */ protected static void objectToXML(User user, Writer out) throws IOException, UnauthorizedException { if (user == null) { return; } out.write("<User><Username>"); out.write(StringUtils.escapeForXML(user.getUsername())); out.write("</Username><Password>"); out.write(StringUtils.escapeForXML(user.getPasswordHash())); boolean emailVisible = user.isEmailVisible(); boolean nameVisible = user.isNameVisible(); String name = user.getName(); out.write("</Password><Email visible=\"" + emailVisible + "\">"); out.write(StringUtils.escapeForXML(user.getEmail())); out.write("</Email>"); if (name != null) { out.write("<Name visible=\"" + nameVisible + "\">"); out.write(StringUtils.escapeForXML(name)); out.write("</Name>"); } //Properties Enumeration enum = user.propertyNames(); if (enum.hasMoreElements()) { out.write("<PropertyList>"); while (enum.hasMoreElements()) { String propName = (String)enum.nextElement(); String propValue = StringUtils.escapeForXML(user.getProperty(propName)); StringBuffer propBuff = new StringBuffer(); propBuff.append("<Property name=\"").append(propName).append("\" "); propBuff.append("value=\"").append(propValue).append("\"/>"); out.write(propBuff.toString()); } out.write("</PropertyList>"); } out.write("</User>"); } /** * Outputs a Group object as an XML snippet. * * @param group the Group to turn into XML. * @param out a Writer to write the XML out to. */ protected static void objectToXML(Group group, Writer out) throws IOException, UnauthorizedException { if (group == null) { return; } out.write("<Group><Groupname>"); out.write(StringUtils.escapeForXML(group.getName())); out.write("</Groupname><Description>"); out.write(StringUtils.escapeForXML(group.getDescription())); out.write("</Description>"); //It's bad of me to hardcode this. At least it won't be hard to maintain int specialUserID = 0; //Admin list Iterator iter = group.administrators(); if (iter.hasNext()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -