Skip to content

Commit

Permalink
changed markup for examples in book
Browse files Browse the repository at this point in the history
  • Loading branch information
mitzimorris committed Jan 2, 2014
1 parent ca9cc6c commit 6d96718
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
71 changes: 37 additions & 34 deletions src/casestudy/src/com/colloquial/casestudy/FederalistParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public static void main(String[] args)
/*x*/

/*x FederalistParser.3 */
public static final String START_PAPER = "FEDERALIST No.";
public static final String START_PAPER
= "FEDERALIST No.";
public static final String END_OF_BOOK
= "End of the Project Gutenberg";

Expand All @@ -69,10 +70,9 @@ public List<Paper> parsePapers(File inFile)
InputStreamReader inReader = null;
BufferedReader bufReader = null;
try {
inStream = new FileInputStream(inFile);
inReader = new InputStreamReader(inStream,
INPUT_ENCODING);
bufReader = new BufferedReader(inReader);
/*bbf*/inStream = new FileInputStream(inFile);/*ebf*/
/*bbf*/inReader = new InputStreamReader(inStream,INPUT_ENCODING);/*ebf*/
/*bbf*/bufReader = new BufferedReader(inReader);/*ebf*/

// read from beginning of file to start of paper #1
String line = null;
Expand All @@ -82,8 +82,8 @@ public List<Paper> parsePapers(File inFile)
if (line == null)
throw new IOException("unexpected EOF");

StringBuilder paragraph = new StringBuilder();
ArrayList<String> parsList = new ArrayList<String>();
/*bbf*/StringBuilder paragraph = new StringBuilder();/*ebf*/
/*bbf*/ArrayList<String> parsList = new ArrayList<String>();/*ebf*/

int ct = 0;
// process all papers
Expand All @@ -106,7 +106,7 @@ public List<Paper> parsePapers(File inFile)
// get this paper
ct++;
parsList.add(line); // header line
while ((line = bufReader.readLine()) != null) {
/*bbf*/while/*ebf*/ ((line = bufReader.readLine()) != null) {
if (line.startsWith(START_PAPER)) break;
if (line.startsWith(END_OF_BOOK)) break;
if ("".equals(line)) {
Expand All @@ -115,7 +115,7 @@ public List<Paper> parsePapers(File inFile)
paragraph.setLength(0);
}
} else {
paragraph.append(line + " ");
/*bbf*/paragraph.append(line + " ");/*ebf*/
}
}
}
Expand All @@ -131,68 +131,71 @@ public List<Paper> parsePapers(File inFile)
+ ct);
}
}
} finally {
close(bufReader);
close(inReader);
close(inStream);
} /*bbf*/finally/*ebf*/ {
/*bbf*/close(bufReader);/*ebf*/
/*bbf*/close(inReader);/*ebf*/
/*bbf*/close(inStream);/*ebf*/
}
return papers;
}
/*x*/

/*x FederalistParser.4 */
static final String regexPaper
= "FEDERALIST No. (\\d+).*";
static final Pattern patternPaper = Pattern.compile(regexPaper);

static final String regexPub
= "\\w+ ([^\\.]+)\\. \\w+, ([^\\.]+).*";
static final Pattern patternPub = Pattern.compile(regexPub);

static final String regexAuthor
= "(\\w+)(?:, with (\\w+))?.*";
static final Pattern patternAuthor = Pattern.compile(regexAuthor);
/*x*/

/*x FederalistParser.5 */
// process list of paragraphs
// first 5 contain header information, rest are body
// first 5 list entries contain header information, rest are body
public Paper parsePaper(List<String> pars) {

String header = pars.remove(0);
String number = null;
String regex = "FEDERALIST No. (\\d+).*";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(header);
Matcher matcher = patternPaper.matcher(header);
if (matcher.matches()) {
number = matcher.group(1);
}

String title = pars.remove(0);

String publication = pars.remove(0);
String pubName = null;
String pubDate = null;
regex = "\\w+ ([^\\.]+)\\. \\w+, ([^\\.]+).*";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(publication);
matcher = patternPub.matcher(publication);
if (matcher.matches()) {
pubName = matcher.group(1);
pubDate = matcher.group(2);
}

String author = pars.remove(0);
String author1 = null;
String author2 = null;
regex = "(\\w+)(?:, with (\\w+))?.*";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(author);
matcher = patternAuthor.matcher(author);
if (matcher.matches()) {
author1 = matcher.group(1);
author2 = matcher.group(2);
}

pars.remove(0); // skip salutation

return new Paper.Builder(number).title(title)
.pubName(pubName).pubDate(pubDate)
.author1(author1).author2(author2)
.paragraphs(pars).build();
}
/*x*/
/*x FederalistParser.5 */

/*x FederalistParser.6 */
public void paperToXml(Paper paper, File outDir)
throws IOException {

// pad paper number so that files sort nicely
String numPaper
= String.format("%02d",paper.getNumber());
= /*bbf*/String.format("%02d",paper.getNumber());/*ebf*/
String filename = "paper_" + numPaper + ".xml";
File outFile = new File(outDir,filename);

Expand All @@ -205,8 +208,8 @@ public void paperToXml(Paper paper, File outDir)
OUTPUT_ENCODING);
bufWriter = new BufferedWriter(outWriter);

Format format = Format.getPrettyFormat();
format.setEncoding(OUTPUT_ENCODING);
/*bbf*/Format format = Format.getPrettyFormat();/*ebf*/
/*bbf*/format.setEncoding(OUTPUT_ENCODING);/*ebf*/
XMLOutputter outputter
= new XMLOutputter(format);
Element root = new Element("add");
Expand All @@ -221,7 +224,7 @@ public void paperToXml(Paper paper, File outDir)
}
/*x*/

/*x FederalistParser.6 */
/*x FederalistParser.7 */
void close(Closeable c) {
if (c == null) return;
try {
Expand Down
8 changes: 4 additions & 4 deletions src/casestudy/src/com/colloquial/casestudy/Paper.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ public Builder pubName(String str) {
}
public Builder pubDate(String str) {
if (str == null) return this;
String pattern = "MMMMM dd, yyyy";
SimpleDateFormat format
= new SimpleDateFormat(pattern,Locale.ENGLISH);
Date date = format.parse(str, new ParsePosition(0));
/*bbf*/String pattern = "MMMMM dd, yyyy";/*ebf*/
/*bbf*/SimpleDateFormat format/*ebf*/
/*bbf*/= new SimpleDateFormat(pattern,Locale.ENGLISH);/*ebf*/
/*bbf*/Date date = format.parse(str, new ParsePosition(0));/*ebf*/
pubDate = date; return this;
}
public Builder author1(String str) {
Expand Down

0 comments on commit 6d96718

Please sign in to comment.