📄 xmlbuilder.java
字号:
out.write("<AdministratorList>"); User user = (User)iter.next(); if (user.isAnonymous()) { out.write("<Username type=\"GUEST\"/>"); } else if (user.getID() == specialUserID) { out.write("<Username type=\"ANYUSER\"/>"); } else { out.write("<Username type=\"USER\">"); out.write(user.getUsername()); out.write("</Username>"); } out.write("</AdministratorList>"); } //Member list iter = group.members(); if (iter.hasNext()) { out.write("<MemberList>"); User user = (User)iter.next(); if (user.isAnonymous()) { out.write("<Username type=\"GUEST\"/>"); } else if (user.getID() == specialUserID) { out.write("<Username type=\"ANYUSER\"/>"); } else { out.write("<Username type=\"USER\">"); out.write(user.getUsername()); out.write("</Username>"); } out.write("</MemberList>"); } out.write("</Group>"); } /** * Outputs a Forum object as an XML snippet. * * @param forum the Forum to turn into XML. * @param out a Writer to write the XML out to. */ protected static void objectToXML(Forum forum, ProfileManager profileManager, Writer out) throws IOException, UnauthorizedException { if (forum == null) { return; } out.write("<Forum><Name>"); out.write(StringUtils.escapeForXML(forum.getName())); out.write("</Name><Description>"); out.write(StringUtils.escapeForXML(forum.getDescription())); out.write("</Description>"); out.write("<CreationDate>"); out.write(formatter.format(forum.getCreationDate())); out.write("</CreationDate><ModifiedDate>"); out.write(formatter.format(forum.getModifiedDate())); out.write("</ModifiedDate>"); //FilterList /* ForumMessageFilter [] filterList = forum.getForumMessageFilters(); if (filterList.length > 0) { out.write("<FilterList>"); for (int i=0; i<filterList.length; i++) { ForumMessageFilter filter = filterList[i]; out.write("<Filter><Name>"); out.write(filter.getName()); out.write("</Name><Classname>"); out.write(filter.getClass().getName()); out.write("</Classname><Index>" + i + "</Index>"); //Hanlde message properties Enumeration enum = filter.filterPropertyNames(); if (enum.hasMoreElements()) { out.write("<PropertyList>"); while (enum.hasMoreElements()) { String propName = (String)enum.nextElement(); String propValue = StringUtils.escapeForXML(filter.getFilterProperty(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("</Filter>"); } out.write("</FilterList>"); }*/ //Permissions List out.write("<PermissionList>"); //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 = forum.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 = forum.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("</PermissionList>"); //Forum properties Enumeration enum = forum.propertyNames(); if (enum.hasMoreElements()) { out.write("<PropertyList>"); while (enum.hasMoreElements()) { String propName = (String)enum.nextElement(); String propValue = StringUtils.escapeForXML(forum.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>"); } //Print out the threads under the forum Iterator iter = forum.threads(); if (iter.hasNext()) { out.write("<ThreadList>"); while (iter.hasNext()) { objectToXML((ForumThread)iter.next(), out); } out.write("</ThreadList>"); } out.write("</Forum>"); } /** * Outputs a ForumThread object as XML. * * @param thread the ForumThread to turn into XML. * @param out a Writer to write the XML out to. */ protected static void objectToXML(ForumThread thread, Writer out) throws IOException { if (thread == null) { return; } String threadName = thread.getName(); if( threadName == null ) { threadName = ""; } out.write("<Thread><Name>"); out.write(StringUtils.escapeForXML(threadName)); out.write("</Name>"); out.write("<CreationDate>"); out.write(formatter.format(thread.getCreationDate())); out.write("</CreationDate><ModifiedDate>"); out.write(formatter.format(thread.getModifiedDate())); out.write("</ModifiedDate>"); //Print out the messages under the thread objectToXML(thread.getRootMessage(), out); out.write("</Thread>"); } /** * Outputs a ForumMessage object as an XML snippet. Child messages are * written out recursively as part of each message XML element. Therefore, * calling this method on the root message of a thread will print out all * messages in the thread. * * @param message the ForumMessage to turn into XML. * @param out a Writer to write the XML out to. */ protected static void objectToXML(ForumMessage message, Writer out) throws IOException { if (message == null) { return; } String subject = message.getSubject(); if( subject == null ) { subject = ""; } String body = message.getUnfilteredBody(); if( body == null ) { body = ""; } out.write("<Message><Subject>"); out.write(StringUtils.escapeForXML(subject)); out.write("</Subject><Body>"); out.write(StringUtils.escapeForXML(body)); out.write("</Body>"); //Only print out a username element if the message isn't anonymous. if (!message.isAnonymous()) { out.write("<Username>"); out.write(StringUtils.escapeForXML(message.getUser().getUsername())); out.write("</Username>"); } out.write("<CreationDate>"); out.write(formatter.format(message.getCreationDate())); out.write("</CreationDate><ModifiedDate>"); out.write(formatter.format(message.getModifiedDate())); out.write("</ModifiedDate>"); //Message properties Iterator iter = message.propertyNames(); if (iter.hasNext()) { out.write("<PropertyList>"); while (iter.hasNext()) { String propName = (String)iter.next(); String propValue = StringUtils.escapeForXML(message.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>"); } //Print out any sub-messages ForumThread thread = message.getForumThread(); TreeWalker walker = thread.treeWalker(); int childCount = walker.getChildCount(message); if (childCount > 0) { out.write("<MessageList>"); for (int i=0; i<childCount; i++) { ForumMessage childMessage = walker.getChild(message, i); objectToXML(childMessage, out); } out.write("</MessageList>"); } out.write("</Message>"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -