001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.xml.export;
019:
020: import java.io.BufferedWriter;
021: import java.io.IOException;
022: import java.io.Writer;
023: import java.util.Collection;
024: import java.util.Iterator;
025:
026: import de.finix.contelligent.core.security.ContelligentPrincipal;
027: import de.finix.contelligent.core.security.PrincipalData;
028: import de.finix.contelligent.core.security.Role;
029: import de.finix.contelligent.core.security.User;
030:
031: public class PrincipalDataXMLWriter {
032:
033: private Writer writer;
034:
035: private PrincipalDataXMLPolicy policy;
036:
037: public PrincipalDataXMLWriter(Writer writer,
038: PrincipalDataXMLPolicy policy) {
039: if (writer instanceof BufferedWriter) {
040: this .writer = writer;
041: } else {
042: this .writer = new BufferedWriter(writer);
043: }
044: this .policy = policy;
045: }
046:
047: /**
048: *
049: * @param principals
050: * collection of ContelligentPrincipal instances
051: * @param groupId
052: * @throws IOException
053: */
054: public void writePrincipals(Collection principals, String groupId)
055: throws IOException {
056: policy.writeXMLHeader(writer, groupId);
057:
058: Iterator iterator = principals.iterator();
059:
060: while (iterator.hasNext()) {
061: ContelligentPrincipal principal = (ContelligentPrincipal) iterator
062: .next();
063: if (principal instanceof User) {
064: User user = (User) principal;
065: policy.writeUser(user.getName(), user.getAlias(), user
066: .getDisplayName(), null, user.getRoles(),
067: writer);
068: } else {
069: Role role = (Role) principal;
070: policy.writeRole(role.getName(), role.getDisplayName(),
071: writer);
072: }
073: }
074:
075: policy.writeXMLFooter(writer);
076: }
077:
078: /**
079: *
080: * @param principals
081: * collection of PrincipalData instances
082: * @param groupId
083: * @throws IOException
084: */
085: public void writePrincipalData(Collection principals, String groupId)
086: throws IOException {
087: policy.writeXMLHeader(writer, groupId);
088:
089: Iterator iterator = principals.iterator();
090:
091: while (iterator.hasNext()) {
092: PrincipalData principal = (PrincipalData) iterator.next();
093: if (principal.isUser()) {
094: policy.writeUser(principal.getId(), principal
095: .getAliasId(), principal.getDisplayName(),
096: principal.getPassword(), principal.getRoles(),
097: writer);
098: } else {
099: policy.writeRole(principal.getId(), principal
100: .getDisplayName(), writer);
101: }
102: }
103:
104: policy.writeXMLFooter(writer);
105: }
106:
107: public void flush() throws IOException {
108: writer.flush();
109: }
110:
111: public void close() throws IOException {
112: writer.close();
113: }
114: }
|