I have a string input in my Mule flow. It passes through my Groovy Script and outputs XML. I originaly had the script followed by an XSLT converter to remove empty nodes and set the indent to "no" in the output tag. But now I removed it as I cannot use it in conjunction with my script if I want to keep the special characters (see previous question here).
Instead I now check each value before printing the nodes. But the problem I have is my XML needs to be unindented in order to work with my InDesign project I adapt the XML for. I lost that ability when I removed the XSLT so I fixed one problem but created another.
I found the method getPrinter(), I used it with the setAutoIndent(false) but it didn't change anything to the output and created no errors. Not to sure where to use it.
Here's my script :
public Boolean isEmpty(value){
if(value.toString().trim() == "" || value.toString().trim() == '' || value == null)
return true;
}
root = new XmlSlurper(false,false).parseText(payload)
if(root.name() == 'GetActivitiesResponse')
startEach = root.children().children()
else
startEach = root.children()
def xml = new StringWriter().with { w -> new groovy.xml.MarkupBuilder(w).with {
mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
escapeAttributes = false
getPrinter().setAutoIndent(false);
"w_import_saisie_web"() {
startEach.each { p -> "w_evenement"() {
if(!isEmpty(p.PresentationDate))
"w_dates"{ mkp.yieldUnescaped (p.PresentationDate.toString() + "
") }
if(!isEmpty(p.SubTitle))
"w_contexte"{ mkp.yieldUnescaped (p.SubTitle.toString() + "
") }
//if(!isEmpty(p.SubTitle))
"w_nom_evenement"{ /*p.GEVT_Type*/ mkp.yieldUnescaped ("Nom evenement" + "
") }
if(!isEmpty(p.Name))
"w_titre"{ mkp.yieldUnescaped (p.Name.toString() + "
")}
if(!isEmpty(p.ShortDescription) || !isEmpty(p.Teaser))
"w_texte"{mkp.yieldUnescaped (p.ShortDescription.toString() + p.Teaser.toString() + "
")}
p.SubEvents.children().each { q -> "w_bloc_sous_evenement"() {
if(!isEmpty(q.PresentationDate) || !isEmpty(q.Name))
"w_sous_eve_titre"{ mkp.yieldUnescaped (q.PresentationDate.toString() + q.Name.toString() + "
")}
if(!isEmpty(q.ShortDescription) || !isEmpty(q.Teaser) || !isEmpty(q.WebDescription))
"w_sous_eve_desc"{mkp.yieldUnescaped (q.ShortDescription.toString() + q.Teaser.toString() + q.WebDescription.toString() + "
")}
}
}
if(!isEmpty(p.Site) || !isEmpty(p.PresentationHours))
"w_coordonnees"{ mkp.yieldUnescaped ("teeeessdfsdfsdfst" + p.Site.toString() + ' - ' + p.PresentationHours.toString() + "
")}
}
}
}
}
w.toString()
}
Add an IndentPrinter when you create the MarkupBuilder.
def xml = new MarkupBuilder(new IndentPrinter(new PrintWriter(writer), "", true))
See this question:
groovy.xml.MarkupBuilder disable PrettyPrint
I tried a bunch of different things to see if setAutoIndent was effective (setting it before passing the IndentPrinter to the MarkupBuilder for example) and it didn't seem to have any effect. So, like you, I'm wondering about its purpose.
Realised I was searching too hard... just added this simple line to the toString() at the end...
w.toString().replaceAll(">\\s+<", "><").trim();
Related
How can I write that if more compact using java 8?
Optional.ofNullable(city).ifPresent(c -> {
if (!city.equalsIgnoreCase(district)) {
address.setCity(district + ", " + c);
}
});
As Eran commented, you can avoid the spurious detour over Optional by just directly checking for null:
if (city != null && !district.equalsIgnoreCase(city)) {
address.setCity(district + ", " + city);
}
Title is self-explanatory.
Instead of using:
if (link.contains(".com") || (link.contains(".net") || (link.contains(".org") || (link.contains(".info") || ("etc there are many domain names")) {
webView.loadUrl("https://www." + link);
} else {
webView.loadUrl("https://www." + link + ".com");
}
I want to do it this way if possible by declaring the String's values globally.
if (link.contains(domainNames)) {
webView.loadUrl("https://www." + link);
} else {
webView.loadUrl("https://www." + link + ".com");
}
Build a regex with the domain names, then test it, e.g.
private static final Pattern DOMAIN_NAMES = Pattern.compile("\\.(?i:com|net|org|info)$");
if (DOMAIN_NAMES.matcher(link).find()) {
webView.loadUrl("https://www." + link);
} else {
webView.loadUrl("https://www." + link + ".com");
}
Another mannual approach could be :
Domainnamelist.contains(substring from link that contains domain ending)
I have never done anything in java before so I really am a newb but while building the program I have ran into a snag that i just can't figure out. I will try to explain and show to the best of my abilities.
Here is what I am building
The UI
Here is the code I have so far to make it work.
private void jButtonGenerateActionPerformed(java.awt.event.ActionEvent evt) {
String ObjectName = jtObjectName.getText();
String ObjectBase = jcbBaseNPC.getSelectedItem().toString();
String NPCName = jtNPCName.getText();
String MinLevel = jtMinLevel.getText();
if (MinLevel != null && MinLevel.isEmpty())
MinLevel = MinLevel.replace(MinLevel, "minLevel" + MinLevel);{
}
//Alignment Combo Box Start
String Alignment = jcbAlignment.getSelectedItem().toString();
if (Alignment.contains("Good")) {
Alignment = Alignment.replace("Good", "255");
}
if (Alignment.contains("Neutral")) {
Alignment = Alignment.replace("Neutral", "127");
}
if (Alignment.contains("Evil")) {
Alignment = Alignment.replace("Evil", "0");
}
//Alignment Combo Box End
// Print to Output Box
jaOutput.append("object" + " " + ObjectName + " " + "of" + " " + ObjectBase +
"\n\tproperties" + "\n\tname" +" " + "\"" + NPCName + "\"" + MinLevel
);
What I can not understand it how to check to see if there is something entered in a String and if there is I need to add to it so the output looks like this.
minLevel 1100
maxLevel 1500
only thing I will be adding is the numbers so i need to add something like
minLevel + MinLevel
and if it is empty just skip it all together. If I add it to the append and its empty I will just get minLevel and i can't have it like that.
Any tips would be great.
Thank you all
Donald
so i am working on a project right now
1st time using Hibernate
in this projet i am using Swing too
i have a form with multiple jTextFields
public List<Object[]> getoperations(String a,String c,String n,String e,String d) {
SessionDao s=new SessionDao();
session=s.getSession();
Query q;
q=session.createQuery("select idTiers,beneficiaire,emetteur,montant,numcompte,t_param_nature_operation.libelleNature,dateValidite,dateCreation where");
if (a != null && !a.isEmpty()) { q+= " and codeBanque='" + a + "'"; }
if (c != null && !c.isEmpty()) { q += " and numCompte='" + c + "'"; }
if (n != null && !n.isEmpty()) { q += " and t_param_nature_operation_.libelleNature='" + n + "'"; }
if (e != null && !e.isEmpty()) { q += " and decision='" + e + "'"; }
if (d != null && !d.isEmpty()) { q += " and dateCreation='" + d + "'"; }
q+= " order by idTiers" ;
return q.list();
}
As you see I am making a test on the values to add them in the query.
My question is there a way to add those values?
since query +="" isn't working.
Personally, I would add Guava utils to my project and use isNotBlank()
function. Anyway, you can write your own static function that would
return true if not null and not empty and false otherwise, and later
use it. It'll make your code much clearer.
The above was my comment and I decided to show you this little piece of code.
public static boolean isBlank(String s) {
if (s == null)
return true;
if (s.isEmpty())
return true;
return false;
}
Now you can simply write:
//static import your isBlank() method
//import static package.classInWhichIsBlankIsDeclared;
if (!isBlank(a) { q+= " and codeBanque='" + a + "'"; }
if (!isBlank(b) { q+= " and codeBanque='" + b + "'"; }
if (!isBlank(c) { q+= " and codeBanque='" + c + "'"; }
if (!isBlank(d) { q+= " and codeBanque='" + d + "'"; }
It's much more readable so it'll be much easier to debug in case of errors in the future.
Please, have a look at DRY principle and follow it. If your issue require checking same condition 4 or 5 times (2 times should be enough to use DRY) consider writing a function. call it the way that it'll be human-friendly instead of combination of different logical statements.
DRY. Don't Repeat Yourself.
"Every piece of knowledge must have a single, unambiguous, authoritative representation within a system"
Wikipedia article about DRY
you should consider using Criteria. it's more clean when dealing with multiple where statements.
eg
Criteria cr = session.createCriteria(YourEntityClass.class);
cr.add(Restrictions.eq("property1", value1));
cr.add(Restrictions.eq("property2", value2));
List results = cr.list();
have a look at these examples here
I know both NetBeans and Eclipse has options where if you paste multi-line, un-escaped string into a string variable, it will automatically add escape characters and add line breaks in. Is there a way to reverse the process?
For example:
function ShowHideOptions(trigger, element) {
if( trigger ) {
document.getElementById( element ).style.display = "";
} else {
document.getElementById( element ).style.display = "none";
}
}
if pasted in to string becomes:
private static final String LABEL_JAVASCRIPT = "function ShowHideOptions(trigger, element) {\n"
+ " if( trigger ) {\n"
+ " document.getElementById( element ).style.display = \"\";\n"
+ " } else {\n"
+ " document.getElementById( element ).style.display = \"none\";\n"
+ " }\n"
+ "}";
I want reverse this process.
I believe your question warrants another question. Why?
If you reverse this, the quotes would not be escaped and therefore you would get errors. Example:
System.out.println("System.out.println("Test");");
^
Error, everything after this quote is
considered code
Notice the quotes aren't escaped. This code would generate an error where I marked it because the quotes apparently mean the string should end.
Also, if the newlines are reversed, this example:
System.out.println("test");
System.out.println("test2");
Would become:
System.out.println("test");System.out.println("test2");
The following code works fine. Please clarify the problem.
System.out.println(LABEL_JAVASCRIPT);