root / framework / tags / fusebox51B1 / fuseboxWriter.cfc

Revision 292, 7.9 kB (checked in by scorfield, 2 years ago)

Fixes #169 by changing copyright to TeraTech and adopting Apache Source License 2.0
(awaiting final confirmation from Michael Smith that ASL2.0 is acceptable).

Line 
1<!---
2Copyright 2006 TeraTech, Inc. http://teratech.com/
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15--->
16<cfcomponent output="false" hint="I manage the creation of and writing to the parsed files.">
17
18        <cffunction name="init" returntype="any" access="public" output="false"
19                                hint="I am the constructor.">
20                <cfargument name="fbApp" type="any" required="false"
21                                        hint="I am the fusebox application object. I am required but it's faster to specify that I am not required." />
22                <cfargument name="myFusebox" type="any" required="false"
23                                        hint="I am the myFusebox data structure. I am required but it's faster to specify that I am not required." />
24               
25                <cfset variables.fuseboxApplication = arguments.fbApp />
26                <cfset variables.myFusebox = arguments.myFusebox />
27                <cfset variables.parsedDir = variables.fuseboxApplication.expandFuseboxPath(variables.fuseboxApplication.parsePath) />
28                <cfset variables.phase = "" />
29                <cfset variables.circuit = "" />
30                <cfset variables.fuseaction = "" />
31                <cfset variables.newline = chr(13) & chr(10) />
32               
33                <cfif not directoryExists(variables.parsedDir)>
34                        <cflock name="#variables.parsedDir#" type="exclusive" timeout="30">
35                                <cfif not directoryExists(variables.parsedDir)>
36                                        <cftry>
37                                                <cfdirectory action="create" directory="#variables.parsedDir#" mode="777" />
38                                        <cfcatch type="any">
39                                                <cfthrow type="fusebox.missingParsedDirException"
40                                                        message="The 'parsed' directory in the application root directory is missing, and could not be created"
41                                                        detail="You must manually create this directory, and ensure that CF has the ability to write and change files within the directory."
42                                                        extendedinfo="#cfcatch.detail#" />
43                                        </cfcatch>
44                                        </cftry>
45                                </cfif>
46                        </cflock>
47                </cfif>
48
49                <cfset reset() />
50
51                <cfreturn this />
52
53        </cffunction>
54       
55        <cffunction name="getMyFusebox" returntype="any" access="public" output="false">
56       
57                <cfreturn variables.myFusebox />
58               
59        </cffunction>
60
61        <cffunction name="reset" returntype="void" access="public" output="false"
62                                hint="I reset the phase, circuit and fuseaction as well as initializing the file content object.">
63
64                <cfset variables.lastPhase = "" />
65                <cfset variables.lastCircuit = "" />
66                <cfset variables.lastFuseaction = "" />
67                <cfset variables.content = createObject("java","java.lang.StringBuffer").init() />
68
69        </cffunction>   
70
71        <cffunction name="open" returntype="void" access="public" output="false"
72                                hint="I 'open' the parsed file. In fact I just setup the writing process. The file is only created when this writer object is 'closed'.">
73                <cfargument name="filename" type="string" required="true"
74                                        hint="I am the name of the parsed file to be created." />
75               
76                <cfset variables.filename = arguments.filename />
77                <cfset reset() />
78                <cfset rawPrintln('<cfsetting enablecfoutputonly="true" />') />
79                <cfset rawPrintln('<cfprocessingdirective pageencoding="#variables.fuseboxApplication.characterEncoding#" />') />
80               
81        </cffunction>
82       
83        <cffunction name="close" returntype="void" access="public" output="false"
84                                hint="I 'close' the parsed file and write it to disk.">
85               
86                <cfset rawPrintln('<cfsetting enablecfoutputonly="false" />') />
87                <cftry>
88                        <cffile action="write" file="#variables.parsedDir#/#variables.filename#"
89                                        output="#variables.content.toString()#"
90                                        charset="#variables.fuseboxApplication.characterEncoding#" />
91                <cfcatch type="any">
92                        <cfthrow type="fusebox.errorWritingParsedFile"
93                                        message="An Error during write of Parsed File or Parsing Directory not found."
94                                        detail="Attempting to write the parsed file '#variables.filename#' threw an error. This can also occur if the parsed file directory cannot be found."
95                                        extendedinfo="#cfcatch.detail#" />
96                </cfcatch>
97                </cftry>
98               
99        </cffunction>
100       
101        <cffunction name="setPhase" returntype="any" access="public" output="false"
102                                hint="I remember the currently executing plugin phase.">
103                <cfargument name="phase" type="any" required="false"
104                                        hint="I am the name of the current phase. I am required but it's faster to specify that I am not required." />
105               
106                <cfset var p = variables.phase />
107               
108                <cfset variables.phase = arguments.phase />
109               
110                <cfreturn p />
111               
112        </cffunction>
113       
114        <cffunction name="setCircuit" returntype="any" access="public" output="false"
115                                hint="I remember the currently executing circuit alias.">
116                <cfargument name="circuit" type="any" required="false"
117                                        hint="I am the name of the current circuit. I am required but it's faster to specify that I am not required." />
118               
119                <cfset var c = variables.circuit />
120               
121                <cfset variables.circuit = arguments.circuit />
122               
123                <cfreturn c />
124               
125        </cffunction>
126       
127        <cffunction name="setFuseaction" returntype="any" access="public" output="false"
128                                hint="I remember the currently executing fuseaction name.">
129                <cfargument name="fuseaction" type="any" required="false"
130                                        hint="I am the name of the current fuseaction. I am required but it's faster to specify that I am not required." />
131               
132                <cfset var f = variables.fuseaction />
133               
134                <cfset variables.fuseaction = arguments.fuseaction />
135               
136                <cfreturn f />
137               
138        </cffunction>
139       
140        <cffunction name="print" returntype="void" access="public" output="false"
141                                hint="I print a string to the parsed file. I set the phase, circuit and fuseaction variables if necessary in the myFusebox structure.">
142                <cfargument name="text" type="any" required="false"
143                                        hint="I am the string to be printed. I am required but it's faster to specify that I am not required." />
144               
145                <cfif variables.lastPhase is not variables.phase>
146                        <cfset rawPrintln('<cfset myFusebox.thisPhase = "#variables.phase#">') />
147                        <cfset variables.lastPhase = variables.phase />
148                </cfif>
149                <cfif variables.lastCircuit is not variables.circuit>
150                        <cfset rawPrintln('<cfset myFusebox.thisCircuit = "#variables.circuit#">') />
151                        <cfset variables.lastCircuit = variables.circuit />
152                </cfif>
153                <cfif variables.lastFuseaction is not variables.fuseaction>
154                        <cfset rawPrintln('<cfset myFusebox.thisFuseaction = "#variables.fuseaction#">') />
155                        <cfset variables.lastFuseaction = variables.fuseaction />
156                </cfif>
157                <cfset variables.content.append(arguments.text) />
158               
159        </cffunction>
160       
161        <cffunction name="println" returntype="void" access="public" output="false"
162                                hint="I print a string to the parsed file, followed by a newline. I set the phase, circuit and fuseaction variables if necessary in the myFusebox structure.">
163                <cfargument name="text" type="any" required="false"
164                                        hint="I am the string to be printed. I am required but it's faster to specify that I am not required." />
165               
166                <cfset print(arguments.text) />
167                <cfset variables.content.append(variables.newline) />
168               
169        </cffunction>
170       
171        <cffunction name="rawPrint" returntype="void" access="public" output="false"
172                                hint="I print a string to the parsed file, without setting any variables.">
173                <cfargument name="text" type="any" required="false"
174                                        hint="I am the string to be printed. I am required but it's faster to specify that I am not required." />
175
176                <cfset variables.content.append(arguments.text) />
177
178        </cffunction>
179       
180        <cffunction name="rawPrintln" returntype="void" access="public" output="false"
181                                hint="I print a string to the parsed file, followed by a newline, without setting any variables.">
182                <cfargument name="text" type="any" required="false"
183                                        hint="I am the string to be printed. I am required but it's faster to specify that I am not required." />
184
185                <cfset variables.content.append(arguments.text).append(variables.newline) />
186
187        </cffunction>
188               
189</cfcomponent>
Note: See TracBrowser for help on using the browser.