Skip to content

Commit

Permalink
Editor will now read in existing files and display them.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrennan committed Apr 30, 2010
1 parent d57ce68 commit 1202f1c
Show file tree
Hide file tree
Showing 16 changed files with 1,774 additions and 3 deletions.
48 changes: 48 additions & 0 deletions Editor.xcodeproj/project.pbxproj
Expand Up @@ -20,6 +20,12 @@
DA18BA98118910FA0096AE4B /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = DA18BA97118910FA0096AE4B /* MainWindow.xib */; };
DA18BAC811891D110096AE4B /* JBArticle.m in Sources */ = {isa = PBXBuildFile; fileRef = DA18BAC711891D110096AE4B /* JBArticle.m */; };
DA18BAF511893D770096AE4B /* JBArticleCountValueTransformer.m in Sources */ = {isa = PBXBuildFile; fileRef = DA18BAF411893D770096AE4B /* JBArticleCountValueTransformer.m */; };
DAB20336118A360C0031C95F /* NSObject+SBJSON.m in Sources */ = {isa = PBXBuildFile; fileRef = DAB2032B118A360C0031C95F /* NSObject+SBJSON.m */; };
DAB20337118A360C0031C95F /* NSString+SBJSON.m in Sources */ = {isa = PBXBuildFile; fileRef = DAB2032D118A360C0031C95F /* NSString+SBJSON.m */; };
DAB20338118A360C0031C95F /* SBJSON.m in Sources */ = {isa = PBXBuildFile; fileRef = DAB2032F118A360C0031C95F /* SBJSON.m */; };
DAB20339118A360C0031C95F /* SBJsonBase.m in Sources */ = {isa = PBXBuildFile; fileRef = DAB20331118A360C0031C95F /* SBJsonBase.m */; };
DAB2033A118A360C0031C95F /* SBJsonParser.m in Sources */ = {isa = PBXBuildFile; fileRef = DAB20333118A360C0031C95F /* SBJsonParser.m */; };
DAB2033B118A360C0031C95F /* SBJsonWriter.m in Sources */ = {isa = PBXBuildFile; fileRef = DAB20335118A360C0031C95F /* SBJsonWriter.m */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -60,6 +66,19 @@
DA18BAC911891D650096AE4B /* adobe_states_obvious.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = adobe_states_obvious.js; sourceTree = "<group>"; };
DA18BAF311893D770096AE4B /* JBArticleCountValueTransformer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JBArticleCountValueTransformer.h; sourceTree = "<group>"; };
DA18BAF411893D770096AE4B /* JBArticleCountValueTransformer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JBArticleCountValueTransformer.m; sourceTree = "<group>"; };
DAB20329118A360C0031C95F /* JSON.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSON.h; sourceTree = "<group>"; };
DAB2032A118A360C0031C95F /* NSObject+SBJSON.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+SBJSON.h"; sourceTree = "<group>"; };
DAB2032B118A360C0031C95F /* NSObject+SBJSON.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+SBJSON.m"; sourceTree = "<group>"; };
DAB2032C118A360C0031C95F /* NSString+SBJSON.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+SBJSON.h"; sourceTree = "<group>"; };
DAB2032D118A360C0031C95F /* NSString+SBJSON.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+SBJSON.m"; sourceTree = "<group>"; };
DAB2032E118A360C0031C95F /* SBJSON.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SBJSON.h; sourceTree = "<group>"; };
DAB2032F118A360C0031C95F /* SBJSON.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SBJSON.m; sourceTree = "<group>"; };
DAB20330118A360C0031C95F /* SBJsonBase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SBJsonBase.h; sourceTree = "<group>"; };
DAB20331118A360C0031C95F /* SBJsonBase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SBJsonBase.m; sourceTree = "<group>"; };
DAB20332118A360C0031C95F /* SBJsonParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SBJsonParser.h; sourceTree = "<group>"; };
DAB20333118A360C0031C95F /* SBJsonParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SBJsonParser.m; sourceTree = "<group>"; };
DAB20334118A360C0031C95F /* SBJsonWriter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SBJsonWriter.h; sourceTree = "<group>"; };
DAB20335118A360C0031C95F /* SBJsonWriter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SBJsonWriter.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -121,6 +140,7 @@
29B97314FDCFA39411CA2CEA /* Editor */ = {
isa = PBXGroup;
children = (
DAB20328118A360C0031C95F /* JSON */,
080E96DDFE201D6D7F000001 /* Classes */,
29B97315FDCFA39411CA2CEA /* Other Sources */,
29B97317FDCFA39411CA2CEA /* Resources */,
Expand Down Expand Up @@ -162,6 +182,26 @@
name = Frameworks;
sourceTree = "<group>";
};
DAB20328118A360C0031C95F /* JSON */ = {
isa = PBXGroup;
children = (
DAB20329118A360C0031C95F /* JSON.h */,
DAB2032A118A360C0031C95F /* NSObject+SBJSON.h */,
DAB2032B118A360C0031C95F /* NSObject+SBJSON.m */,
DAB2032C118A360C0031C95F /* NSString+SBJSON.h */,
DAB2032D118A360C0031C95F /* NSString+SBJSON.m */,
DAB2032E118A360C0031C95F /* SBJSON.h */,
DAB2032F118A360C0031C95F /* SBJSON.m */,
DAB20330118A360C0031C95F /* SBJsonBase.h */,
DAB20331118A360C0031C95F /* SBJsonBase.m */,
DAB20332118A360C0031C95F /* SBJsonParser.h */,
DAB20333118A360C0031C95F /* SBJsonParser.m */,
DAB20334118A360C0031C95F /* SBJsonWriter.h */,
DAB20335118A360C0031C95F /* SBJsonWriter.m */,
);
path = JSON;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -226,6 +266,12 @@
DA18BA96118910260096AE4B /* JBMainWindowController.m in Sources */,
DA18BAC811891D110096AE4B /* JBArticle.m in Sources */,
DA18BAF511893D770096AE4B /* JBArticleCountValueTransformer.m in Sources */,
DAB20336118A360C0031C95F /* NSObject+SBJSON.m in Sources */,
DAB20337118A360C0031C95F /* NSString+SBJSON.m in Sources */,
DAB20338118A360C0031C95F /* SBJSON.m in Sources */,
DAB20339118A360C0031C95F /* SBJsonBase.m in Sources */,
DAB2033A118A360C0031C95F /* SBJsonParser.m in Sources */,
DAB2033B118A360C0031C95F /* SBJsonWriter.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -259,6 +305,7 @@
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"\"$(SRCROOT)/../../Downloads/BWToolkit\"",
"\"$(SRCROOT)/../../Library/Frameworks\"",
);
GCC_DYNAMIC_NO_PIC = NO;
GCC_ENABLE_FIX_AND_CONTINUE = YES;
Expand All @@ -280,6 +327,7 @@
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"\"$(SRCROOT)/../../Downloads/BWToolkit\"",
"\"$(SRCROOT)/../../Library/Frameworks\"",
);
GCC_MODEL_TUNING = G5;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
Expand Down
1 change: 1 addition & 0 deletions JBMainWindowController.h
Expand Up @@ -27,5 +27,6 @@


- (IBAction)addAction:(id)sender;
- (void)loadArticlesFromDisk;

@end
65 changes: 62 additions & 3 deletions JBMainWindowController.m
Expand Up @@ -8,6 +8,7 @@

#import "JBMainWindowController.h"
#import "JBArticle.h"
#import "JSON.h"

@implementation JBMainWindowController
@synthesize tableView = _tableView;
Expand All @@ -17,25 +18,83 @@ @implementation JBMainWindowController
@synthesize internalArrayOfArticles = _internalArrayOfArticles;

- (void)awakeFromNib {
self.internalArrayOfArticles = [NSMutableArray array];


self.internalArrayOfArticles = [NSMutableArray array];
[self loadArticlesFromDisk];

JBArticle *a1 = [[JBArticle alloc] init];
JBArticle *a2 = [[JBArticle alloc] init];

a1.headline = @"Wickedsweet first headline";

[self.arrayController addObject:a1];
[self.arrayController addObject:a2];
//[self.arrayController addObject:a1];
//[self.arrayController addObject:a2];


}


- (IBAction)addAction:(id)sender {
NSLog(@"Add button pressed");
}


- (void)loadArticlesFromDisk {
NSString *articlesDirectoryPath = @"/Users/jasonbrennan/web/app/articles/";

NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];

NSError *error = nil;
NSArray *articlesInDirectory = [fileManager contentsOfDirectoryAtPath:articlesDirectoryPath error:&error];

if (nil == articlesInDirectory) {
NSLog(@"The articles directory path returned a nil array. Error says: %@", [error localizedDescription]);
return;
}

NSLog(@"%@", [articlesInDirectory description]);

// Get all the .js files in the directory
NSPredicate *jsPredicate = [NSPredicate predicateWithFormat:@"self LIKE '*.js'"];
NSArray *filteredArray = [articlesInDirectory filteredArrayUsingPredicate:jsPredicate];

NSLog(@"%@", [filteredArray description]);

[fileManager changeCurrentDirectoryPath:articlesDirectoryPath];

for (NSString *jsFileName in filteredArray) {
NSError *fileReadingError = nil;
NSString *jsonString = [NSString stringWithContentsOfFile:jsFileName encoding:NSUTF8StringEncoding error:&fileReadingError];

if (nil == jsonString) {
NSLog(@"There was an error reading in %@: %@", jsFileName, [fileReadingError localizedDescription]);
}

SBJSON *parser = [[[SBJSON alloc] init] autorelease];
NSDictionary *articleDictionary = [parser objectWithString:jsonString];

JBArticle *readArticle = [[JBArticle alloc] init];
readArticle.headline = [articleDictionary objectForKey:@"headline"];
readArticle.lede = [articleDictionary objectForKey:@"lede"];
readArticle.authorName = [articleDictionary objectForKey:@"author"];
readArticle.byLine = [articleDictionary objectForKey:@"by_line"];
readArticle.altText = [articleDictionary objectForKey:@"alt_text"];
readArticle.bodyFile = [articleDictionary objectForKey:@"body_text"];
readArticle.bodyText = [NSString stringWithContentsOfFile:readArticle.bodyFile encoding:NSUTF8StringEncoding error:NULL];
readArticle.sourceLink = [articleDictionary objectForKey:@"source"];

readArticle.createdAtDate = [NSDate dateWithNaturalLanguageString:[articleDictionary objectForKey:@"created_at"]];
readArticle.updatedAtDate = [NSDate dateWithNaturalLanguageString:[articleDictionary objectForKey:@"updated_at"]];

[self.arrayController addObject:readArticle];

[readArticle release];
}

}


- (void)dealloc {

self.tableView = nil;
Expand Down
50 changes: 50 additions & 0 deletions JSON/JSON.h
@@ -0,0 +1,50 @@
/*
Copyright (C) 2009 Stig Brautaset. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
@mainpage A strict JSON parser and generator for Objective-C
JSON (JavaScript Object Notation) is a lightweight data-interchange
format. This framework provides two apis for parsing and generating
JSON. One standard object-based and a higher level api consisting of
categories added to existing Objective-C classes.
Learn more on the http://code.google.com/p/json-framework project site.
This framework does its best to be as strict as possible, both in what it
accepts and what it generates. For example, it does not support trailing commas
in arrays or objects. Nor does it support embedded comments, or
anything else not in the JSON specification. This is considered a feature.
*/

#import "SBJSON.h"
#import "NSObject+SBJSON.h"
#import "NSString+SBJSON.h"

68 changes: 68 additions & 0 deletions JSON/NSObject+SBJSON.h
@@ -0,0 +1,68 @@
/*
Copyright (C) 2009 Stig Brautaset. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import <Foundation/Foundation.h>


/**
@brief Adds JSON generation to Foundation classes
This is a category on NSObject that adds methods for returning JSON representations
of standard objects to the objects themselves. This means you can call the
-JSONRepresentation method on an NSArray object and it'll do what you want.
*/
@interface NSObject (NSObject_SBJSON)

/**
@brief Returns a string containing the receiver encoded as a JSON fragment.
This method is added as a category on NSObject but is only actually
supported for the following objects:
@li NSDictionary
@li NSArray
@li NSString
@li NSNumber (also used for booleans)
@li NSNull
@deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed.
*/
- (NSString *)JSONFragment;

/**
@brief Returns a string containing the receiver encoded in JSON.
This method is added as a category on NSObject but is only actually
supported for the following objects:
@li NSDictionary
@li NSArray
*/
- (NSString *)JSONRepresentation;

@end

53 changes: 53 additions & 0 deletions JSON/NSObject+SBJSON.m
@@ -0,0 +1,53 @@
/*
Copyright (C) 2009 Stig Brautaset. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import "NSObject+SBJSON.h"
#import "SBJsonWriter.h"

@implementation NSObject (NSObject_SBJSON)

- (NSString *)JSONFragment {
SBJsonWriter *jsonWriter = [SBJsonWriter new];
NSString *json = [jsonWriter stringWithFragment:self];
if (!json)
NSLog(@"-JSONFragment failed. Error trace is: %@", [jsonWriter errorTrace]);
[jsonWriter release];
return json;
}

- (NSString *)JSONRepresentation {
SBJsonWriter *jsonWriter = [SBJsonWriter new];
NSString *json = [jsonWriter stringWithObject:self];
if (!json)
NSLog(@"-JSONRepresentation failed. Error trace is: %@", [jsonWriter errorTrace]);
[jsonWriter release];
return json;
}

@end

0 comments on commit 1202f1c

Please sign in to comment.