Replace XML value in Velocity - java

I will preface the below with stating i am new to Java in general:
I have the need to insert the a value into an already existing XML in the below case for the attribute "directory".
<?xml version="1.0" encoding="UTF-8"?>
<DTABLEITEM Name="0216.11 neu">
<DVALUE Name="Type" Value="2"/>
<DVALUE Name="Workspace" Value="ST_0216.11.ARD"/>
<DVALUE Name="IntSetupFile" Value=""/>
<DVALUE Name="IntDocPlotCmd" Value=""/>
<DVALUE Name="Directory" Value=""/>
<DVALUE Name="Resource" Value=""/>
/DTABLEITEM>
Below is an example of code that works in one use case where i was able to replace a value easily.
#foreach ($item1 in $xmlf1.find("/."))
$item1.toString().replace("<<wfp.Macro_Settings_Name/>>","(Broken or Missing) <<wfp.Macro_Settings_Name/>>")
#end
The below returns my XML without any modifications but after many hours searching i cannot find anything that works for me either i need a way of replacing that whole line as a string or a way to set the value neither of which i have been successful with.
I understand the below is simply loading the XML and printing
#foreach ($item1 in $xmlf1.find("/."))
$item1
#end

Related

Language.properties file in Liferay

I want to support multiple languages for my portlet application. So I:
Created content package in src folder.
Created Language.properties file with string Book-Name=Book Name
Paste this line
<supported-locale>es</supported-locale>
<resource-bundle>content/Language</resource-bundle>
in portlet.xml file.
So could you please tell me why I still have Book-Name here?!
<liferay-ui:search-container>
<liferay-ui:search-container-results results="${bookListArray}" />
<liferay-ui:search-container-row className="com.softwerke.model.Book" modelVar="aBook">
<liferay-ui:search-container-column-text property="bookName" name="Book-Name"/>
<liferay-ui:search-container-column-text property="bookDescription" name="Description"/>
<liferay-ui:search-container-column-jsp path="/html/action.jsp" align="right" />
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
</liferay-ui:search-container>
UPDATE
This:
<liferay-ui:search-container-column-text property="bookName" name="${bookName}" />
....
<c:set var="bookName"> <liferay-ui:message key="book-Name" /> </c:set>
does NOT work too
You are not using it at all.
The name="Book-Name" in this line
<liferay-ui:search-container-column-text property="bookName" name="Book-Name"/>
adds name property to this html component with valye defined inside the quotation marks to make this value the one defined in the properties file you have to use <liferay-ui:message /> tag in your case it would be:
:
<liferay-ui:search-container-column-text property="bookName" name="<liferay-ui:message key="Book-Name" />"/>
Also it is not relevant but a dev practice that the language key is all lower case.

How to add a LIST item with Java API Repository in Oracle ATG?

I create this repository:
<gsa-template>
<item-descriptor name="indirizzo" >
<table name="INDIRIZZO" type="primary" id-column-name="ID_INDIRIZZO">
<property name="via" data-type="string" column-name="VIA" />
<property name="civico" data-type="int" column-name="CIVICO" />
</table>
</item-descriptor>
<item-descriptor name="utente" >
<table name="UTENTE" type="primary" id-column-name="ID_UTENTE">
<property name="nome" data-type="string" column-name="NOME" />
<property name="cognome" data-type="string" column-name="COGNOME" />
<property name="indirizzi" data-type="list" component-item-type="indirizzo" />
</table>
</item-descriptor>
In the Java class I want to add a new user with a multiple address. Then I want to use the Java API repository. I tried this:
MutableRepositoryItem item_utente = getMutableRepository().createItem(
UTENTE);
MutableRepositoryItem item_indirizzo = getMutableRepository().createItem(
INDIRIZZO);
item_indirizzo.setPropertyValue(VIA, v1);
item_indirizzo.setPropertyValue(CIVICO, civ1);
getMutableRepository().addItem(item_indirizzo);
item_indirizzo.setPropertyValue(VIA, v2);
item_indirizzo.setPropertyValue(CIVICO, civ2);
getMutableRepository().addItem(item_indirizzo);
item_utente.setPropertyValue(NOME, n);
item_utente.setPropertyValue(COGNOME, c);
item_utente.setPropertyValue(INDIRIZZI, item_indirizzo);
getMutableRepository().addItem(item_utente);
but it does not work, I SUPPOSE because I did not create an actual java list.
I want to insert in my DB an user with respective 2 or more addresses.
Some different idea or do I fix my code?
If your datatype is list, you can add items using an xml just like you do for primitive data types with comma separated values as below:
<add-item item-descriptor="utente" id="test1">
<set-property name="nome" value="testNome"/>
<set-property name="cognome" value="testCognome"/>
<set-property name="indirizzi" value="test2,test3,test4"/>
</add-tem>
Take look at below page for more details.
http://docs.oracle.com/cd/E24152_01/Platform.10-1/ATGRepositoryGuide/html/s1302setproperty01.html
create the List of Repository item .
List<RepositoryItem> item_indirizzo_List=new ArrayList<RepositoryItem>();
add item to the list. And then add like below
item_utente.setPropertyValue(INDIRIZZI, item_indirizzo_List);
May be this will help you.

How to add Prefix for the attribute while marshling

I like to add prefix for attribute while marshaling using castors.
I would like to get result as like below
<ThesaurusConcept dc:identifier="C268">
<ScopeNote xml:lang="en">
<LexicalValue>index heading is Atomic absorption spectroscopy</LexicalValue>
</ScopeNote>
</ThesaurusConcept>
but I am getting
<ThesaurusConcept identifier="C621">
<ScopeNote lang="en">
<LexicalValue>index heading is Atomic absorption spectroscopy</LexicalValue>
</ScopeNote>
</ThesaurusConcept>
I got an answer for my question
we need to add the following in mapping.xml file
<mapping xmlns:dc="http://purl.org/dc/elements/1.1/">
<bind-xml name="dc:identifier" node="attribute" ></bind-xml>
and also we need to set namespace by using following code.
Marshaller casreactmp = new Marshaller(handler);
casreactmp.setNamespaceMapping("dc", "http://purl.org/dc/elements/1.1/");

Using regex in web harvest xml

I'm using web harvest to scrap some e-commerce site.I'm iterating over the search page and getting each product details in output xml.But now I want to use regular expression in anchor(a) tag while scraping and get particular string.i.e.,
let $linktoprod :=data($item//a[#class="fk-anchor-link"]/#href)
The above line returns anchor tag href value of each product i.e., for first product the value returned is,
/casio-sheen-analog-watch-women/p/itmdaqmvzyy23hz5?pid=WATDAQMVVNQEM9CX&ref=6df83d8f-f61f-4648-b846-403938ae92fa
Now I want to using the regular expression like /([^/\?]+)\? and get the string between last / and ? i.e.,
itmdaqmvzyy23hz5
in the output xml.
Please anyone who has any idea regarding this help me.
Thank you.
Updated -
<?xml version="1.0" encoding="UTF-8"?>
<config charset="ISO-8859-1">
<function name="download-multipage-list">
<return>
<while condition="${pageUrl.toString().length() != 0}" maxloops="${maxloops}" index="i">
<empty>
<var-def name="content">
<html-to-xml>
<http url="${pageUrl}"/>
</html-to-xml>
</var-def>
<var-def name="nextLinkUrl">
<xpath expression="${nextXPath}">
<var name="content"/>
</xpath>
</var-def>
<var-def name="pageUrl">
<template>${sys.fullUrl(pageUrl.toString(), nextLinkUrl.toString())}</template>
</var-def>
</empty>
<xpath expression="${itemXPath}">
<var name="content"/>
</xpath>
</while>
</return>
</function>
<var-def name="products">
<call name="download-multipage-list">
<call-param name="pageUrl">http://www.flipkart.com/watches/pr?sid=reh%2Cr18</call-param>
<call-param name="nextXPath">//a[starts-with(., 'Next')]/#href</call-param>
<call-param name="itemXPath">//div[#class="product browse-product "]</call-param>
<call-param name="pids"></call-param>
<call-param name="maxloops">5</call-param>
</call>
</var-def>
<var-def name="scrappedContent">
<!-- iterates over all collected products and extract desired data -->
<![CDATA[ <catalog> ]]>
<loop item="item" index="i">
<list><var name="products"/></list>
<body>
<xquery>
<xq-param name="item" type="node()"><var name="item"/></xq-param>
<xq-expression><![CDATA[
declare variable $item as node() external;
let $linktoprod :=data($item//a[#class="fk-anchor-link"]/#href)
let $name := data($item//div[#class="title"])
return
<product>
<link>{$linktoprod}</link>
<title>{normalize-space($name)}</title>
</product>
]]></xq-expression>
</xquery>
</body>
</loop>
<![CDATA[ </catalog> ]]>
</var-def>
</config>
My config xml is as show above.Where to use regexp code block in my xml? And I want the regexp to be applied to
linktoprod and finally get the regexp output in link tag as output xml.Please anyone guide me.
Thank you.
I don't know about web harvest, but if it supports a non greedy quantifier, you can use this pattern
/([^/]+?)\?
According to Web Harvest User manual - regexp you must insert something like this
<regexp>
<regexp-pattern>/([^/]+?)\?</regexp-pattern>
<regexp-source>
/casio-sheen-analog-watch-women/p/itmdaqmvzyy23hz5?pid=WATDAQMVVNQEM9CX&ref=6df83d8f-f61f-4648-b846-403938ae92fa
</regexp-source>
<regexp-result>
<template>Last URL part is "${_1}"</template>
</regexp-result>
</regexp>
In the <regexp-source> part you must insert your URL or variable to search for. Guessing from the manual and your config xml it might be something like
<regexp-source>
<var>scrappedContent</var>
</regexp-source>
or
<regexp-source>
${linktoprod}
</regexp-source>
I think you must experiment a bit.
Try this regex:
/([^/]+)\?
You might need to strip the leading / and trailing ?.
To illustrate that the regex works, this is it's result in JavaScript:
var s = "/casio-sheen-analog-watch-women/p/itmdaqmvzyy23hz5?pid=WATDAQMVVNQEM9CX&ref=6df83d8f-f61f-4648-b846-403938ae92fa"
console.log(s.match(/\/([^/]+)\?/g)); // /itmdaqmvzyy23hz5?

How to put a newline in ant property

This doesn't seem to work:
<property name="foo" value="\n bar \n"/>
I use the property value in the body of an e-mail message (which is sent as plain text):
<mail ...>
<message>some text${foo}</message>
and I get literal "\n" in the e-mail output.
These all work for me:
<property name="foo" value="bar${line.separator}bazz"/>
<property name="foo">bar
bazz2</property>
<property name="foo" value="bar
bazz"/>
You want ${line.separator}. See this post for an example. Also, the Ant echo task manual page has an example using ${line.separator}.
By using ${line.separator} you're simply using a Java system property. You can read up on the list of system properties here, and here is Ant's manual page on Properties.

Categories