static void write(OutputStream stream) throws IOException {
XmlWriter out = new XmlWriter(stream);
// Root element, add the encryption method used
XmlAttributes attributes = new XmlAttributes();
attributes.add(ATTRIBUTE_ENCRYPTION, WEAK_ENCRYPTION_METHOD);
// Version the file
attributes.add(ATTRIBUTE_VERSION, RuntimeConstants.VERSION);
out.startElement(ELEMENT_ROOT, attributes);
out.println();
Iterator<CredentialsMapping> iterator = CredentialsManager.getPersistentCredentialMappings().iterator();
CredentialsMapping credentialsMapping;
FileURL realm;
Enumeration<String> propertyKeys;
String name;
while(iterator.hasNext()) {
credentialsMapping = iterator.next();
realm = credentialsMapping.getRealm();
// Start credentials element
out.startElement(ELEMENT_CREDENTIALS);
out.println();
// Write URL
out.startElement(ELEMENT_URL);
out.writeCData(realm.toString(false));
out.endElement(ELEMENT_URL);
Credentials credentials = credentialsMapping.getCredentials();
// Write login
out.startElement(ELEMENT_LOGIN);
out.writeCData(credentials.getLogin());
out.endElement(ELEMENT_LOGIN);
// Write password (XOR encrypted)
out.startElement(ELEMENT_PASSWORD);
out.writeCData(XORCipher.encryptXORBase64(credentials.getPassword()));
out.endElement(ELEMENT_PASSWORD);
// Write properties, each property is stored in a separate 'property' element
propertyKeys = realm.getPropertyNames();
while(propertyKeys.hasMoreElements()) {
name = propertyKeys.nextElement();
attributes = new XmlAttributes();
attributes.add(ATTRIBUTE_NAME, name);
attributes.add(ATTRIBUTE_VALUE, realm.getProperty(name));
out.startElement(ELEMENT_PROPERTY, attributes);
out.endElement(ELEMENT_PROPERTY);
}
// End credentials element