Source Code Cross Referenced for FileSave.java in  » XML-UI » xui32 » com » xoetrope » service » file » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML UI » xui32 » com.xoetrope.service.file 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.xoetrope.service.file;
002:
003:        import java.io.BufferedReader;
004:        import java.io.BufferedWriter;
005:        import java.io.File;
006:        import java.io.FileInputStream;
007:        import java.io.FileOutputStream;
008:        import java.io.IOException;
009:        import java.io.InputStreamReader;
010:        import java.io.OutputStreamWriter;
011:        import net.xoetrope.optional.service.ServiceContext;
012:
013:        import net.xoetrope.optional.service.ServiceProxy;
014:        import net.xoetrope.optional.service.ServiceProxyArgs;
015:        import net.xoetrope.optional.service.ServiceProxyException;
016:
017:        /**
018:         * This ServiceProxy class enables the saving and opening of files on a local
019:         * filesystem.
020:         *
021:         * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
022:         * the GNU Public License (GPL), please see license.txt for more details. If
023:         * you make commercial use of this software you must purchase a commercial
024:         * license from Xoetrope.</p>
025:         * <p> $Revision: 1.13 $</p>
026:         */
027:        public class FileSave extends ServiceProxy {
028:            /**
029:             * The mode of the call. Either ARG_VALUE_OPEN or ARG_VALUE_SAVE
030:             */
031:            public static final String ARG_NAME_MODE = "mode";
032:
033:            /**
034:             * The name of the file being written or opened
035:             */
036:            public static final String ARG_NAME_FILENAME = "filename";
037:
038:            /**
039:             * The path to the directory where the file is being written or opened
040:             */
041:            public static final String ARG_NAME_DIR = "filesavedirectory";
042:
043:            /**
044:             * The contents to be written if saving or returned if opening
045:             */
046:            public static final String ARG_NAME_CONTENTS = "contents";
047:
048:            /**
049:             * Set if a file with the same name exists and should be overwritten
050:             */
051:            public static final String ARG_NAME_OVERWRITE = "overwrite";
052:
053:            /**
054:             * Apply to the ARG_NAME_MODE parameter if opening the file
055:             */
056:            public static final String ARG_VALUE_OPEN = "open";
057:
058:            /**
059:             * Apply to the ARG_NAME_MODE parameter if saving the file
060:             */
061:            public static final String ARG_VALUE_SAVE = "save";
062:
063:            /**
064:             * Apply to the ARG_NAME_OVERWRITE parameter if the file is to be overwritten
065:             */
066:            public static final String ARG_VALUE_OVERWRITE_YES = "yes";
067:
068:            public static final String ARG_ERROR_FILE_EXISTS = "The specified file already exists.";
069:
070:            public static final String ARG_ERROR_CANNOT_OPEN_FILE = "Cannot open the specified file.";
071:
072:            /**
073:             * If the mode parameter is 'save' call the saveFile function. If the call 
074:             * mode parameter is 'open' open the file and return its contents
075:             * @return the result of the call
076:             * @param method the name of the service being called
077:             * @param context The ServiceContext contain pass and return parameters
078:             * @throws net.xoetrope.optional.service.ServiceProxyException Throw an exception if there is a problem with the call
079:             */
080:            public Object call(String method, ServiceContext context)
081:                    throws ServiceProxyException {
082:                ServiceProxyArgs args = context.getArgs();
083:                String mode = (String) args.getPassParam(ARG_NAME_MODE);
084:                if (mode.compareTo(ARG_VALUE_SAVE) == 0) {
085:                    String filename = (String) args.getPassParam(ARG_NAME_DIR)
086:                            + File.separator
087:                            + (String) args.getPassParam(ARG_NAME_FILENAME);
088:                    saveFile(context, filename, (String) args
089:                            .getPassParam(ARG_NAME_CONTENTS), (String) args
090:                            .getPassParam(ARG_NAME_OVERWRITE));
091:                } else if (mode.compareTo(ARG_VALUE_OPEN) == 0) {
092:                    String dir = (String) args.getPassParam(ARG_NAME_DIR);
093:                    String filename;
094:                    if (dir != null)
095:                        filename = dir + File.separator
096:                                + (String) args.getPassParam(ARG_NAME_FILENAME);
097:                    else
098:                        filename = (String) args
099:                                .getPassParam(ARG_NAME_FILENAME);
100:                    args.setReturnParam(ARG_NAME_CONTENTS, openFile(filename,
101:                            args));
102:                }
103:                return callNextProxy(method, context, null);
104:            }
105:
106:            /**
107:             * Save the file to the path specified by the name parameter
108:             * @param name The path to the file 
109:             * @param contents The contents to be written to the file
110:             */
111:            public void saveFile(ServiceContext context, String name,
112:                    String contents, String overwrite) {
113:                if (overwrite == null
114:                        || overwrite.compareTo(ARG_VALUE_OVERWRITE_YES) != 0) {
115:                    File f = new File(name);
116:                    if (f.exists()) {
117:                        context.addError(ARG_ERROR_FILE_EXISTS);
118:                        return;
119:                    }
120:                }
121:                try {
122:                    FileOutputStream fos = new FileOutputStream(name);
123:                    OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
124:                    BufferedWriter bw = new BufferedWriter(osw);
125:                    bw.write(contents);
126:                    bw.flush();
127:                    bw.close();
128:                } catch (IOException ex) {
129:                    ex.printStackTrace();
130:                }
131:            }
132:
133:            /**
134:             * Open the file, read it and return its contents
135:             * @param name The path to the file to be read
136:             * @return The contents of the file
137:             */
138:            protected String openFile(String name, ServiceProxyArgs args) {
139:                StringBuffer contents = new StringBuffer();
140:                try {
141:                    FileInputStream fos = new FileInputStream(name);
142:                    InputStreamReader osw = new InputStreamReader(fos, "UTF8");
143:                    BufferedReader bw = new BufferedReader(osw);
144:                    String temp = bw.readLine();
145:                    while (temp != null) {
146:                        if (temp != null)
147:                            contents.append(temp);
148:                        temp = bw.readLine();
149:                    }
150:                    bw.close();
151:                } catch (IOException ex) {
152:                    args.addError(ARG_ERROR_CANNOT_OPEN_FILE);
153:                }
154:                return contents.toString();
155:            }
156:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.