use of com.google.cloud.language.v1beta2.Document in project pcgen by PCGen.
the class Initiative method loadINIT.
/**
* Perform initial loading
* @param initFile
* @param comp
*/
public void loadINIT(File initFile, PCGenMessageHandler comp) {
try {
SAXBuilder builder = new SAXBuilder();
Document character = builder.build(initFile);
loadFromDocument(character, comp);
} catch (Exception e) {
JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(this), "File load error: " + initFile.getName());
Logging.errorPrint("File Load Error" + initFile.getName());
Logging.errorPrint(e.getMessage(), e);
}
}
use of com.google.cloud.language.v1beta2.Document in project pcgen by PCGen.
the class Initiative method saveToDocument.
/**
* Saves the current combatants out to an XML file
*
*@param xml The File to save to
*@exception Exception XML and file IO exceptions
*/
private void saveToDocument(File xml) throws IOException {
Element party = new Element("Party");
party.setAttribute("filever", "1.0");
party.setAttribute("filetype", "initsave");
/*if(currentInit > -1) {
party.setAttribute("current_init", Integer.toString(currentInit));
}*/
initList.forEach((InitHolder anInitList) -> party.addContent(anInitList.getSaveElement()));
XMLOutputter xmlOut = new XMLOutputter();
xmlOut.setFormat(Format.getRawFormat().setEncoding("US-ASCII"));
try (Writer fr = new FileWriter(xml)) {
Document saveDocument = new Document(party);
xmlOut.output(saveDocument, fr);
fr.flush();
fr.close();
}
}
use of com.google.cloud.language.v1beta2.Document in project JMRI by JMRI.
the class WebServerManager method preferencesFromMiniServerPreferences.
@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Catch is covering both JDOMException and IOException, FindBugs seems confused")
private void preferencesFromMiniServerPreferences(File MSFile, File WSFile) {
XmlFile xmlFile = new XmlFile() {
};
try {
Element MSRoot = xmlFile.rootFromFile(MSFile);
Element WSRoot = new Element(WebServerPreferences.WEB_SERVER_PREFERENCES);
// NOI18N
Element MSPrefs = MSRoot.getChild("MiniServerPreferences");
for (Object pref : MSPrefs.getChildren()) {
WSRoot.addContent((Element) pref);
}
for (Attribute attr : MSPrefs.getAttributes()) {
if (attr.getName().equals("getDisallowedFrames")) {
// NOI18N
Element DF = new Element(WebServerPreferences.DISALLOWED_FRAMES);
// NOI18N
String[] frames = attr.getValue().split("\\n");
for (String frame : frames) {
DF.addContent(new Element(WebServerPreferences.FRAME).addContent(frame));
}
WSRoot.addContent(DF);
} else if (attr.getName().equals("getPort")) {
// NOI18N
WSRoot.setAttribute(WebServerPreferences.PORT, attr.getValue());
} else if (attr.getName().equals("getClickDelay")) {
// NOI18N
WSRoot.setAttribute(WebServerPreferences.CLICK_DELAY, attr.getValue());
} else if (attr.getName().equals("getRefreshDelay")) {
// NOI18N
WSRoot.setAttribute(WebServerPreferences.REFRESH_DELAY, attr.getValue());
} else {
// double cast because clone() is Protected on Object
WSRoot.setAttribute(attr.clone());
}
}
Document WSDoc = XmlFile.newDocument(WSRoot);
File parent = new File(WSFile.getParent());
if (!parent.exists()) {
// directory known to not exist from previous conditional
boolean created = parent.mkdir();
if (!created) {
log.error("Failed to create directory {}", parent.toString());
throw new java.io.IOException("Failed to create directory " + parent.toString());
}
}
// known to not exist or this method would not have been called
boolean created = WSFile.createNewFile();
if (!created) {
log.error("Failed to new create file {}", WSFile.toString());
throw new java.io.IOException("Failed to create new file " + WSFile.toString());
}
xmlFile.writeXML(WSFile, WSDoc);
} catch (IOException | JDOMException ex) {
log.error("Error converting miniServer preferences to Web Server preferences.", ex);
}
}
use of com.google.cloud.language.v1beta2.Document in project JMRI by JMRI.
the class LayoutPanelServlet method getXmlPanel.
@Override
protected String getXmlPanel(String name) {
log.debug("Getting {} for {}", getPanelType(), name);
try {
LayoutEditor editor = (LayoutEditor) getEditor(name);
Element panel = new Element("panel");
panel.setAttribute("name", name);
panel.setAttribute("paneltype", getPanelType());
panel.setAttribute("height", Integer.toString(editor.getLayoutHeight()));
panel.setAttribute("width", Integer.toString(editor.getLayoutWidth()));
panel.setAttribute("panelheight", Integer.toString(editor.getLayoutHeight()));
panel.setAttribute("panelwidth", Integer.toString(editor.getLayoutWidth()));
panel.setAttribute("showtooltips", (editor.showTooltip()) ? "yes" : "no");
panel.setAttribute("controlling", (editor.allControlling()) ? "yes" : "no");
panel.setAttribute("xscale", Float.toString((float) editor.getXScale()));
panel.setAttribute("yscale", Float.toString((float) editor.getYScale()));
panel.setAttribute("mainlinetrackwidth", Integer.toString(editor.getMainlineTrackWidth()));
panel.setAttribute("sidetrackwidth", Integer.toString(editor.getSideTrackWidth()));
panel.setAttribute("turnoutcircles", (editor.getTurnoutCircles()) ? "yes" : "no");
panel.setAttribute("turnoutcirclesize", Integer.toString(editor.getTurnoutCircleSize()));
panel.setAttribute("turnoutdrawunselectedleg", (editor.getTurnoutDrawUnselectedLeg()) ? "yes" : "no");
if (editor.getBackgroundColor() == null) {
panel.setAttribute("backgroundcolor", ColorUtil.colorToString(Color.lightGray));
} else {
panel.setAttribute("backgroundcolor", ColorUtil.colorToString(editor.getBackgroundColor()));
}
panel.setAttribute("defaulttrackcolor", editor.getDefaultTrackColor());
panel.setAttribute("defaultoccupiedtrackcolor", editor.getDefaultOccupiedTrackColor());
panel.setAttribute("defaultalternativetrackcolor", editor.getDefaultAlternativeTrackColor());
panel.setAttribute("defaulttextcolor", editor.getDefaultTextColor());
panel.setAttribute("turnoutcirclecolor", editor.getTurnoutCircleColor());
// include positionable elements
List<Positionable> contents = editor.getContents();
log.debug("N positionable elements: {}", contents.size());
for (Positionable sub : contents) {
if (sub != null) {
try {
Element e = ConfigXmlManager.elementFromObject(sub);
if (e != null) {
if ("signalmasticon".equals(e.getName())) {
//insert icon details into signalmast
e.addContent(getSignalMastIconsElement(e.getAttributeValue("signalmast")));
}
try {
e.setAttribute(JSON.ID, sub.getNamedBean().getSystemName());
} catch (NullPointerException ex) {
if (sub.getNamedBean() == null) {
log.debug("{} {} does not have an associated NamedBean", e.getName(), e.getAttribute(JSON.NAME));
} else {
log.debug("{} {} does not have a SystemName", e.getName(), e.getAttribute(JSON.NAME));
}
}
parsePortableURIs(e);
panel.addContent(e);
}
} catch (Exception ex) {
log.error("Error storing panel element: " + ex, ex);
}
}
}
// include PositionablePoints
int num = editor.pointList.size();
log.debug("N positionablepoint elements: {}", num);
if (num > 0) {
for (int i = 0; i < num; i++) {
Object sub = editor.pointList.get(i);
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
panel.addContent(e);
}
} catch (Exception e) {
log.error("Error storing panel positionalpoint element: " + e);
}
}
}
// include LayoutBlocks
LayoutBlockManager tm = InstanceManager.getDefault(LayoutBlockManager.class);
java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
SensorManager sm = InstanceManager.sensorManagerInstance();
num = 0;
while (iter.hasNext()) {
String sname = iter.next();
if (sname == null) {
log.error("System name null during LayoutBlock store");
}
LayoutBlock b = tm.getBySystemName(sname);
if (b.getUseCount() > 0) {
// save only those LayoutBlocks that are in use--skip abandoned ones
Element elem = new Element("layoutblock").setAttribute("systemname", sname);
String uname = b.getUserName();
if (uname != null && !uname.isEmpty()) {
elem.setAttribute("username", uname);
}
// get occupancy sensor from layoutblock if it is valid
if (!b.getOccupancySensorName().isEmpty()) {
Sensor s = sm.getSensor(b.getOccupancySensorName());
if (s != null) {
//send systemname
elem.setAttribute("occupancysensor", s.getSystemName());
}
//if layoutblock has no occupancy sensor, use one from block, if it is populated
} else {
Sensor s = b.getBlock().getSensor();
if (s != null) {
//send systemname
elem.setAttribute("occupancysensor", s.getSystemName());
}
}
elem.setAttribute("occupiedsense", Integer.toString(b.getOccupiedSense()));
elem.setAttribute("trackcolor", ColorUtil.colorToString(b.getBlockTrackColor()));
elem.setAttribute("occupiedcolor", ColorUtil.colorToString(b.getBlockOccupiedColor()));
elem.setAttribute("extracolor", ColorUtil.colorToString(b.getBlockExtraColor()));
if (!b.getMemoryName().isEmpty()) {
elem.setAttribute("memory", b.getMemoryName());
}
if (!b.useDefaultMetric()) {
elem.addContent(new Element("metric").addContent(Integer.toString(b.getBlockMetric())));
}
//add to the panel xml
panel.addContent(elem);
num++;
}
}
log.debug("N layoutblock elements: {}", num);
// include LevelXings
num = editor.xingList.size();
log.debug("N levelxing elements: {}", num);
if (num > 0) {
for (int i = 0; i < num; i++) {
Object sub = editor.xingList.get(i);
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
panel.addContent(e);
}
} catch (Exception e) {
log.error("Error storing panel levelxing element: " + e);
}
}
}
// include LayoutTurnouts
num = editor.turnoutList.size();
log.debug("N layoutturnout elements: {}", num);
if (num > 0) {
for (int i = 0; i < num; i++) {
Object sub = editor.turnoutList.get(i);
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
panel.addContent(e);
}
} catch (Exception e) {
log.error("Error storing panel layoutturnout element: " + e);
}
}
}
// include TrackSegments
num = editor.trackList.size();
log.debug("N tracksegment elements: {}", num);
if (num > 0) {
for (int i = 0; i < num; i++) {
Object sub = editor.trackList.get(i);
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
panel.addContent(e);
}
} catch (Exception e) {
log.error("Error storing panel tracksegment element: " + e);
}
}
}
// include LayoutSlips
num = editor.slipList.size();
log.debug("N layoutSlip elements: {}", num);
if (num > 0) {
for (int i = 0; i < num; i++) {
Object sub = editor.slipList.get(i);
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
panel.addContent(e);
}
} catch (Exception e) {
log.error("Error storing panel layoutSlip element: " + e);
}
}
}
// include LayoutTurntables
num = editor.turntableList.size();
log.debug("N turntable elements: {}", num);
if (num > 0) {
for (int i = 0; i < num; i++) {
Object sub = editor.turntableList.get(i);
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
panel.addContent(e);
}
} catch (Exception e) {
log.error("Error storing panel turntable element: " + e);
}
}
}
//write out formatted document
Document doc = new Document(panel);
XMLOutputter fmt = new XMLOutputter();
fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.TRIM));
return fmt.outputString(doc);
} catch (NullPointerException ex) {
log.warn("Requested Layout panel [" + name + "] does not exist.");
return "ERROR Requested panel [" + name + "] does not exist.";
}
}
use of com.google.cloud.language.v1beta2.Document in project JMRI by JMRI.
the class PanelServlet method getXmlPanel.
@Override
protected String getXmlPanel(String name) {
log.debug("Getting {} for {}", getPanelType(), name);
try {
PanelEditor editor = (PanelEditor) getEditor(name);
Element panel = new Element("panel");
JFrame frame = editor.getTargetFrame();
panel.setAttribute("name", name);
panel.setAttribute("height", Integer.toString(frame.getContentPane().getHeight()));
panel.setAttribute("width", Integer.toString(frame.getContentPane().getWidth()));
panel.setAttribute("panelheight", Integer.toString(editor.getTargetPanel().getHeight()));
panel.setAttribute("panelwidth", Integer.toString(editor.getTargetPanel().getWidth()));
panel.setAttribute("showtooltips", (editor.showTooltip()) ? "yes" : "no");
panel.setAttribute("controlling", (editor.allControlling()) ? "yes" : "no");
if (editor.getBackgroundColor() != null) {
Element color = new Element("backgroundColor");
color.setAttribute("red", Integer.toString(editor.getBackgroundColor().getRed()));
color.setAttribute("green", Integer.toString(editor.getBackgroundColor().getGreen()));
color.setAttribute("blue", Integer.toString(editor.getBackgroundColor().getBlue()));
panel.addContent(color);
}
// include contents
List<Positionable> contents = editor.getContents();
log.debug("Panel has {} elements", contents.size());
for (Positionable sub : contents) {
if (sub != null) {
try {
Element e = ConfigXmlManager.elementFromObject(sub);
if (e != null) {
if ("signalmasticon".equals(e.getName())) {
//insert icon details into signalmast
e.addContent(getSignalMastIconsElement(e.getAttributeValue("signalmast")));
}
try {
e.setAttribute(JSON.ID, sub.getNamedBean().getSystemName());
} catch (NullPointerException ex) {
if (sub.getNamedBean() == null) {
log.debug("{} {} does not have an associated NamedBean", e.getName(), e.getAttribute(JSON.NAME));
} else {
log.debug("{} {} does not have a SystemName", e.getName(), e.getAttribute(JSON.NAME));
}
}
parsePortableURIs(e);
panel.addContent(e);
}
} catch (Exception ex) {
log.error("Error storing panel element: {}", ex.getMessage(), ex);
}
}
}
Document doc = new Document(panel);
XMLOutputter out = new XMLOutputter();
out.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.TRIM));
return out.outputString(doc);
} catch (NullPointerException ex) {
log.warn("Requested Panel [" + name + "] does not exist.");
return "ERROR Requested panel [" + name + "] does not exist.";
}
}
Aggregations