]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSString+XMLEscaping.m
added -isNotEmpty
[sope] / sope-core / NGExtensions / FdExt.subproj / NSString+XMLEscaping.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "NSString+misc.h"
23 #include "common.h"
24
25 @implementation NSString(XMLEscaping)
26
27 - (NSString *)stringByEscapingXMLStringUsingCharacters {
28   register unsigned i, len, j;
29   register unichar  *chars, *buf;
30   unsigned escapeCount;
31   
32   if ((len = [self length]) == 0) return @"";
33   
34   chars = malloc((len + 3) * sizeof(unichar));
35   [self getCharacters:chars];
36   
37   /* check for characters to escape ... */
38   for (i = 0, escapeCount = 0; i < len; i++) {
39     switch (chars[i]) {
40       case '&': case '"': case '<': case '>':
41         escapeCount++;
42         break;
43       default:
44         if (chars[i] > 127)
45           escapeCount++;
46         break;
47     }
48   }
49   if (escapeCount == 0 ) {
50     /* nothing to escape ... */
51     if (chars) free(chars);
52     return [[self copy] autorelease];
53   }
54   
55   buf = malloc(((len + 3) * sizeof(unichar)) +
56                (escapeCount * 8 * sizeof(unichar)));
57   for (i = 0, j = 0; i < len; i++) {
58     switch (chars[i]) {
59       /* escape special chars */
60       case '&':
61         buf[j] = '&'; j++; buf[j] = 'a'; j++; buf[j] = 'm'; j++;
62         buf[j] = 'p'; j++; buf[j] = ';'; j++;
63         break;
64       case '"':
65         buf[j] = '&'; j++; buf[j] = 'q'; j++; buf[j] = 'u'; j++;
66         buf[j] = 'o'; j++; buf[j] = 't'; j++; buf[j] = ';'; j++;
67         break;
68       case '<':
69         buf[j] = '&'; j++; buf[j] = 'l'; j++; buf[j] = 't'; j++;
70         buf[j] = ';'; j++;
71         break;
72       case '>':
73         buf[j] = '&'; j++; buf[j] = 'g'; j++; buf[j] = 't'; j++;
74         buf[j] = ';'; j++;
75         break;
76         
77       default:
78         /* escape big chars */
79         if (chars[i] > 127) {
80           unsigned char nbuf[16];
81           unsigned int k;
82           
83           sprintf((char *)nbuf, "&#%i;", (int)chars[i]);
84           for (k = 0; nbuf[k] != '\0'; k++) {
85             buf[j] = nbuf[k];
86             j++;
87           }
88         }
89         else {
90           /* nothing to escape */
91           buf[j] = chars[i];
92           j++;
93         }
94         break;
95     }
96   }
97   
98   self = [NSString stringWithCharacters:buf length:j];
99   
100   if (chars) free(chars);
101   if (buf)   free(buf);
102   return self;
103 }
104
105 - (NSString *)stringByEscapingXMLString {
106   return [self stringByEscapingXMLStringUsingCharacters];
107 }
108
109 - (NSString *)stringByEscapingXMLAttributeValue {
110   return [self stringByEscapingHTMLAttributeValue];
111 }
112
113 /* XML FQNs */
114
115 - (BOOL)xmlIsFQN {
116   if ([self length] == 0) return NO;
117   return [self characterAtIndex:0] == '{' ? YES : NO;
118 }
119
120 - (NSString *)xmlNamespaceURI {
121   NSRange r;
122   
123   r = [self rangeOfString:@"}" options:(NSLiteralSearch | NSBackwardsSearch)];
124   if (r.length == 0) return nil;
125   if ([self characterAtIndex:0] != '{') return nil;
126   
127   r.length   = (r.location - 1);
128   r.location = 1;
129   return [self substringWithRange:r];
130 }
131
132 - (NSString *)xmlLocalName {
133   NSRange r;
134   
135   r = [self rangeOfString:@"}" options:(NSLiteralSearch | NSBackwardsSearch)];
136   if (r.length == 0) return nil;
137   if ([self characterAtIndex:0] != '{') return nil;
138   
139   return [self substringFromIndex:(r.location + r.length)];
140 }
141
142 @end /* NSString(XMLEscaping) */