Tags

Here is the source code I promised in my previous post KeePass to PasswordSafe – Part 1 (https://twentymegahertz.wordpress.com/2016/03/06/keepass-to-passwordsafe-part-1/).

Please feel free to use this source code. I am releasing this code under Apache License 2.0.

The application needs an object, which is defined as follows.

/*
  EntryObject.java

  Code released under Apache License 2.0
*/

package k2p;

public class EntryObject
{
    public String title;
    public String notes;
    public String url;
    public String password;
    public String username;
}
/* end */

Here is the main program

/*
  KeePassToPasswordSafe.java

  Code released under Apache License 2.0
*/
package k2p;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;

public class KeePassToPasswordSafe
{
    public KeePassToPasswordSafe()
    {
    }
    
    public void convertXML(String inputFile, String outputFile)
    {
        SAXBuilder builder = new SAXBuilder();
        File xmlFile;
        PrintWriter outFile;
        int idx = 1;
        
        try {
            xmlFile = new File(inputFile);
            outFile = new PrintWriter(outputFile);
            
            Document document = (Document) builder.build(xmlFile);
            // use default implementation
            XPathFactory xFactory = XPathFactory.instance();
            XPathExpression expr = xFactory.compile("/KeePassFile/Root/Group/Group", Filters.element());
            List groupList = expr.evaluate(document);
            
            outFile.println("");
            outFile.println("");

            outFile.println("<passwordsafe"); //outFile.println("delimiter=\"»\""); outFile.println("delimiter=\"#\""); outFile.println("Database=\"temp.psafe3\""); outFile.println("ExportTimeStamp=\"2015-07-12T15:22:09\""); outFile.println("FromDatabaseFormat=\"3.13\""); outFile.println("WhoSaved=\"temp\""); outFile.println("WhatSaved=\"Password Safe V3.36\""); outFile.println("WhenLastSaved=\"2015-07-12T15:21:56\""); outFile.println("Database_uuid=\"8f0e6af5-3f18-474e-8865-d3f55adc76a3\""); outFile.println("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""); outFile.println("xsi:noNamespaceSchemaLocation=\"pwsafe.xsd\">");

            outFile.println(""); 
            outFile.println("");
            outFile.println("1");
            outFile.println("1");
            outFile.println("");
            outFile.println("");

            for ( Element group : groupList ) {
                //System.out.println("Group name: " + group.getChildText("Name"));
                List entryList = group.getChildren("Entry");
                for ( Element entry : entryList ) {
                    outFile.println("");
                    
                    outFile.println("\t");
                    //System.out.println("\tEntry: " + idx);
                    
                    idx++;
                    
                    List attributeList = entry.getChildren("String");
                    String keyName, value;
                    EntryObject eo = new EntryObject();
                    for ( Element attribute : attributeList ) {
                        //System.out.println("\t\tKey: " + string.getChildText("Key") + ", value: " + string.getChildText("Value").replace("\n", " x-x-x-x"));
                        keyName = attribute.getChildText("Key");
                        value = attribute.getChildText("Value");//.replace("\n", " x-x-x-x-x");
                    
                        if ( "Notes".equals(keyName) ) {
                            eo.notes = value;
                        } else if ( "Title".equals(keyName) ) {
                            eo.title = value;
                        } else if ( "Password".equals(keyName) ) {
                            eo.password = value;
                            if ( eo.password == null || "".equals(eo.password) ) {
                                eo.password = "1";
                            }
                        } else if ( "URL".equals(keyName) ) {
                            eo.url = value;
                            if ( eo.url == null || "".equals(eo.url) ) {
                                eo.url = "www";
                            }
                        } else if ( "UserName".equals(keyName) ) {
                            eo.username = value;
                            if ( eo.username == null || "".equals(eo.username) ) {
                                eo.username = "username";
                            }
                        }
                    }
                    
                    outFile.println("\t");
                    outFile.println("\t");
                    outFile.println("\t");
                    outFile.println("\t");
                    outFile.println("\t");
                    outFile.println("\t");
                    outFile.println("\t");
                    outFile.println("\t");
                    outFile.println("\t");
                    
                    outFile.println("");
                }
            }
            
            outFile.println("");
            
            outFile.close();
        } catch ( IOException ex ) {
            ex.printStackTrace();
        } catch ( JDOMException ex ) {
            ex.printStackTrace();
        }
    }
    
    public static void main(String[] args)
    {
        KeePassToPasswordSafe kp = new KeePassToPasswordSafe();
        kp.convertXML(args[0], args[1]);
    }
}
Advertisements