1xquery version "3.0";
2
3(:
4 : Copyright 2006-2010 The FLWOR Foundation.
5 :
6 : Licensed under the Apache License, Version 2.0 (the "License");
7 : you may not use this file except in compliance with the License.
8 : You may obtain a copy of the License at
9 :
10 : http://www.apache.org/licenses/LICENSE-2.0
11 :
12 : Unless required by applicable law or agreed to in writing, software
13 : distributed under the License is distributed on an "AS IS" BASIS,
14 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : See the License for the specific language governing permissions and
16 : limitations under the License.
17:)
18
19(:~
20 : Function library provides functions for generating the XQDoc XML's for a project.
21 :
22 : @author Sorin Nasoi
23 : @project xqdoc
24 :)
25
26module namespace pxqdoc  = "http://www.zorba-xquery.com/modules/project_xqdoc";
27
28(:~ needed for generating the XML's :)
29import module namespace xqd  = "http://www.zorba-xquery.com/modules/xqdoc";
30(:~ needed for writing the XML's to disk:)
31import module namespace file = "http://expath.org/ns/file";
32
33import schema namespace xqdoc = "http://www.xqdoc.org/1.0";
34import schema namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
35
36declare namespace an = "http://www.zorba-xquery.com/annotations";
37declare namespace zm = "http://www.zorba-xquery.com/manifest";
38import module namespace fetch = "http://www.zorba-xquery.com/modules/fetch";
39
40declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
41
42import module namespace err  = "http://www.zorba-xquery.com/modules/xqdoc2xhtml/error";
43declare namespace werr = "http://www.w3.org/2005/xqt-errors";
44declare option ver:module-version "2.0";
45
46(:~
47 : The serialization parameters for XML serialization.
48 :)
49declare variable $pxqdoc:serParamXml :=
50  <output:serialization-parameters>
51    <output:method value="xml"/>
52    <output:indent value="yes"/>
53  </output:serialization-parameters>;
54
55(:~
56 : Delete the intermediary xml dir
57 :
58 : @param $xqdocPath where to generate the XQDoc XML documents.
59 : @return empty sequence.
60 :)
61declare %an:sequential function pxqdoc:delete-XML-dir(
62  $xqdocPath as xs:string) as empty-sequence()
63{
64  variable $xqdocXMLPath  := fn:concat( $xqdocPath,
65                                        file:directory-separator(),
66                                        "xml");
67  (: clear the XML folder :)
68  if(file:exists($xqdocXMLPath)) then
69    file:delete($xqdocXMLPath);
70  else ();
71};
72
73(:~
74 : This function loads the ZorbaManifest.xml
75 :
76 : @param $zorbaManifestPath location of ZorbaManifest.xml.
77 : @return the manifest.
78 :)
79declare %an:nondeterministic function pxqdoc:load-manifest(
80  $zorbaManifestPath as xs:string) as document-node()?
81{
82  try
83  {
84    fn:parse-xml(file:read-text($zorbaManifestPath))
85  }
86  catch *
87  {
88    fn:error($err:UE004,fn:concat("The file <",$zorbaManifestPath,"> does not have the correct structure."))
89  }
90};
91
92(:~
93 : This function generates the XQDoc XML for all the modules found in build/ZorbaManifest.xml
94 :
95 : @param $zorbaManifestPath location of ZorbaManifest.xml.
96 : @param $xqdocPath where to generate the XQDoc XML documents.
97 : @return empty sequence.
98 :)
99declare %an:sequential function pxqdoc:generate-xqdoc-XML(
100  $zorbaManifestPath as xs:string,
101  $xqdocPath as xs:string) as empty-sequence()
102{
103  (: Note: only the modules that are configured in the Zorba version you are using will be build :)
104  variable $xqdocXMLPath := concat($xqdocPath, file:directory-separator(), "xml");
105
106  (: create the XML folder if it does not exist already :)
107  file:create-directory($xqdocXMLPath);
108
109  if(not(file:is-file($zorbaManifestPath))) then
110  {
111    variable $message := fn:concat("The file <ZorbaManifest.xml> was not found: <", $zorbaManifestPath, ">. Suggestion: run 'cmake' in your build folder such that ZorbaManifest.xml is regenerated.");
112    fn:error($err:UE004, $message);
113  }
114  else
115  {
116    variable $manifestXML := pxqdoc:load-manifest($zorbaManifestPath);
117    variable $moduleManifests := $manifestXML/zm:manifest/zm:module;
118    if(count($moduleManifests) eq xs:integer(0)) then ();
119    else
120    {
121      try
122      {
123      for $module in $moduleManifests
124      (: note the module version is not supported because of a bug in the fetch for the module URI ending with / :)
125      (:let $moduleURI := if(ends-with(data($module/zm:uri),'/')) then data($module/zm:uri)
126                       else if(exists($module/@version)) then concat(data($module/zm:uri),"#",data($module/@version))
127                       else data($module/zm:uri):)
128      (:let $moduleFetched := fetch:content(trace($moduleURI,"fetch module URI version.."), "MODULE"):)
129      let $moduleURI := data($module/zm:uri)
130      let $moduleFetched := fetch:content($moduleURI, "MODULE")
131      let $moduleFetched := fn:replace($moduleFetched, '&amp;(nbsp|#160);' , codepoints-to-string(160))
132      let $xqdoc := xqd:xqdoc-content($moduleFetched)
133      let $xqdocRelFileName  := pxqdoc:get-filename($moduleURI)
134      let $xqdocFileName := concat($xqdocXMLPath, file:directory-separator(), $xqdocRelFileName, ".xml")
135      return
136        file:write($xqdocFileName,
137                   $xqdoc,
138                   $pxqdoc:serParamXml)
139      }
140      catch *
141      {
142        fn:error($err:UE004,
143                 concat("Error processing module ",
144                        $werr:code,
145                        " - ",
146                        $werr:description))
147      }
148    };
149  }
150};
151
152(:~
153 : Returns the string resulting from replacing the directory-separators (i.e. / ) with '_'
154 :
155 : @param $moduleURI the path to the module URI.
156 : @return the string resulting from replacing the directory-separators (i.e. / ) with '_'.
157 :
158 :)
159declare function pxqdoc:get-filename(
160  $moduleURI as xs:string) as xs:string {
161  let $lmodule := if(fn:ends-with($moduleURI,"/")) then fn:concat($moduleURI,"index") else $moduleURI
162  return
163    replace(
164      replace($lmodule, "/", "_"),
165      "http:__",
166      ""
167  )
168};
169