Created
February 1, 2016 13:57
-
-
Save robbiemu/38cabf2d0fc759a1967f to your computer and use it in GitHub Desktop.
working on using DummyContent.java for markdown pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xyz.selfenrichment.robertotomas.camtutnotes.markdown; | |
import org.w3c.dom.Element; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
import org.w3c.dom.Document; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.DocumentBuilder; | |
import us.feras.mdv.MarkdownView; | |
/** | |
* Helper class for providing sample content for user interfaces created by | |
* Android template wizards. | |
* <p> | |
* TODO: Replace all uses of this class before publishing your app. | |
*/ | |
public class MarkdownContent { | |
/** | |
* An array of `markdown` items. | |
*/ | |
public static final List<MarkdownItem> ITEMS = new ArrayList<MarkdownItem>(); | |
/** | |
* A map of `markdown` items, by ID. | |
*/ | |
public static final Map<String, MarkdownItem> ITEM_MAP = new HashMap<String, MarkdownItem>(); | |
/* Original code: | |
private static final int COUNT = 28; | |
static { | |
// Add some sample items. | |
for (int i = 1; i <= COUNT; i++) { | |
addItem(createMarkdownItem(i)); | |
} | |
} | |
*/ | |
/* replacement workbench: */ | |
static { | |
MarkdownContents noteContents = new MarkdownContents("contents.xml"); | |
MarkdownNoteXML MarkdownNoteXML [] = noteContents.notes; | |
for(MarkdownNoteXML note : noteXML) { | |
addItem(createMarkdownItem(note.position, note.title, note.fileURI)); | |
} | |
} | |
public static class MarkdownNoteXML { | |
public final position; | |
public final title; | |
public final fileURI; | |
public MarkdownNoteXML(position, title, fileURI){ | |
this.position = position; | |
this.title = title; | |
this.fileURI = fileURI; | |
} | |
} | |
public static class MarkdownContents { | |
public final String filename; | |
public final MarkdownNoteXML [] notes = new MarkdownNoteXML [countMarkdownPages()]; | |
public MarkdownContents(String filename) { | |
this.filename = filename; | |
generateNotes(); | |
} | |
public void generateNotes() { | |
String pos, t, file; | |
MarkdownNoteXML note; | |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder db = dbf.newDocumentBuilder(); | |
Document dom = db.parse(getFileURI(this.filename)); | |
Element docEle = dom.getDocumentElement(); | |
NodeList nl = docEle.getChildNodes(); | |
if (nl != null && nl.getLength() > 0) { | |
for (int i = 0; i < nl.getLength(); i++) { | |
if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) { | |
Element el = (Element) nl.item(i); | |
if (el.getNodeName().contains("note")) { | |
pos = el.getElementsByTagName("position").item(0).getTextContent(); | |
t = el.getElementsByTagName("title").item(0).getTextContent(); | |
file = el.getElementsByTagName("filename").item(0).getTextContent(); | |
note = new MarkdownNoteXML(pos, t, file); | |
notes.push(note); | |
} | |
} | |
} | |
} | |
} | |
} | |
public static int countMarkdownPages(){ | |
int number = 0; | |
String [] files = getAssets().list(""); | |
for(String filename : files){ | |
if filename.endsWith(".md"){ | |
number++; | |
} | |
} | |
return number; | |
} | |
/* end replacement workbench */ | |
private static void addItem(MarkdownItem item) { | |
ITEMS.add(item); | |
ITEM_MAP.put(item.id, item); | |
} | |
private static MarkdownItem createMarkdownItem(String position, String title, String fileURI) { | |
return new MarkdownItem(position, title, getFileURI(filename)); | |
} | |
private static String getFileURI(String filename) { | |
return String.format("file:///android_asset/%s", filename); | |
} | |
/** | |
* A markdown item representing a piece of content. | |
*/ | |
public static class MarkdownItem { | |
public final String id; | |
public final String note_title; | |
public final String note_source; | |
public MarkdownItem(String id, String note_title, String note_source) { | |
this.id = id; | |
this.note_title = note_title; | |
this.note_source = note_source; | |
} | |
@Override | |
public String toString() { | |
return note_title; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment