it.isislab.floasys.core.security.users.UserAccountXMLParser.java Source code

Java tutorial

Introduction

Here is the source code for it.isislab.floasys.core.security.users.UserAccountXMLParser.java

Source

/*    
 *   Copyright (c) 2012-2014, Dipartimento Informatica, Universit di Salerno (Italy) 
 *   [vitsca@dia.unisa.it, dpirozzi@unisa.it]
 *     
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *      
 *   Contributors:
 *      ISISLab, Dipartimento Informatica, Universit di Salerno (Italy) 
 *      [vitsca@dia.unisa.it, dpirozzi@unisa.it]
 */

package it.isislab.floasys.core.security.users;

import it.isislab.floasys.core.security.exceptions.CannotParseException;

import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
 * Parse XML file with user accounts.
 * @author Donato Pirozzi - dpirozzi@unisa.it
 */
public class UserAccountXMLParser {

    public static final String EL_USERACCOUNT = "user";
    public static final String EL_USERNAME = "username";
    public static final String EL_PASSWORD = "password";
    public static final String EL_ACTIVATIONCODE = "activationcode";
    public static final String EL_ACTIVATED = "activated";

    public UserAccountXMLParser() {
    }//EndConstructor.

    public List<UserAccount> parse(InputStream is) throws CannotParseException {
        try {
            Document doc = new SAXBuilder().build(is);
            Element rootNode = doc.getRootElement();
            List<Element> elUsers = rootNode.getChildren(EL_USERACCOUNT);

            for (Element elUser : elUsers) {
                UserAccount userAccount = new UserAccount();
                List<Element> children = elUser.getChildren();
                for (Element child : children) {
                    String childName = child.getName();
                    if (childName.equals(EL_USERNAME)) {

                    } else if (childName.equals(EL_PASSWORD)) {

                    } else if (childName.equals(EL_ACTIVATIONCODE)) {

                    } else if (childName.equals(EL_ACTIVATED)) {

                    }
                } //EndFor.
            }
        } catch (Exception e) {
            throw new CannotParseException(e);
        }

        return null;
    }//EndMethod.

    /**
     * Write another account to the XML file.
     * @param is
     * @param os
     * @param account
     * @throws CannotParseException
     */
    public void write(InputStream is, OutputStream os, UserAccount account) throws CannotParseException {
        try {
            //Read the existing file.
            Document doc = new SAXBuilder().build(is);
            Element rootNode = doc.getRootElement();

            //Create a new user element.
            Element elAccount = new Element(EL_USERACCOUNT);
            Element elUsername = new Element(EL_USERNAME);
            Element elPassword = new Element(EL_PASSWORD);
            Element elActivationCode = new Element(EL_ACTIVATIONCODE);
            Element elActivated = new Element(EL_ACTIVATED);

            elAccount.addContent(elUsername);
            elAccount.addContent(elPassword);
            elAccount.addContent(elActivationCode);
            elAccount.addContent(elActivated);

            //Add the account to the root element.
            rootNode.addContent(elAccount);

            //Set the root for document.
            doc.setContent(rootNode);

            //Write the file content.
            XMLOutputter outputter = new XMLOutputter();
            outputter.setFormat(Format.getPrettyFormat());
            outputter.output(doc, os);
        } catch (Exception ex) {
            throw new CannotParseException(ex);
        }
    }//EndMethod.

}//EndClass.