source: ccr/trunk/p/C0CMXMLB.m@ 727

Last change on this file since 727 was 607, checked in by George Lilly, 15 years ago

MXML DOM output library

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