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

    classdef FileReader < handle
    %FILEREADER Create a file reader for AppDesigner files
    %   
    %   obj = FileReader(FILENAME) constructs a FileReader object to read
    %   AppDesigner specific files.  
    %
    % Methods:
    %   readMATLABCodeText     - Reads the MATLAB code in the file
    %   code to the file
    %   readAppDesignerData    - Reads the AppDesigner specific information,
    %                            AppWindow and MetaData
    %
    % Properties:
    %   FileName            - String specifying path and name of file 
    %   DefaultExtension    - Extension of AppDesigner files
    %
    % Example:
    %
    %   % Create FileReader object
    %   fileReader = FileReader('myApp.mlapp');
    %
    %   % Get MATLAB code from file
    %   matlabCode = readMATLABCodeText(fileReader);
    %
    %   % Get AppDesigner specific data from file
    %   appData = readAppDesignerData(fileReader)

    % Copyright 2013-2015 The MathWorks, Inc.

    
    properties (Access = private)
        DefaultExtension = '.mlapp';
    end
    properties
        FileName;
    end
    methods
        
        function obj = FileReader(fileName)
            narginchk(1, 1);
            validateattributes(fileName, ...
                {'char'}, ...
                {});
            obj.FileName = fileName;
            obj.validateFileExtensionForRead();
        end
        
        function matlabCode = readMATLABCodeText(obj)
            % READMATLABCODETEXT reads the MATLAB code stored in the
            % AppDesigner file and outputs the MATLAB code as a string.
            % matlabcode - MATLAB code readable from AppDesigner file
            
            [~, name, ext] = fileparts(obj.FileName);
            
            % Validate basic features of file location
            obj.validateFileExtensionForRead();
            obj.validateFileForRead();
            obj.validateFolderForRead();
            
            try
                matlabCode = appdesigner.internal.serialization.mexMLAPPReading(...
                    'readCode', obj.FileName);
            catch me
                error(message('MATLAB:appdesigner:appdesigner:LoadFailed', [name, ext]));
            end
        end
        
        function appData = readAppDesignerData(obj)                        
            % READAPPDESIGNERDATA extracts the AppDesigner specific data
            % from the AppDesigner
            % appData - the serialized app data
            
            [~, name, ext] = fileparts(obj.FileName);
            
            % Data from the MLAPP file will be copied to a local
            % .mat file in a temporary directory
            tempFileLocation = [tempname, '.mat'];

            % Validate basic features of file location
            obj.validateFileExtensionForRead();
            obj.validateFileForRead();
            obj.validateFolderForRead();
            
            % Copy data from file to the temporary location
            try
                appdesigner.internal.serialization.mexMLAPPReading(...
                    'getAppDesignerData', obj.FileName, tempFileLocation );
            catch me
                error(message('MATLAB:appdesigner:appdesigner:LoadFailed', [name, ext]));
            end
            tic
            while (exist(tempFileLocation, 'file')~=2 && toc < 10)
            end
            
            % EXIST returns 2 for files.
            if exist(tempFileLocation, 'file')~=2
                error(message('MATLAB:appdesigner:appdesigner:TransferOfDataFailed', obj.FileName));
            end
            
            % The temporary file will need to be deleted after read
            c = onCleanup(@()delete(tempFileLocation));

            % Disable load warning and capture current lastwarn state
            previousWarning = warning('off','MATLAB:ui:uifigure:UnsupportedAppDesignerFunctionality');
            [lastWarnStr, lastWarnId] = lastwarn;            
            
            % Load data from .mat file using MATLAB
            data = load(tempFileLocation);
            appData = data.appData;
            
            % Restore previous warning state
            warning(previousWarning);
            lastwarn(lastWarnStr, lastWarnId);
        end
    end  
    
     methods (Access = private)
        
        function obj = validateFileExtensionForRead(obj)
            % This function confirms that the file extension is consistent
            % with the default
            [path, name, ext] = fileparts(obj.FileName);
            if ~(isempty(ext) || strcmp(ext, obj.DefaultExtension))
            
                error(message('MATLAB:appdesigner:appdesigner:InvalidExtension', obj.DefaultExtension));
            end
            
            if isempty(ext) || ~strcmp(ext, obj.DefaultExtension)
                ext = obj.DefaultExtension;
            end

            obj.FileName = fullfile(path, [name, ext]);
                        
        end
        
        function obj = validateFileForRead(obj)
            % VALIDATEFILEFORREAD - Confirm that the file is valid by
            % verifying it has the correct parts
            [~, name, ext] = fileparts(obj.FileName);
            
            part = '/appdesigner/appModel.mat';
            hasPart = false;
            
            try
                hasPart = appdesigner.internal.serialization.mexMLAPPReading(...
                    'hasPart', obj.FileName, part);
            catch me
                error(message('MATLAB:appdesigner:appdesigner:LoadFailed', [name, ext]));
            end
            
            assert(hasPart, message('MATLAB:appdesigner:appdesigner:LoadFailed', [name, ext]));
        end
        
        function obj = validateFolderForRead(obj)
            % VALIDATEFOLDERFORREAD - Confirm that the directory is
            % readable and that the attributes can be obtained without
            % error
            
            [filePath, ~] = fileparts(obj.FileName);
            % Check basic attributes of the file location
            [stat,struc]=fileattrib(filePath);
            % if stat == 0, struc will contain the error message
            if stat == 0
                error(message('MATLAB:appdesigner:appdesigner:InvalidFolder',filePath, struc))
            end 
            
            if ~struc.UserRead
                error(message('MATLAB:appdesigner:appdesigner:NotReadableLocation', filePath));
            end
    
        end
    end
end