Please take time to read the code disclaimer.
/**
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package metadatagenerator;
import broadscope.StaticClass;
import java.io.*;
import java.lang.reflect.Field;
import java.util.*;
import java.util.logging.*;
import java.util.regex.*;
import org.apache.commons.lang.WordUtils;
/**
*
* @author Kent
*/
public class TalkContent {
private Talk owner;
private Language language;
private String title = "", url = "", speaker = "", speakerTitle = "", summary = "", tagsString = "";
private String conferenceName = "", documentString = "", filepath = "NO FILEPATH", targetKeyword = "";
private LinkedList specificTagsList = new LinkedList();
private boolean madeUp = false, scanned = false;
/**
* This is the constructor
* @param owner the Talk this TalkContent is assigned to
* @param language
*/
public TalkContent(Talk owner) {
this.owner = owner;
}
/**
* This method will read the information we need from the web
*/
public void talkReader() {
if (!madeUp) {
if (!url.equals("")) {
try {
//Regex String
String titleRegEx = "";
String summaryRegEx = "";
String tagsRegEx = "";
String conferenceNameRegEx = "(.{2,10}?\\d{4}).{3,16}";
//Regex Pattern
Pattern titlePattern = Pattern.compile(titleRegEx, Pattern.DOTALL);
Pattern summaryPattern = Pattern.compile(summaryRegEx, Pattern.DOTALL);
Pattern tagsPattern = Pattern.compile(tagsRegEx, Pattern.DOTALL);
Pattern speakerPattern = Pattern.compile("", Pattern.DOTALL);
Pattern endSpeakerPattern = Pattern.compile("
", Pattern.DOTALL);
Pattern conferenceNamePattern = Pattern.compile(conferenceNameRegEx, Pattern.DOTALL);
BufferedReader reader = StaticClass.getBufferedReader(url);
String line = "";
boolean titleFound = false;
boolean summaryFound = false;
boolean tagsFound = false;
boolean speakerFound = false;
boolean conferenceNameFound = false;
while ((line = reader.readLine()) != null && (!titleFound || !summaryFound || !tagsFound || !speakerFound || !conferenceNameFound)) {
Matcher titleMatcher = titlePattern.matcher(line);
Matcher summaryMatcher = summaryPattern.matcher(line);
Matcher tagsMatcher = tagsPattern.matcher(line);
Matcher speakerMatcher = speakerPattern.matcher(line);
Matcher conferenceNameMatcher = conferenceNamePattern.matcher(line);
if (titleMatcher.find()) {
title = titleMatcher.group(1);
titleFound = true;
}
if (summaryMatcher.find()) {
summary = summaryMatcher.group(1);
summaryFound = true;
}
if (tagsMatcher.find()) {
String stringOfTags = tagsMatcher.group(1);
Scanner src = new Scanner(stringOfTags);
src.useDelimiter(",");
while (src.hasNext()) {
specificTagsList.add(src.next().trim().toLowerCase());
}
tagsFound = true;
}
if (conferenceNameMatcher.find()) {
conferenceName = conferenceNameMatcher.group(1);
conferenceNameFound = true;
}
if (speakerMatcher.find()) {
boolean found = false;
StringBuilder authorStringBuilder = new StringBuilder();
while (!found && line != null) {
line = reader.readLine();
if (line != null) {
Matcher endSpeakerMatcher = endSpeakerPattern.matcher(line);
authorStringBuilder.append(line);
if (endSpeakerMatcher.find()) {
Pattern speakerName1 = Pattern.compile("\\s*(.*?)\\s*", Pattern.DOTALL);
Pattern speakerName2AndTitle = Pattern.compile("(.*?)
", Pattern.DOTALL);
Pattern checkProblem = Pattern.compile(".*?.*?
", Pattern.DOTALL);
String authorString = authorStringBuilder.toString();
Matcher matcher1 = speakerName1.matcher(authorString);
Matcher matcher2 = speakerName2AndTitle.matcher(authorString);
Matcher matcher3 = checkProblem.matcher(authorString);
if (matcher1.find()) {
speaker = matcher1.group(1).trim();
if (matcher2.find()) {
if (matcher3.find()) {
if (matcher2.find()) {
speakerTitle = matcher2.group(1).trim();
}
} else {
speakerTitle = matcher2.group(1).trim();
}
}
found = true;
} else {
if (matcher2.find()) {
speaker = matcher2.group(1).trim();
}
if (matcher2.find()) {
speakerTitle = matcher2.group(1).trim();
}
found = true;
}
}
}
}
speakerFound = true;
}
}
} catch (IOException ex) {
System.out.println("Problem with this object: ");
System.out.println(getObjectInString());
Logger.getLogger(TalkContent.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
scanned = true;
if (speaker.equals("")) {
System.out.println("Speaker not found! " + url);
}
}
/**
* This method will create the talk's document string
*/
public void createTalkDocumentString() {
setToEnglish();
resetTargetKeywordAndTags();
String nl = StaticClass.newline;
LanguageType languageType = language.getOwner();
String languageLine = "Language:" + nl + languageType.getName();
if (!languageType.getName().equals(languageType.getEnglishName())) {
languageLine += " (" + languageType.getEnglishName() + ")";
}
String conferenceLine = "Conference:" + nl + owner.getSession().getConference().getName();
String sessionLine = "Session:" + nl + owner.getSession().getOwner().getName();
String titleLine = "Talk Title:" + nl + title;
String capTargetKeyword = "";
if (targetKeyword.equals("lds")) {
capTargetKeyword = targetKeyword.toUpperCase();
} else {
capTargetKeyword = WordUtils.capitalize(targetKeyword);
}
String speakerLine = "Speaker:" + nl + speaker;
if (!speakerTitle.equals("")) {
speakerLine += ", " + speakerTitle;
}
String youTubeLine = "Online Title:" + nl + capTargetKeyword + " - " + title;
String descriptionLines = "Description:" + nl + capTargetKeyword + " - " + summary + " " + nl
+ languageType.getMoreInfo() + ": " + languageType.getMormonSite() + " " + nl; //Mormon.org
if (!languageType.getSocialSite().equals("")) {
descriptionLines += languageType.getTalkWithMormon() + ": " + languageType.getSocialSite() + nl; //Social LDS Site
}
descriptionLines += " " + nl;
if (!speaker.equals("")) {
descriptionLines += speaker;
if (!speakerTitle.equals("")) {
descriptionLines += ", " + speakerTitle + ",";
}
}
String confName = "";
if (conferenceName.equals("")) {
confName = language.getConferenceName();
} else {
confName = conferenceName;
}
descriptionLines += " \"" + title + "\" " + languageType.getMormonGeneralConferece() + " " + confName + " " + nl
+ languageType.getMoreOnSermon() + ": " + owner.getSession().getConference().getLandingPage();
if (owner.getEnglishTalkContent() != this) {
descriptionLines += "&clang=" + languageType.getAbb(); //Conference Landing page
}
String tagLine = "Tags:" + nl + tagsString;
documentString = languageLine + nl + nl
+ conferenceLine + nl + nl
+ sessionLine + nl + nl
+ titleLine + nl + nl
+ speakerLine + nl + nl
+ "------------------------------------------------------------------" + nl + nl + nl
+ youTubeLine + nl + nl + nl
+ descriptionLines + nl + nl + nl
+ tagLine;
documentString = documentString.replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ").replace("…", "...").replace("�", "\"").replace("“", "\"").replace("’", "\'").replace("\"\"", "\"");
}
/**
* This method sets the target keyword. It chooses a random generic tag and sets that as the target keyword
*/
private void resetTargetKeywordAndTags() {
LinkedList genericTagList = language.getOwner().getGenericTagList();
if (genericTagList.size() < 1 || (genericTagList.size() == 1 && genericTagList.get(0).equals(""))) {
genericTagList = specificTagsList;
}
if (genericTagList.size() > 0) {
int randomInt = new Random().nextInt(genericTagList.size());
targetKeyword = genericTagList.get(randomInt);
}
generateTags();
}
/**
* this method generates the tags we want and randomizes them.
*/
private void generateTags() {
int tagLimit = 15;
String tags = "";
LanguageType languageType = language.getOwner();
LinkedList tagsList = new LinkedList();
addFromThisToThat(languageType.getGenericTagList(), tagsList, tagLimit, languageType.getGenericTagList().size(), true);
addFromThisToThat(specificTagsList, tagsList, tagLimit, 6, true);
addFromThisToThat(languageType.getRandomTagList(), tagsList, tagLimit, tagLimit - 3, true);
//randomize tags, add target keyword to the front and convert to string and return
tagsList.add(title);
tagsList.add(speaker);
Collections.shuffle(tagsList);
tagsList.addFirst(targetKeyword);
for (int i = 0; i < tagsList.size(); i++) {
tags += tagsList.get(i);
if (i != tagsList.size() - 1) {
tags += ", ";
}
}
tagsString = tags.trim().toLowerCase();
}
/**
* This method accepts a list of strings and adds it to the other given list
* @param thisList
* @param thatList
* @param limit
* @param addLimit
* @param shuffle
*/
private void addFromThisToThat(LinkedList thisList, LinkedList thatList, int limit, int addLimit, boolean shuffle) {
if (thatList.size() < limit) {
if (!thisList.isEmpty()) {
LinkedList thisListCopy = thisList;
if (shuffle) {
Collections.shuffle(thisListCopy);
}
int iteration = 0;
int adds = 0;
while (thatList.size() < limit && adds < addLimit && iteration < thisListCopy.size()) {
boolean found = false;
for (String thatString : thatList) {
if (thatString.equals(thisListCopy.get(iteration))) {
found = true;
break;
}
}
if (!found) {
thatList.add(thisListCopy.get(iteration));
adds++;
}
iteration++;
}
}
}
}
/**
* This method sets the content to English if they were never set for some reason
*/
public void setToEnglish() {
boolean needTranslation = false;
TalkContent english = owner.getEnglishTalkContent();
if (title.equals("")) {
title = english.getTitle();
needTranslation = true;
}
if (url.equals("")) {
url = english.getUrl();
needTranslation = true;
}
if (speaker.equals("")) {
speaker = english.getSpeaker();
needTranslation = true;
}
if (speakerTitle.equals("")) {
speakerTitle = english.getSpeakerTitle();
needTranslation = true;
}
if (summary.equals("")) {
summary = english.getSummary();
needTranslation = true;
}
if (specificTagsList.isEmpty()) {
for (String tag : english.getSpecificTagsList()) {
specificTagsList.add(tag);
}
needTranslation = true;
}
if (language.getConferenceName().equals("")) {
language.setConferenceName(english.getLanguage().getConferenceName());
needTranslation = true;
}
if (needTranslation) {
owner.getSession().getConference().getOwner().getNeedToTranslateList().add(this);
}
}
/**
* this method gets a string of the object's fields and values
* @return string of the object's fields and values
*/
public String getObjectInString() {
StringBuilder result = new StringBuilder();
result.append(this.getClass().getName());
result.append(" Object {");
result.append(StaticClass.newline);
//determine fields declared in this class only (no fields of superclass)
Field[] fields = this.getClass().getDeclaredFields();
//print field names paired with their values
for (Field field : fields) {
result.append(" ");
try {
result.append(field.getName());
result.append(": ");
//requires access to private field:
result.append(field.get(this));
} catch (IllegalAccessException e) {
System.out.println(e);
}
result.append(StaticClass.newline);
}
result.append("}");
return result.toString();
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/**
* @return the speaker
*/
public String getSpeaker() {
return speaker;
}
/**
* @param speaker the speaker to set
*/
public void setSpeaker(String speaker) {
this.speaker = speaker;
}
/**
* @return the summary
*/
public String getSummary() {
return summary;
}
/**
* @param summary the summary to set
*/
public void setSummary(String summary) {
this.summary = summary;
}
/**
* @return the specificTagsList
*/
public LinkedList getSpecificTagsList() {
return specificTagsList;
}
/**
* @param specificTagsList the specificTagsList to set
*/
public void setSpecificTagsList(LinkedList specificTagsList) {
this.specificTagsList = specificTagsList;
}
/**
* @return the filepath
*/
public String getFilepath() {
return filepath;
}
/**
* @param filepath the filepath to set
*/
public void setFilepath(String filepath) {
this.filepath = filepath;
}
/**
* @return the documentString
*/
public String getDocumentString() {
return documentString;
}
/**
* @param documentString the documentString to set
*/
public void setDocumentString(String documentString) {
this.documentString = documentString;
}
/**
* @return the owner
*/
public Talk getOwner() {
return owner;
}
/**
* @param owner the owner to set
*/
public void setOwner(Talk owner) {
this.owner = owner;
}
/**
* @return the language
*/
public Language getLanguage() {
return language;
}
/**
* @param language the language to set
*/
public void setLanguage(Language language) {
this.language = language;
}
/**
* @return the speakerTitle
*/
public String getSpeakerTitle() {
return speakerTitle;
}
/**
* @param speakerTitle the speakerTitle to set
*/
public void setSpeakerTitle(String speakerTitle) {
this.speakerTitle = speakerTitle;
}
/**
* @return the targetKeyword
*/
public String getTargetKeyword() {
return targetKeyword;
}
/**
* @param targetKeyword the targetKeyword to set
*/
public void setTargetKeyword(String targetKeyword) {
this.targetKeyword = targetKeyword;
}
/**
* @return the tagsString
*/
public String getTagsString() {
return tagsString;
}
/**
* @param tagsString the tagsString to set
*/
public void setTagsString(String tagsString) {
this.tagsString = tagsString;
}
/**
* @return the conferenceName
*/
public String getConferenceName() {
return conferenceName;
}
/**
* @param conferenceName the conferenceName to set
*/
public void setConferenceName(String conferenceName) {
this.conferenceName = conferenceName;
}
/**
* @return the scanned
*/
public boolean isScanned() {
return scanned;
}
/**
* @param scanned the scanned to set
*/
public void setScanned(boolean scanned) {
this.scanned = scanned;
}
/**
* @return the madeUp
*/
public boolean isMadeUp() {
return madeUp;
}
/**
* @param madeUp the madeUp to set
*/
public void setMadeUp(boolean madeUp) {
this.madeUp = madeUp;
}
}