use of com.google.cloud.language.v1beta2.Document in project JMRI by JMRI.
the class CdiPanelDemo method getRootFromFile.
Element getRootFromFile(String name) {
Element root = null;
try {
// argument controls validation
SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", false);
Document doc = builder.build(new BufferedInputStream(new FileInputStream(new File(name))));
root = doc.getRootElement();
} catch (Exception e) {
System.out.println("While reading file: " + e);
}
return root;
}
use of com.google.cloud.language.v1beta2.Document in project JMRI by JMRI.
the class SampleFactory method main.
// Main entry point for standalone run
public static void main(String[] args) {
// dump a document to stdout
Element root = getBasicSample();
Document doc = new Document(root);
try {
org.jdom2.output.XMLOutputter fmt = new org.jdom2.output.XMLOutputter();
fmt.setFormat(org.jdom2.output.Format.getPrettyFormat());
fmt.output(doc, System.out);
} catch (Exception e) {
System.err.println("Exception writing file: " + e);
}
}
use of com.google.cloud.language.v1beta2.Document in project JMRI by JMRI.
the class DefaultCatalogTreeManagerXml method writeCatalogTrees.
/*
* Writes out tree values to a file in the user's preferences directory
*/
public void writeCatalogTrees() throws java.io.IOException {
if (log.isDebugEnabled()) {
log.debug("entered writeCatalogTreeValues");
}
CatalogTreeManager manager = InstanceManager.getDefault(jmri.CatalogTreeManager.class);
List<String> trees = manager.getSystemNameList();
boolean found = false;
Iterator<String> iter = manager.getSystemNameList().iterator();
while (iter.hasNext()) {
String sname = iter.next();
CatalogTree tree = manager.getBySystemName(sname);
if (log.isDebugEnabled()) {
log.debug("Tree: sysName= " + sname + ", userName= " + tree.getUserName());
CatalogTreeNode root = tree.getRoot();
log.debug("enumerateTree called for root= " + root.toString() + ", has " + root.getChildCount() + " children");
// root.depthFirstEnumeration isn't fully typed in JDOM2
@SuppressWarnings("unchecked") Enumeration<CatalogTreeNode> e = root.depthFirstEnumeration();
while (e.hasMoreElements()) {
CatalogTreeNode n = e.nextElement();
log.debug("nodeName= " + n.getUserObject() + " has " + n.getLeaves().size() + " leaves and " + n.getChildCount() + " subnodes.");
}
}
if (sname != null && sname.charAt(1) == CatalogTree.XML) {
found = true;
break;
}
}
if (found) {
// there are trees defined, create root element
Element root = new Element("catalogTrees");
Document doc = newDocument(root, dtdLocation + "catalogTree.dtd");
// add XSLT processing instruction
// <?xml-stylesheet type="text/xsl" href="XSLT/tree-values.xsl"?>
java.util.Map<String, String> m = new java.util.HashMap<>();
m.put("type", "text/xsl");
m.put("href", xsltLocation + "panelfile.xsl");
org.jdom2.ProcessingInstruction p = new org.jdom2.ProcessingInstruction("xml-stylesheet", m);
doc.addContent(0, p);
store(root, trees);
try {
if (!checkFile(DEFAULT_FILE_NAME)) {
// file does not exist, create it
File file = new File(DEFAULT_FILE_NAME);
if (!file.createNewFile()) {
log.error("createNewFile failed");
}
}
// write content to file
writeXML(findFile(DEFAULT_FILE_NAME), doc);
// memory consistent with file
jmri.jmrit.catalog.ImageIndexEditor.indexChanged(false);
} catch (java.io.IOException ioe) {
log.error("IO Exception " + ioe);
throw (ioe);
}
}
}
use of com.google.cloud.language.v1beta2.Document in project JMRI by JMRI.
the class DecoderIndexFile method writeFile.
public void writeFile(String name, DecoderIndexFile oldIndex, String[] files) throws java.io.IOException {
if (log.isDebugEnabled()) {
log.debug("writeFile " + name);
}
// This is taken in large part from "Java and XML" page 368
File file = new File(FileUtil.getUserFilesPath() + name);
// create root element and document
Element root = new Element("decoderIndex-config");
root.setAttribute("noNamespaceSchemaLocation", "http://jmri.org/xml/schema/decoder.xsd", org.jdom2.Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"));
Document doc = newDocument(root);
// add XSLT processing instruction
// <?xml-stylesheet type="text/xsl" href="XSLT/DecoderID.xsl"?>
java.util.Map<String, String> m = new java.util.HashMap<>();
m.put("type", "text/xsl");
m.put("href", xsltLocation + "DecoderID.xsl");
ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
doc.addContent(0, p);
// add top-level elements
Element index;
root.addContent(index = new Element("decoderIndex"));
index.setAttribute("version", Integer.toString(fileVersion));
log.debug("version written to file as " + fileVersion);
// add mfg list from existing DecoderIndexFile item
Element mfgList = new Element("mfgList");
// copy dates from original mfgList element
if (oldIndex.nmraListDate != null) {
mfgList.setAttribute("nmraListDate", oldIndex.nmraListDate);
}
if (oldIndex.updated != null) {
mfgList.setAttribute("updated", oldIndex.updated);
}
if (oldIndex.lastAdd != null) {
mfgList.setAttribute("lastadd", oldIndex.lastAdd);
}
// We treat "NMRA" special...
Element mfg = new Element("manufacturer");
mfg.setAttribute("mfg", "NMRA");
mfg.setAttribute("mfgID", "999");
mfgList.addContent(mfg);
// start working on the rest of the entries
List<String> keys = new ArrayList<>(oldIndex._mfgIdFromNameHash.keySet());
Collections.sort(keys);
for (Object item : keys) {
String mfgName = (String) item;
if (!mfgName.equals("NMRA")) {
mfg = new Element("manufacturer");
mfg.setAttribute("mfg", mfgName);
mfg.setAttribute("mfgID", oldIndex._mfgIdFromNameHash.get(mfgName));
mfgList.addContent(mfg);
}
}
// add family list by scanning files
Element familyList = new Element("familyList");
for (String fileName : files) {
DecoderFile d = new DecoderFile();
try {
Element droot = d.rootFromName(DecoderFile.fileLocation + fileName);
Element family = droot.getChild("decoder").getChild("family").clone();
family.setAttribute("file", fileName);
familyList.addContent(family);
} catch (org.jdom2.JDOMException exj) {
log.error("could not parse " + fileName + ": " + exj.getMessage());
} catch (java.io.FileNotFoundException exj) {
log.error("could not read " + fileName + ": " + exj.getMessage());
} catch (IOException exj) {
log.error("other exception while dealing with " + fileName + ": " + exj.getMessage());
}
}
index.addContent(mfgList);
index.addContent(familyList);
writeXML(file, doc);
// force a read of the new file next time
resetInstance();
}
use of com.google.cloud.language.v1beta2.Document in project JMRI by JMRI.
the class ConsistFile method writeFile.
/**
* Write all consists to a file.
*
* @param consistList list of consist addresses
* @param fileName path to file
* @throws java.io.IOException if unable to write file
*/
public void writeFile(ArrayList<DccLocoAddress> consistList, String fileName) throws IOException {
// create root element
Element root = new Element("consist-roster-config");
Document doc = newDocument(root, dtdLocation + "consist-roster-config.dtd");
// add XSLT processing instruction
Map<String, String> m = new HashMap<>();
m.put("type", "text/xsl");
m.put("href", xsltLocation + "consistRoster.xsl");
ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
doc.addContent(0, p);
Element roster = new Element("roster");
for (int i = 0; i < consistList.size(); i++) {
Consist newConsist = consistMan.getConsist(consistList.get(i));
roster.addContent(consistToXml(newConsist));
}
root.addContent(roster);
try {
if (!checkFile(fileName)) {
//The file does not exist, create it before writing
File file = new File(fileName);
File parentDir = file.getParentFile();
if (!parentDir.exists()) {
if (!parentDir.mkdir()) {
throw (new IOException());
}
}
if (!file.createNewFile()) {
throw (new IOException());
}
}
writeXML(findFile(fileName), doc);
} catch (IOException ioe) {
log.error("IO Exception " + ioe);
throw (ioe);
}
}
Aggregations