www.gusucode.com > appdesigner工具箱matlab源码程序 > appdesigner/+appdesigner/+internal/+serialization/FileWriter.m

    classdef FileWriter < handle
    %FILEWRITER Create a file writer for AppDesigner files
    %
    %   obj = FileWriter(FILENAME) constructs a FileWriter object to write
    %   AppDesigner specific files.
    %
    % Methods:
    %   writeMATLABCodeText     - Creates the file and writes the MATLAB
    %   code to the file
    %   writeAppDesignerData    - Appends the file with the AppDesigner
    %                             specific information
    %
    % Properties:
    %   FileName            - String specifying path and name of file
    %   DefaultExtension   - Extension of AppDesigner files
    %
    % Example:
    %
    %   % Create FileWriter object
    %   fileWriter = FileWriter('myApp.mlapp');
    %
    %   % Write MATLAB code from file
    %   matlabCode = 'function myfunction()';
    %   writeMATLABCodeText(fileWriter, matlabCode);
    %
    %   % Get AppDesigner specific data from file
    %   writeAppDesignerData(fileWriter, appData)

    % Copyright 2013-2015 The MathWorks, Inc.
    
    properties (Access = private)
        DefaultExtension = '.mlapp';
    end
    
    properties
        FileName;
    end
    
    methods
        
        function obj = FileWriter(fileName)
            narginchk(1, 1);
            validateattributes(fileName, ...
                {'char'}, ...
                {});
            obj.FileName = fileName;
            obj.validateFileExtensionForWrite();
            
        end
        function  writeMATLABCodeText(obj, matlabCode)
            % WRITEMATLABCODETEXT writes MATLAB code and stores it in the
            % AppDesigner file as a string
            % matlabcode - MATLAB code to write to the AppDesigner file
            
            [~, name, ext] = fileparts(obj.FileName);
            
            % Validate basic features of file location
            obj.validateFileExtensionForWrite();
            obj.validateFileForWrite();
            obj.validateFolderForWrite();
            
            % Create file and write MATLAB code to file
            try
                appdesigner.internal.serialization.mexMLAPPWriting(...
                    'writeCode', obj.FileName, matlabCode);
            catch me
                error(message('MATLAB:appdesigner:appdesigner:SaveFailed', [name, ext]));
            end
        end
        
        function writeAppDesignerData(obj, appData)
            % WRITEAPPDESIGNERDATA writes the AppDesigner specific data
            % from the AppDesigner
            % appData - the App Designer data to serialize
            
            [~, name, ext] = fileparts(obj.FileName);
            
            % Check if file was created
            if exist(obj.FileName, 'file') ~= 2, ...
                error(message('MATLAB:appdesigner:appdesigner:SaveFailed', [name, ext]));
            end

            % Data will be saved using MATLAB to a temporary file, then
            % written to the AppDesigner from that file
            
            tempFileLocation = [tempname, '.mat'];
            
            % Disable save warning and capture current lastwarn state
            previousWarning = warning('off','MATLAB:ui:uifigure:UnsupportedAppDesignerFunctionality');
            [lastWarnStr, lastWarnId] = lastwarn;
            
            % Save the appWindow and metaData to a temporary .mat file
            save(tempFileLocation, 'appData');
            
            % Restore previous warning state
            warning(previousWarning);
            lastwarn(lastWarnStr, lastWarnId);
            
            % Check basic features of file location
            obj.validateFileExtensionForWrite();
            obj.validateFileForWrite();
            obj.validateFolderForWrite();
                        
            % write contents of mat file into MLAPP file
            try
                appdesigner.internal.serialization.mexMLAPPWriting(...
                    'setAppDesignerData', obj.FileName, tempFileLocation);
            catch me
                error(message('MATLAB:appdesigner:appdesigner:SaveFailed', [name, ext]));
            end
            
            % The temporary file will need to be deleted after read
            c = onCleanup(@()delete(tempFileLocation));
        end
    end
    methods (Access = private)
        
        function obj = validateFileExtensionForWrite(obj)
            % This function confirms that the file extension is consistent
            % with the default
            [path, name, ext] = fileparts(obj.FileName);
            
            if isempty(ext)
                ext = obj.DefaultExtension;
            end
            
            % Check if file has correct extension
            if ~strcmp(ext, obj.DefaultExtension)
                error(message('MATLAB:appdesigner:appdesigner:InvalidExtension', obj.DefaultExtension));
            end            
            
            obj.FileName = fullfile(path, [name, ext]);
        end
        
        function obj = validateFileForWrite(obj)
            [~, name, ext] = fileparts(obj.FileName);
        
            % Check if file already exists and is readonly
            if exist(obj.FileName, 'file') == 2
                [~, fileAttributes] = fileattrib(obj.FileName);
                if ~fileAttributes.UserWrite
                    error(message('MATLAB:appdesigner:appdesigner:ReadOnlyFile', [name, ext]));
                end
            end
        end
        
        function obj = validateFolderForWrite(obj)
            path = fileparts(obj.FileName);
            
            % Assert that the path exists
            if exist(path, 'dir') ~= 7
                error(message('MATLAB:appdesigner:appdesigner:NotWritableLocation', obj.FileName));
            end
            
            
            % create a random folder name so no existing folders are affected
            randomNumber = floor(rand*1e12);
            testDirPrefix = 'appDesignerTempData_';
            testDir = [testDirPrefix, num2str(randomNumber)];
            while exist(testDir, 'dir')
                % The folder name should not match an existing folder
                % in the directory
                randomNumber = randomNumber + 1;
                testDir = [testDirPrefix, num2str(randomNumber)];
            end
            
            % Attempt to write a folder in the save location
            [isWritable,~,~] = mkdir(path, testDir);
            if ~isWritable
                error(message('MATLAB:appdesigner:appdesigner:NotWritableLocation', obj.FileName));
            end
            
            [status,~,~] = rmdir(fullfile(path, testDir));
            if status ~=1
                warning(['Temporary folder %s could not be ', ...
                    'deleted.  Please delete manually.'], ...
                    fullfile(path, testDir) )
            end
        end
    end
end