Package com.kellerkindt.scs.storage

Source Code of com.kellerkindt.scs.storage.FastFileShopStorage

/**
* ShowCaseStandalone
* Copyright (C) 2012 Kellerkindt <copyright at kellerkindt.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.kellerkindt.scs.storage;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;

import org.bukkit.World;
import org.xml.sax.SAXException;

import com.kellerkindt.scs.ShowCaseStandalone;
import com.kellerkindt.scs.interfaces.ShopHandler;
import com.kellerkindt.scs.interfaces.StorageHandler;
import com.kellerkindt.scs.interfaces.WorldProvider;
import com.kellerkindt.scs.internals.Storage;
import com.kellerkindt.scs.shops.Shop;
import com.kellerkindt.scs.utilities.Properties;

/**
* Only for loading 
* @author kellerkindt <michael at kellerkindt.com>
*/
public class FastFileShopStorage implements StorageHandler{
 
 
  private static final String  directory        = "ffs-storage";
  private static final String nodeNameRoot      = "scs-shop";
 
  private ShowCaseStandalone        scs;
  private SAXStorageParser        parser;
 
 
  public FastFileShopStorage (ShowCaseStandalone scs) throws TransformerConfigurationException, TransformerFactoryConfigurationError {
    this.scs  = scs;
    this.parser  = new SAXStorageParser();
   
    this.parser.setTransformerOutputProperty(OutputKeys.ENCODING,               Properties.dataEncoding/*"UTF-8"*//*"ISO-8859-1"*/);
    this.parser.setTransformerOutputProperty(OutputKeys.INDENT,               "yes");
    this.parser.setTransformerOutputProperty(OutputKeys.VERSION, "1.1");
    this.parser.setTransformerOutputProperty("{http://xml.apache.org/xslt}indent-amount",   "4");
  }
 
  /**
   * @param file
   * @param shop
   * @throws IOException
   * @throws TransformerException
   * @throws ParserConfigurationException
   * @throws XMLStreamException
   */
  private void saveStorageToXMLFile (File file, Storage storage) throws IOException, TransformerException, ParserConfigurationException, XMLStreamException {   
    // set the current storage version
    storage.setVersion(Properties.storageVersion);
   
    parser.saveStorage(storage, nodeNameRoot, file);
  }
 
  /**
   * @param file
   * @return
   * @throws SAXException
   * @throws IOException
   * @throws ParserConfigurationException
   */
  private Storage loadXMLStorageFromFile (File file) throws SAXException, IOException, ParserConfigurationException {
    Storage  storage  = parser.loadStorage(file);
   
    if (storage != null) {
     
      /*
       * Since version 6, a UUID is used
       * instead of a sha1 key, so delete
       * the old file
       * --> REWRITE NOW
       */
      if (storage.getVersion() <= 5) {
        try {
          saveStorageToXMLFile(file, storage);
        } catch (Exception e) {
          scs.log(Level.SEVERE, "Couldn't save new storage, trying again later", false);
        }
      }
     
      // only raw loading
//      storage = update(storage, storage.getVersion(), Properties.storageVersion);
     
      // reset, will be added to the list if an update was done
      storage.resetHasChanged();
    }
   
    return storage;
  }
 
  /**
   * @see com.kellerkindt.scs.interfaces.StorageHandler#flush()
   */
  @Override
  public void flush() throws IOException {
    // nothing to flush
  }
 
  /**
   * @see com.kellerkindt.scs.interfaces.StorageHandler#load(com.kellerkindt.scs.interfaces.ShopHandler)
   */
  @Override
  public void load(ShopHandler handler) throws IOException {
    File     dataDir = new File(scs.getDataFolder(), directory);
    List<Shop>  shops  = new ArrayList<Shop>();
   
    for (File file : dataDir.listFiles()) {
      try {
       
        Storage storage = loadXMLStorageFromFile(file);
        shops.add(ShopImporter.loadShop(storage, new WorldProvider() {
         
          @Override
          public World getWorld(String name) {
            return scs.getServer().getWorld(name);
          }
         
          @Override
          public World getWorld(UUID uuid) {
            return scs.getServer().getWorld(uuid);
          }
        }, scs.getServer().getVersion()));       
       
      } catch (Exception e) {
        // dirty...
        e.printStackTrace();
      }
    }
   
    // add them all
    handler.addAll(shops, true);
  }
 
  /**
   * @see com.kellerkindt.scs.interfaces.StorageHandler#save(com.kellerkindt.scs.interfaces.ShopHandler)
   */
  @Override
  public void save(ShopHandler handler) throws IOException {
    throw new IOException("NOT SUPPORTED ANYMORE");
  }
}
TOP

Related Classes of com.kellerkindt.scs.storage.FastFileShopStorage

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.