Package org.jampa.model.radio.io

Source Code of org.jampa.model.radio.io.RadioWriter

/*
* Jampa
* Copyright (C) 2008-2009 J. Devauchelle and contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*/

package org.jampa.model.radio.io;

import java.io.File;
import java.io.FileWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.jampa.logging.Log;
import org.jampa.model.radio.CategoryRadioItem;
import org.jampa.model.radio.IRadioItem;
import org.jampa.model.radio.RadioItem;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class RadioWriter {
 
  private String _fileName;
  private CategoryRadioItem _rootCategory;
 
  public RadioWriter(String fileName, CategoryRadioItem rootCategory) {
    _fileName = fileName;
    _rootCategory = rootCategory;
  }
 
  private void writeCategory(Document document, Element parentElement, List<IRadioItem> itemList) {
    if (itemList != null) {
      IRadioItem item;
     
      for (int i = 0; i < itemList.size(); i++) {
        item = itemList.get(i);
       
        try {
       
          if (item instanceof CategoryRadioItem) {
            Element categoryElement = document.createElement(RadioXmlConstants.CATEGORY_TAG);
            Element categoryNameElement = document.createElement(RadioXmlConstants.CATEGORY_NAME_TAG);
            categoryNameElement.setTextContent(URLEncoder.encode(item.getName(), "UTF-8"));
            categoryElement.appendChild(categoryNameElement);

            parentElement.appendChild(categoryElement);

            writeCategory(document, categoryElement, item.getChildren());

          } else if (item instanceof RadioItem) {
            Element radioElement = document.createElement(RadioXmlConstants.RADIO_TAG);
           
            radioElement.setAttribute(RadioXmlConstants.RADIO_IS_FAVORITE_ATTR, Boolean.toString(((RadioItem) item).isFavorite()));

            Element radioNameElement = document.createElement(RadioXmlConstants.RADIO_NAME_TAG);
            radioNameElement.setTextContent(URLEncoder.encode(item.getName(), "UTF-8"));

            Element radioUrlElement = document.createElement(RadioXmlConstants.RADIO_URL_TAG);
            radioUrlElement.setTextContent(URLEncoder.encode(((RadioItem) item).getUrl(), "UTF-8"));

            radioElement.appendChild(radioNameElement);
            radioElement.appendChild(radioUrlElement);

            parentElement.appendChild(radioElement);
          }
        } catch (UnsupportedEncodingException e) {
          Log.getInstance(RadioWriter.class).warn("Unable to encode value for item: " + item.getName());
        }
      }
    }
  }
 
  public void write() {
   
    Log.getInstance(RadioWriter.class).debug("Writing radio list to :" + _fileName);
   
    Document document = null;
   
    try {
     
      DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = builderFactory.newDocumentBuilder();

      document = builder.newDocument();

      document.setXmlVersion("1.0"); //$NON-NLS-1$
      document.setXmlStandalone(true);
     
      Element radioListElement = document.createElement(RadioXmlConstants.RADIOLIST_TAG);
      radioListElement.setAttribute(RadioXmlConstants.XMLNS_ATTRIBUTE, RadioXmlConstants.XMLNS_ATTRIBUTE_VALUE);
      radioListElement.setAttribute(RadioXmlConstants.VERSION_ATTRIBUTE, "1");
     
      writeCategory(document, radioListElement, _rootCategory.getChildren());
     
      document.appendChild(radioListElement);
     
      DOMSource domSource = new DOMSource(document);
      FileWriter writer = new FileWriter(new File(_fileName));
      StreamResult result = new StreamResult(writer);
      TransformerFactory tf = TransformerFactory.newInstance();
      Transformer transformer = tf.newTransformer();
      transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
      //transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
      transformer.transform(domSource, result);    
     
      writer.flush();
      writer.close();
     
     
    } catch (Exception e) {
      Log.getInstance(RadioWriter.class).error("Error while writting radio file: " + _fileName + " (" + e.getStackTrace() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }
  }

}
TOP

Related Classes of org.jampa.model.radio.io.RadioWriter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.