This project has retired. For details please refer to its
Attic page.
RedbackKeyManagementJdoStaxWriter xref
1
2
3
4
5
6 package org.apache.archiva.redback.keys.jdo.io.stax;
7
8
9
10
11
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.io.StringWriter;
15 import java.io.Writer;
16 import java.text.DateFormat;
17 import java.util.Iterator;
18 import java.util.Locale;
19 import java.util.jar.Manifest;
20 import javax.xml.stream.*;
21 import org.apache.archiva.redback.keys.jdo.AuthenticationKeyDatabase;
22 import org.apache.archiva.redback.keys.jdo.JdoAuthenticationKey;
23
24
25
26
27
28
29 public class RedbackKeyManagementJdoStaxWriter
30 {
31
32
33
34
35
36
37
38
39 private int curId;
40
41
42
43
44 private java.util.Map idMap;
45
46
47
48
49
50
51 public RedbackKeyManagementJdoStaxWriter()
52 {
53 idMap = new java.util.HashMap();
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public void write( Writer writer, AuthenticationKeyDatabase authenticationKeyDatabase )
70 throws java.io.IOException, XMLStreamException
71 {
72 XMLOutputFactory factory = XMLOutputFactory.newInstance();
73 boolean supportWindowsLineEndings = false;
74 if ( factory.isPropertySupported( "com.ctc.wstx.outputEscapeCr" ) )
75 {
76 factory.setProperty( "com.ctc.wstx.outputEscapeCr", Boolean.FALSE );
77 supportWindowsLineEndings = true;
78 }
79 if ( factory.isPropertySupported( "org.codehaus.stax2.automaticEmptyElements" ) )
80 {
81 factory.setProperty( "org.codehaus.stax2.automaticEmptyElements", Boolean.FALSE );
82 }
83 IndentingXMLStreamWriter serializer = new IndentingXMLStreamWriter( factory.createXMLStreamWriter( writer ) );
84 if ( supportWindowsLineEndings )
85 {
86 serializer.setNewLine( serializer.getLineSeparator() );
87 }
88 serializer.writeStartDocument( authenticationKeyDatabase.getModelEncoding(), "1.0" );
89 writeAuthenticationKeyDatabase( authenticationKeyDatabase, "authenticationKeyDatabase", serializer );
90 serializer.writeEndDocument();
91 }
92
93
94
95
96
97
98
99
100
101 public void write( OutputStream stream, AuthenticationKeyDatabase authenticationKeyDatabase )
102 throws java.io.IOException, XMLStreamException
103 {
104 XMLOutputFactory factory = XMLOutputFactory.newInstance();
105 boolean supportWindowsLineEndings = false;
106 if ( factory.isPropertySupported( "com.ctc.wstx.outputEscapeCr" ) )
107 {
108 factory.setProperty( "com.ctc.wstx.outputEscapeCr", Boolean.FALSE );
109 supportWindowsLineEndings = true;
110 }
111 if ( factory.isPropertySupported( "org.codehaus.stax2.automaticEmptyElements" ) )
112 {
113 factory.setProperty( "org.codehaus.stax2.automaticEmptyElements", Boolean.FALSE );
114 }
115 IndentingXMLStreamWriter serializer = new IndentingXMLStreamWriter( factory.createXMLStreamWriter( stream, authenticationKeyDatabase.getModelEncoding() ) );
116 if ( supportWindowsLineEndings )
117 {
118 serializer.setNewLine( serializer.getLineSeparator() );
119 }
120 serializer.writeStartDocument( authenticationKeyDatabase.getModelEncoding(), "1.0" );
121 writeAuthenticationKeyDatabase( authenticationKeyDatabase, "authenticationKeyDatabase", serializer );
122 serializer.writeEndDocument();
123 }
124
125
126
127
128
129
130
131
132
133
134 private void writeAuthenticationKeyDatabase( AuthenticationKeyDatabase authenticationKeyDatabase, String tagName, XMLStreamWriter serializer )
135 throws java.io.IOException, XMLStreamException
136 {
137 if ( authenticationKeyDatabase != null )
138 {
139 serializer.writeStartElement( tagName );
140 if ( ( authenticationKeyDatabase.getKeys() != null ) && ( authenticationKeyDatabase.getKeys().size() > 0 ) )
141 {
142 serializer.writeStartElement( "keys" );
143 for ( Iterator iter = authenticationKeyDatabase.getKeys().iterator(); iter.hasNext(); )
144 {
145 JdoAuthenticationKey o = (JdoAuthenticationKey) iter.next();
146 writeJdoAuthenticationKey( o, "key", serializer );
147 }
148 serializer.writeEndElement();
149 }
150 serializer.writeEndElement();
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163 private void writeJdoAuthenticationKey( JdoAuthenticationKey jdoAuthenticationKey, String tagName, XMLStreamWriter serializer )
164 throws java.io.IOException, XMLStreamException
165 {
166 if ( jdoAuthenticationKey != null )
167 {
168 serializer.writeStartElement( tagName );
169 if ( jdoAuthenticationKey.getKey() != null )
170 {
171 serializer.writeStartElement( "key" );
172 serializer.writeCharacters( jdoAuthenticationKey.getKey() );
173 serializer.writeEndElement();
174 }
175 if ( jdoAuthenticationKey.getForPrincipal() != null )
176 {
177 serializer.writeStartElement( "forPrincipal" );
178 serializer.writeCharacters( jdoAuthenticationKey.getForPrincipal() );
179 serializer.writeEndElement();
180 }
181 if ( jdoAuthenticationKey.getPurpose() != null )
182 {
183 serializer.writeStartElement( "purpose" );
184 serializer.writeCharacters( jdoAuthenticationKey.getPurpose() );
185 serializer.writeEndElement();
186 }
187 if ( jdoAuthenticationKey.getDateCreated() != null )
188 {
189 serializer.writeStartElement( "dateCreated" );
190 serializer.writeCharacters( String.valueOf( jdoAuthenticationKey.getDateCreated().getTime() ) );
191 serializer.writeEndElement();
192 }
193 if ( jdoAuthenticationKey.getDateExpires() != null )
194 {
195 serializer.writeStartElement( "dateExpires" );
196 serializer.writeCharacters( String.valueOf( jdoAuthenticationKey.getDateExpires().getTime() ) );
197 serializer.writeEndElement();
198 }
199 serializer.writeEndElement();
200 }
201 }
202
203 }