alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.xml.core.sync;

import java.util.Date;
import java.io.*;
import java.net.URL;

import org.xml.sax.*;

import org.openide.filesystems.*;
import org.openide.loaders.DataObject;

import org.netbeans.modules.xml.core.*;
import org.netbeans.modules.xml.core.lib.*;
import org.netbeans.modules.xml.core.sync.*;

/**
 * This representation stays for external representation at filesystem.
 * It may be modified externally at any time!
 *
 * @author  Petr Kuzel
 * @version 
 */
public class FileRepresentation extends SyncRepresentation {

    private final DataObject dataObject;
    private Date lastSave;
    
    /** Creates new FileRepresentation */
    public FileRepresentation (DataObject dataObject, Synchronizator sync) {
        super (sync);

        this.dataObject = dataObject;

        lastSave = getFileObject().lastModified();        
    }


    /**
     */
    private FileObject getFileObject () {
        return dataObject.getPrimaryFile();
    }


    /**
     * Does this representation wraps given model?
     */
    public boolean represents(Class type) {
        return FileObject.class.isAssignableFrom(type);
    }

    /**
     * Update the representation without marking it as modified.
     * User MUST use SaveCookie explicitly to update it.
     */
    public void update(Object change) {        
//            SaveCookie save = (SaveCookie) getCookie(SaveCookie.class);
//            if (save != null) {
//                save.save();
//                lastUpdate = getFileObject().lastModified();
//            } else {
//                //!!! is is modified and does not have save cookie
//                // introduce prepare() + commit() ??
//            }
    }

    /**
     * Return accepted update class
     */
    public Class getUpdateClass() {
        return null;
    }

    /**
     * Is this representation modified since last sync?
     */
    public boolean isModified() {
        return lastSave.getTime() < getFileObject().lastModified().getTime();
    }

    /**
     * @return select button diplay name used during notifying concurent modification
     * conflict.
     */
    public String getDisplayName() {
        return Util.THIS.getString ("PROP_File_representation");
    }

    /**
     * Return modification passed as update parameter to all slave representations.
     */
    public Object getChange(Class type) {

        if (type == null || type.isAssignableFrom(InputSource.class)) {
            try {
                //!!! try to autodectect encoding since these routines in Xerces2 are bad
                InputSource source = new InputSource(getFileObject().getURL().toExternalForm());
                InputStream in = new BufferedInputStream(getFileObject().getInputStream());
                String encoding = EncodingHelper.detectEncoding(in);
                if ( encoding == null ) {
                    encoding = "UTF8"; //!!! // NOI18N
                }
                source.setCharacterStream(new InputStreamReader(in, encoding));
                return source;
                
            } catch (IOException ex) {
                return null;
            }
        } else if (type.isAssignableFrom(InputStream.class)) {
            try {
                return getFileObject().getInputStream();
            } catch (IOException ex) {
                return null;
            }
        }

        throw new RuntimeException("FileRepresentation does not support: " + type); // NOI18N
    }

    public int level() {
        return 0;
    }

    // Listents for extarnal modification and deletion
    private class FileListener extends FileChangeAdapter {

        /** Fired when a file is changed.
         * @param fe the event describing context where action has taken place
         */
        public void fileChanged (FileEvent fe) {
            getSynchronizator().representationChanged(FileObject.class);
        }

    } // end of inner class FileListener
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.