Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mm_getdocument.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Writes the TextStore of a Document Object to an external file
4  @details If the document exists, and has a textstore object, the contents
5  of that textstore are written to an external file.
6 
7  usage:
8 
9  %mm_getdocument(tree=/some/meta/path
10  ,name=someDocument
11  ,outref=/some/unquoted/filename.ext
12  )
13 
14  <h4> Dependencies </h4>
15  @li mp_abort.sas
16 
17  @param tree= The metadata path of the document
18  @param name= Document object name.
19  @param outref= full and unquoted path to the desired text file. This will be
20  overwritten if it already exists.
21 
22  @author Allan Bowe
23 
24 **/
25 
26 %macro mm_getdocument(
27  tree=/User Folders/sasdemo
28  ,name=myNote
29  ,outref=%sysfunc(pathname(work))/mm_getdocument.txt
30  ,mDebug=1
31  );
32 
33 %local mD;
34 %if &mDebug=1 %then %let mD=;
35 %else %let mD=%str(*);
36 %&mD.put Executing &sysmacroname..sas;
37 %&mD.put _local_;
38 
39 /**
40  * check tree exists
41  */
42 
43 data _null_;
44  length type uri $256;
45  rc=metadata_pathobj("","&tree","Folder",type,uri);
46  call symputx('type',type,'l');
47  call symputx('treeuri',uri,'l');
48 run;
49 
50 %mp_abort(
51  iftrue= (&type ne Tree)
52  ,mac=mm_getdocument.sas
53  ,msg=Tree &tree does not exist!
54 )
55 
56 /**
57  * Check object exists
58  */
59 data _null_;
60  length type docuri tsuri tsid $256 ;
61  rc1=metadata_pathobj("","&tree/&name","Note",type,docuri);
62  rc2=metadata_getnasn(docuri,"Notes",1,tsuri);
63  rc3=metadata_getattr(tsuri,"Id",tsid);
64  call symputx('type',type,'l');
65  call symputx("tsid",tsid,'l');
66  putlog (_all_)(=);
67 run;
68 
69 %mp_abort(
70  iftrue= (&type ne Document)
71  ,mac=mm_getdocument.sas
72  ,msg=Document &name could not be found in &tree!
73 )
74 
75 /**
76  * Now we can extract the textstore
77  */
78 filename __getdoc temp lrecl=10000000;
79 proc metadata
80  in="<GetMetadata><Reposid>$METAREPOSITORY</Reposid>
81  <Metadata><TextStore Id='&tsid'/></Metadata>
82  <Ns>SAS</Ns><Flags>1</Flags><Options/></GetMetadata>"
83  out=__getdoc ;
84 run;
85 
86 /* find the beginning of the text */
87 data _null_;
88  infile __getdoc lrecl=10000;
89  input;
90  start=index(_infile_,'StoredText="');
91  if start then do;
92  call symputx("start",start+11);
93  put start= "type=&type";
94  putlog '"' _infile_ '"';
95  end;
96  stop;
97 
98 /* read the content, byte by byte, resolving escaped chars */
99 filename __outdoc "&outref" lrecl=100000;
100 data _null_;
101  length filein 8 fileid 8;
102  filein = fopen("__getdoc","I",1,"B");
103  fileid = fopen("__outdoc","O",1,"B");
104  rec = "20"x;
105  length entity $6;
106  do while(fread(filein)=0);
107  x+1;
108  if x>&start then do;
109  rc = fget(filein,rec,1);
110  if rec='"' then leave;
111  else if rec="&" then do;
112  entity=rec;
113  do until (rec=";");
114  if fread(filein) ne 0 then goto getout;
115  rc = fget(filein,rec,1);
116  entity=cats(entity,rec);
117  end;
118  select (entity);
119  when ('&amp;' ) rec='&' ;
120  when ('&lt;' ) rec='<' ;
121  when ('&gt;' ) rec='>' ;
122  when ('&apos;') rec="'" ;
123  when ('&quot;') rec='"' ;
124  when ('&#x0a;') rec='0A'x;
125  when ('&#x0d;') rec='0D'x;
126  when ('&#36;' ) rec='$' ;
127  when ('&#x09;') rec='09'x;
128  otherwise putlog "WARNING: missing value for " entity=;
129  end;
130  rc =fput(fileid, substr(rec,1,1));
131  rc =fwrite(fileid);
132  end;
133  else do;
134  rc =fput(fileid,rec);
135  rc =fwrite(fileid);
136  end;
137  end;
138  end;
139  getout:
140  rc=fclose(filein);
141  rc=fclose(fileid);
142 run;
143 filename __getdoc clear;
144 filename __outdoc clear;
145 
146 %mend;