use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCRChangeData method getProcessingInstruction.
public ProcessingInstruction getProcessingInstruction() {
if (pi == null) {
String data = RAW_OUTPUTTER.outputString(new Text(text));
this.pi = new ProcessingInstruction(type, data);
}
return pi;
}
use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCRLabelTransformer method getElement.
public static Element getElement(MCRLabel label) {
Element le = new Element("label");
if (MetaDataElementFactory.stringNotEmpty(label.getLang())) {
le.setAttribute("lang", label.getLang(), XML_NAMESPACE);
}
le.setAttribute("text", label.getText() != null ? label.getText() : "");
if (MetaDataElementFactory.stringNotEmpty(label.getDescription())) {
le.setAttribute("description", label.getDescription());
}
return le;
}
use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCRDynamicURIResolver method resolveVariablesFromElement.
/**
* This method runs through the whole content of the startElement and
* tries to resolve all variables in texts and attributes.
*
* @param startElement where to start to resolve the variables
* @param variablesMap a map of all variables
*/
protected void resolveVariablesFromElement(Element startElement, Hashtable<String, String> variablesMap) {
Iterator<Element> it = startElement.getDescendants(Filters.element());
MCRTextResolver varResolver = new MCRTextResolver(variablesMap);
while (it.hasNext()) {
Element element = it.next();
// text
String text = element.getText();
if (text != null && !text.equals("") && text.contains("{")) {
element.setText(varResolver.resolve(text));
}
// attributes
for (Attribute attrib : element.getAttributes()) {
String attribValue = attrib.getValue();
if (attribValue.contains("{")) {
attrib.setValue(varResolver.resolve(attribValue));
}
}
}
}
use of org.jdom2.Text in project JMRI by JMRI.
the class RosterServlet method doPost.
/**
* Handle any POST request as an upload of a roster file from client.
*
* @param request servlet request
* @param response servlet response
* @throws javax.servlet.ServletException if unable to process uploads
* @throws java.io.IOException if communications is cut with
* client
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
OutputStream out = null;
InputStream fileContent = null;
String rosterFolderName = Roster.getDefault().getRosterLocation() + "roster" + File.separator;
String tempFolderName = System.getProperty("java.io.tmpdir");
if (!tempFolderName.endsWith(File.separator)) {
//make sure path ends with a separator
tempFolderName += File.separator;
}
Locale rl = request.getLocale();
//get the uploaded file(s)
List<FileMeta> files = MultipartRequestHandler.uploadByJavaServletAPI(request);
List<String> msgList = new ArrayList<>();
//loop thru files returned and validate and (if ok) save each
for (FileMeta fm : files) {
log.debug("processing uploaded '{}' file '{}' ({}), group='{}', roster='{}', temp='{}'", fm.getFileType(), fm.getFileName(), fm.getFileSize(), fm.getRosterGroup(), rosterFolderName, tempFolderName);
//only allow xml files or image files
if (!fm.getFileType().equals("text/xml") && !fm.getFileType().startsWith("image")) {
String m = String.format(rl, Bundle.getMessage(rl, "ErrorInvalidFileType"), fm.getFileName(), fm.getFileType());
log.error(m);
msgList.add(m);
//stop processing this one
break;
}
//save received file to temporary folder
File fileTemp = new File(tempFolderName + fm.getFileName());
try {
out = new FileOutputStream(fileTemp);
fileContent = fm.getContent();
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = fileContent.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
log.debug("file '{}' of type '{}' temp saved to {}", fm.getFileType(), fm.getFileName(), tempFolderName);
} catch (IOException e) {
String m = String.format(rl, Bundle.getMessage(rl, "ErrorSavingFile"), fm.getFileName());
log.error(m);
msgList.add(m);
//stop processing this one
break;
} finally {
if (out != null) {
out.close();
}
if (fileContent != null) {
fileContent.close();
}
}
//finally
//reference to target file name and location
File fileNew = new File(rosterFolderName + fm.getFileName());
//save image file, replacing if parm is set that way. return appropriate message
if (fm.getFileType().startsWith("image")) {
if (fileNew.exists()) {
if (!fm.getFileReplace()) {
String m = String.format(rl, Bundle.getMessage(rl, "ErrorFileExists"), fm.getFileName());
log.error(m);
msgList.add(m);
//get rid of temp file
fileTemp.delete();
} else {
//delete the old file
fileNew.delete();
if (fileTemp.renameTo(fileNew)) {
String m = String.format(rl, Bundle.getMessage(rl, "FileReplaced"), fm.getFileName());
log.debug(m);
msgList.add(m);
} else {
String m = String.format(rl, Bundle.getMessage(rl, "ErrorRenameFailed"), fm.getFileName());
log.error(m);
msgList.add(m);
//get rid of temp file
fileTemp.delete();
}
}
} else {
if (fileTemp.renameTo(fileNew)) {
String m = String.format(rl, Bundle.getMessage(rl, "FileAdded"), fm.getFileName());
log.debug(m);
msgList.add(m);
} else {
String m = String.format(rl, Bundle.getMessage(rl, "ErrorRenameFailed"), fm.getFileName());
log.error(m);
msgList.add(m);
//get rid of temp file
fileTemp.delete();
}
}
} else {
// create a temp rosterentry to check, based on uploaded file
RosterEntry reTemp = null;
try {
reTemp = RosterEntry.fromFile(new File(tempFolderName + fm.getFileName()));
} catch (JDOMException e) {
//handle XML failures
String m = String.format(rl, Bundle.getMessage(rl, "ErrorInvalidXML"), fm.getFileName(), e.getMessage());
log.error(m);
msgList.add(m);
//get rid of temp file
fileTemp.delete();
break;
}
//get existing entry if found
RosterEntry reOld = Roster.getDefault().getEntryForId(reTemp.getId());
if (reOld != null) {
if (!fm.getFileReplace()) {
String m = String.format(rl, Bundle.getMessage(rl, "ErrorFileExists"), fm.getFileName());
log.error(m);
msgList.add(m);
//get rid of temp file
fileTemp.delete();
} else {
//replace specified
//remove the old entry from roster
Roster.getDefault().removeEntry(reOld);
//saves XML file to roster folder and makes backup
reTemp.updateFile();
//add the new entry to roster
Roster.getDefault().addEntry(reTemp);
//save modified roster.xml file
Roster.getDefault().writeRoster();
String m = String.format(rl, Bundle.getMessage(rl, "RosterEntryReplaced"), fm.getFileName(), reTemp.getDisplayName());
log.debug(m);
msgList.add(m);
//get rid of temp file
fileTemp.delete();
}
} else {
//move the file to proper location
fileTemp.renameTo(fileNew);
Roster.getDefault().addEntry(reTemp);
Roster.getDefault().writeRoster();
String m = String.format(rl, Bundle.getMessage(rl, "RosterEntryAdded"), fm.getFileName(), reTemp.getId());
log.debug(m);
msgList.add(m);
}
}
}
//for FileMeta
//respond with a json list of messages from the upload attempts
response.setContentType("application/json");
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(response.getOutputStream(), msgList);
}
use of org.jdom2.Text in project JMRI by JMRI.
the class SignalMastIconXml method load.
/**
* Create a SignalMastIcon, then add
*
* @param element Top level Element to unpack.
* @param o an Editor as an Object
*/
@Override
public void load(Element element, Object o) {
// create the objects
Editor ed = (Editor) o;
SignalMastIcon l = new SignalMastIcon(ed);
String name;
Attribute attr;
/*
* We need to set the rotation and scaling first, prior to setting the
* signalmast, otherwise we end up in a situation where by the icons do
* not get rotated or scaled correctly.
**/
try {
int rotation = 0;
double scale = 1.0;
// former attribute name.
attr = element.getAttribute("rotation");
if (attr != null) {
rotation = attr.getIntValue();
}
attr = element.getAttribute("degrees");
if (attr != null) {
rotation = attr.getIntValue();
}
l.rotate(rotation);
attr = element.getAttribute("scale");
String text = "Error attr null";
if (attr != null) {
scale = attr.getDoubleValue();
text = attr.getValue();
}
l.setScale(scale);
if (log.isDebugEnabled()) {
log.debug("Load SignalMast rotation= " + rotation + " scale= " + scale + " attr text= " + text);
}
} catch (org.jdom2.DataConversionException e) {
log.error("failed to convert rotation or scale attribute");
}
attr = element.getAttribute("signalmast");
if (attr == null) {
log.error("incorrect information for signal mast; must use signalmast name");
ed.loadFailed();
return;
} else {
name = attr.getValue();
if (log.isDebugEnabled()) {
log.debug("Load SignalMast " + name);
}
}
SignalMast sh = jmri.InstanceManager.getDefault(jmri.SignalMastManager.class).getSignalMast(name);
if (sh != null) {
l.setSignalMast(name);
} else {
log.error("SignalMast named '" + attr.getValue() + "' not found.");
ed.loadFailed();
// return;
}
attr = element.getAttribute("imageset");
if (attr != null) {
l.useIconSet(attr.getValue());
}
attr = element.getAttribute("imageset");
if (attr != null) {
l.useIconSet(attr.getValue());
}
try {
attr = element.getAttribute("clickmode");
if (attr != null) {
l.setClickMode(attr.getIntValue());
}
} catch (org.jdom2.DataConversionException e) {
log.error("Failed on clickmode attribute: " + e);
}
try {
attr = element.getAttribute("litmode");
if (attr != null) {
l.setLitMode(attr.getBooleanValue());
}
} catch (org.jdom2.DataConversionException e) {
log.error("Failed on litmode attribute: " + e);
}
ed.putItem(l);
// load individual item's option settings after editor has set its global settings
loadCommonAttributes(l, Editor.SIGNALS, element);
}
Aggregations