Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 76 lines (66 sloc) 3.713 kb
4f935814 » PeyloW
2011-09-21 Added new CWTranslator, CWTranslation and CWXMLTranslation implementa…
1 CWXMLTranslator
2 ===============
3
4 CWTranslator is a utility class for translating an ordered input into live
5 objects. The concrete subclass CWXMLTranslatir is useful for well defined and
6 non-recursive XML documents, for example RSS-feeds, status responses, etc.
7
8 Previous versions of CWFoundation where CWXMLTranslator inherited directly from
9 NSObject is not compatible with the new and improved translator. This disruptive
10 update is made to get rid of legacy code that is hard to maintain, and to enable
11 future suport for JSON translations as well.
12
13 How to perform the translation is defined in a translation DSL language, that is
14 parsed by the CWXMLTranslation class. The compiled internal format is complex
15 and bound to change, so always use the DSL.
16
17
18 Translation DSL Syntax
19 ==================
20
21 A human readable text-format should instead be used, by loading text files
22 using the standard xml translation file extension: .xmltranslation.
23
24 XML Translation format in Backus-Naur Form:
25 translation ::= statement | # A translation is one or more statement
26 "{" statement* "}"
27 statement ::= source action { ";" } # A statment is a source to perform actions on.
28 source ::= { "." } SYMBOL | # A symbol, with optional argument marker.
29 source "|" source* # A pipe separated list of sources.
30 action ::= "->" translation | # -> Is a required tag to descend into, but take no action on.
31 assignment target { ":" type } # All other actions are assignment to a target, with optional type (NSString is used for untyped actions)
32 assignment ::= ">>" | # Assign to target using setValue:forKey:
33 "+>" # Append to target using addValue:forKey:
34 target ::= destination context # The target
35 destination ::= "@root" | # Target is the array of root objects to return.
36 SYMBOL # Target is a named property accessable using setValue:forKey:
37 context ::= { "(" SYMBOL ")" } # Context is a user defined string.
38 type ::= SYMBOL # Type is a known Objective-C class (NSNumber, NSDate, NSURL)
39 SYMBOL translation | # Type is an Objective-C class with inline translation definition
40 "@" SYMBOL # Type is an Objective-C class with translation defiition in external class
41
42 Exmaple for translation this XML;
43 <Node>
44 <Id>12</Id>
45 <Url>http://www.foo.com"</Url>
46 <Type name="bar" date="2010-10-28"/>
47 </Node>
48 To fit these Objective-C classes:
49 @interface CWNodeType : NSObject {
50 }
51 @property(copy) NSString* name;
52 @property(retain) NSDate* date;
53 @end
54 @interface CWNode : NSObject {
55 }
56 @property(assign) NSInteger nodeID;
57 @property(copy) NSURL* url;
58 @property(retain) CWNodeType* type;
59 @end
60
61 Use this translation definition
62 Node +> @root : CWNode { # Node tag shoudl be added as root object of class CWNode
63 Id >> nodeID : NSNumber; # Set Id tag content to nodeID property typed as a NSNumber
64 Url >> url : NSURL; # Set Url tag content to url property typed as a NSURL
65 Type >> type : CWNodeType { # Type tag sets a the type property to a new instance of CWNodeType class
66 .name >> name; # Set name attribute content to name property as a string
67 .date >> date : NSDate; # Set date attribute content to date property typed as a NSDate
68 }
69 }
70
71 Like this:
72 NSArray* objects = [CWXMLTranslator translateContentsOfURL:urlToXML
73 withTranslationNamed:@"NameOfTranslationFile"
74 delegate:nil
75 error:NULL];
76 CWNode* node = [objects lastObject];
77
78
Something went wrong with that request. Please try again.