source: smart/trunk/p/C0SMXMLB.m@ 1742

Last change on this file since 1742 was 1591, checked in by Sam Habiel, 12 years ago

Updated license for routines

File size: 3.4 KB
Line 
1MXMLBLD ;;ISF/RWF - Tool to build XML ;07/09/09 16:55 - Smart Container Ver.
2 ;;1.0;VISTA SMART CONTAINER;;Sep 26, 2012;Build 5
3 ; Public Domain
4 QUIT
5 ;
6 ;DOC - The top level tag
7 ;DOCTYPE - Want to include a DOCTYPE node
8 ;FLAG - Set to 'G' to store the output in the global ^TMP("MXMLBLD",$J,
9START(DOC,DOCTYPE,FLAG,NO1ST) ;Call this once at the begining.
10 K ^TMP("MXMLBLD",$J)
11 S ^TMP("MXMLBLD",$J,"DOC")=DOC,^TMP("MXMLBLD",$J,"STK")=0
12 I $G(FLAG)["G" S ^TMP("MXMLBLD",$J,"CNT")=1
13 I $G(NO1ST)'=1 D OUTPUT($$XMLHDR)
14 D:$L($G(DOCTYPE)) OUTPUT("<!DOCTYPE "_DOCTYPE_">") D OUTPUT("<"_DOC_">")
15 Q
16 ;
17END ;Call this once to close out the document
18 D OUTPUT("</"_$G(^TMP("MXMLBLD",$J,"DOC"))_">")
19 I '$G(^TMP("MXMLBLD",$J,"CNT")) K ^TMP("MXMLBLD",$J)
20 K ^TMP("MXMLBLD",$J,"DOC"),^("CNT"),^("STK")
21 Q
22 ;
23ITEM(INDENT,TAG,ATT,VALUE) ;Output a Item
24 N I,X
25 S ATT=$G(ATT)
26 I '$D(VALUE) D OUTPUT($$BLS($G(INDENT))_"<"_TAG_$$ATT(.ATT)_" />") Q
27 D OUTPUT($$BLS($G(INDENT))_"<"_TAG_$$ATT(.ATT)_">"_$$CHARCHK(VALUE)_"</"_TAG_">")
28 Q
29 ;DOITEM is a callback to output the lower level.
30MULTI(INDENT,TAG,ATT,DOITEM) ;Output a Multipule
31 N I,X,S
32 S ATT=$G(ATT)
33 D PUSH($G(INDENT),TAG,.ATT)
34 D @DOITEM
35 D POP
36 Q
37 ;
38ATT(ATT) ;Output a string of attributes
39 I $D(ATT)<9 Q ""
40 N I,S,V
41 S S="",I=""
42 F S I=$O(ATT(I)) Q:I="" S S=S_" "_I_"="_$$Q(ATT(I))
43 Q S
44 ;
45Q(X) ;Add Quotes - Changed by gpl to use single instead of double quotes 6/11
46 ;I X'[$C(34) Q $C(34)_X_$C(34)
47 I X'[$C(39) Q $C(39)_X_$C(39)
48 ;N Q,Y,I,Z S Q=$C(34),(Y,Z)=""
49 N Q,Y,I,Z S Q=$C(39),(Y,Z)=""
50 F I=1:1:$L(X,Q)-1 S Y=Y_$P(X,Q,I)_Q_Q
51 S Y=Y_$P(X,Q,$L(X,Q))
52 ;Q $C(34)_Y_$C(34)
53 Q $C(39)_Y_$C(39)
54 ;
55XMLHDR() ; -- provides current XML standard header
56 Q "<?xml version=""1.0"" encoding=""utf-8"" ?>"
57 ;
58OUTPUT(S) ;Output
59 N C S C=$G(^TMP("MXMLBLD",$J,"CNT"))
60 I C S ^TMP("MXMLBLD",$J,C)=S,^TMP("MXMLBLD",$J,"CNT")=C+1 Q
61 W S,!
62 Q
63 ;
64CHARCHK(STR) ; -- replace xml character limits with entities
65 N A,I,X,Y,Z,NEWSTR
66 S (Y,Z)=""
67 ;IF STR["&" SET NEWSTR=STR DO SET STR=Y_Z
68 ;. FOR X=1:1 SET Y=Y_$PIECE(NEWSTR,"&",X)_"&amp;",Z=$PIECE(STR,"&",X+1,999) QUIT:Z'["&"
69 I STR["&" F I=1:1:$L(STR,"&")-1 S STR=$P(STR,"&",1,I)_"&amp;"_$P(STR,"&",I+1,999)
70 I STR["<" F S STR=$PIECE(STR,"<",1)_"&lt;"_$PIECE(STR,"<",2,99) Q:STR'["<"
71 I STR[">" F S STR=$PIECE(STR,">",1)_"&gt;"_$PIECE(STR,">",2,99) Q:STR'[">"
72 I STR["'" F S STR=$PIECE(STR,"'",1)_"&apos;"_$PIECE(STR,"'",2,99) Q:STR'["'"
73 I STR["""" F S STR=$PIECE(STR,"""",1)_"&quot;"_$PIECE(STR,"""",2,99) Q:STR'[""""
74 ;
75 S STR=$TR(STR,$C(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31))
76 QUIT STR
77 ;
78COMMENT(VAL) ;Add Comments
79 N I,L
80 ;I $D($G(VAL))=1 D OUTPUT("<!-- "_ATT_" -->") Q
81 I $D(VAL) D OUTPUT("<!-- "_ATT_" -->") Q ;CHANGED BY GPL FOR GTM
82 S I="",L="<!--"
83 F S I=$O(ATT(I)) Q:I="" D OUTPUT(L_ATT(I)) S L=""
84 D OUTPUT("-->")
85 Q
86 ;
87PUSH(INDENT,TAG,ATT) ;Write a TAG and save.
88 N CNT
89 S ATT=$G(ATT)
90 D OUTPUT($$BLS($G(INDENT))_"<"_TAG_$$ATT(.ATT)_">")
91 S CNT=$G(^TMP("MXMLBLD",$J,"STK"))+1,^TMP("MXMLBLD",$J,"STK")=CNT,^TMP("MXMLBLD",$J,"STK",CNT)=INDENT_"^"_TAG
92 Q
93 ;
94POP ;Write last pushed tag and pop
95 N CNT,TAG,INDENT,X
96 S CNT=$G(^TMP("MXMLBLD",$J,"STK")),X=^TMP("MXMLBLD",$J,"STK",CNT),^TMP("MXMLBLD",$J,"STK")=CNT-1
97 S INDENT=+X,TAG=$P(X,"^",2)
98 D OUTPUT($$BLS(INDENT)_"</"_TAG_">")
99 Q
100 ;
101BLS(I) ;Return INDENT string
102 N S
103 S S="",I=$G(I) S:I>0 $P(S," ",I)=" "
104 Q S
105 ;
106INDENT() ;Renturn indent level
107 Q +$G(^TMP("MXMLBLD",$J,"STK"))
Note: See TracBrowser for help on using the repository browser.