-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResponseView.m
79 lines (63 loc) · 2.67 KB
/
ResponseView.m
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// ResponseView.m
// Jayson
//
// Created by Ben Yellin on 12/22/10.
// Copyright 2010 Been Yelling. All rights reserved.
//
#import "ResponseView.h"
#import "Response.h"
NSString *const JAYResponseViewJSON = @"json";
NSString *const JAYResponseViewRaw = @"raw";
NSString *const JAYResponseViewError = @"error";
@interface ResponseView (Private)
- (NSString *)generateUnformattedHTMLForResponse:(Response *)resp;
- (NSString *)generateFormattedHTMLForResponse:(Response *)resp;
- (NSString *)generateErrorHTMLForString:(NSString *)errorText;
- (NSString *)generateHTMLForString:(NSString *)source withType:(NSString *)responseViewType;
@end
@implementation ResponseView
- (id)initWithFrame:(NSRect)frameRect frameName:(NSString *)frameName groupName:(NSString *)groupName {
if (self = [super initWithFrame:frameRect frameName:frameName groupName:groupName]) {
}
return self;
}
- (void)displayResponseUnformatted:(Response *)resp {
NSURL *resourceURL = [[NSBundle mainBundle] resourceURL];
[[self mainFrame] loadHTMLString:[self generateUnformattedHTMLForResponse:resp]
baseURL:resourceURL];
}
- (void)displayResponseFormatted:(Response *)resp {
NSURL *resourceURL = [[NSBundle mainBundle] resourceURL];
[[self mainFrame] loadHTMLString:[self generateFormattedHTMLForResponse:resp]
baseURL:resourceURL];
}
- (void)displayError:(NSString *)errorText {
NSURL *resourceURL = [[NSBundle mainBundle] resourceURL];
[[self mainFrame] loadHTMLString:[self generateErrorHTMLForString:errorText]
baseURL:resourceURL];
}
- (NSString *)generateFormattedHTMLForResponse:(Response *)resp {
return [self generateHTMLForString:resp.source withType:JAYResponseViewJSON];
}
- (NSString *)generateUnformattedHTMLForResponse:(Response *)resp {
return [self generateHTMLForString:resp.source withType:JAYResponseViewRaw];
}
- (NSString *)generateErrorHTMLForString:(NSString *)errorText {
NSString *headerErrorText = [NSString stringWithFormat:@"<h2>%@</h2>", errorText];
return [self generateHTMLForString:headerErrorText withType:JAYResponseViewError];
}
- (NSString *)generateHTMLForString:(NSString *)source withType:(NSString *)responseViewType {
NSString *templatePath = [[NSBundle mainBundle] pathForResource:@"template" ofType:@"html"];
NSMutableString *html = [[NSMutableString alloc] initWithContentsOfFile:templatePath];
[html replaceOccurrencesOfString:@"%%RESP%%"
withString:source
options:NSLiteralSearch
range:NSMakeRange(0, [html length])];
[html replaceOccurrencesOfString:@"%%TYPE%%"
withString:responseViewType
options:NSLiteralSearch
range:NSMakeRange(0, [html length])];
return [html autorelease];
}
@end