What is "Document" keyword and ".something" means? - java

I have this code:
Public static List <LinkNode> parse (LinkNode inputLink) {
List <LinkNode> outputLinks = new LinkList<>();
try {
Document parsedResults = Jsoup
.connect (inputLink.getUrl ())
.timeout (READ_TIMEOUT_IN_MILLISSECS)
.get ();
}
String tag;
Elements elements;
List <LinkNode> result;
}
I have few questions about the code:
Is Document a reserved keyword Java?
try {
Document parsedResults = Jsoup
.connect (inputLink.getUrl ())
.timeout (READ_TIMEOUT_IN_MILLISSECS)
.get ();
What is this form of coding (dot something)? Can I write it in another form If yes can you show me how?
.connect (inputLink.getUrl ())
.timeout (READ_TIMEOUT_IN_MILLISSECS)
.get ();

No, it is not a reserved keyword. In this case, Document is the name of a class defined in the Jsoup library. Other libraries can have their own class named Document, or you could write your own. For example:
In contrast, reserved keywords cannot be used as identifiers (i.e. class names, function names, variable names). These words include class, try, if, while, public, private, and more. See a bigger list here.
The dot (.) operator allows you to access an object's functions or variables. For example rectangle.width or dog.run().
Your example shows method chaining, in which subsequent methods are called on the object returned by the previous method. This removes the need for creating intermediate variables and makes things easier (for you as a programmer and also someone that is reading your code).
You could achieve exactly the same result with more code:
Connection conn1 = Jsoup.connect(inputLink.getUrl ());
Connection conn2 = conn1.timeout(READ_TIMEOUT_IN_MILLISSECS);
Document parsedResults = conn2.get();

1) No, it's a class defined in the Jsoup library.
2) It's part of a normal method call, as in Integer.parseInt("5") or scanner.readLine().

Related

How to convert string to By type

How to convert String to By type.
Following is my scenario:
Keep object identification in Properties file in below manner
username=By.id("username")
password=By.id("password")
In the application i would like to retrieve the values like
Properties prop=new Properties();
prop.load("above properties file path")
driver.findelement(prop.getProperty("username")) //Here in eclipse it is complaining saying "The method findElement(By) in the type WebDriver is not applicable for the arguments (String)"
So can somebody help me in this?
I can use like below or some other format, but i want solution for the above
username="//*[#id='username']"
username="username"
driver.findElement(By.xpath(prop.getProperty("username"))
driver.findElement(By.id(prop.getProperty("username"))
You can create one parser method which will return desired locator object something like below:
public static By locatorParser(String locator) {
By loc = By.id(locator);
if (locator.contains("id"))
loc = By.id(locator.substring(locator.indexOf("\"") + 1,
locator.length() - 2));
else if (locator.contains("name"))
loc = By.name(locator.substring(locator.indexOf("\"") + 1,
locator.length() - 2));
if (locator.contains("xpath"))
loc = By.xpath(locator.substring(locator.indexOf("\"") + 1,
locator.length() - 2));
return loc;
}
Can be called in your code in the following way:
driver.findElement(locatorParser(prop.getProperty("username")));
Added logic for id, name, xpath. You can modify the same method to add all the available locators. Hope this helps!
The WebDriver.findElement method accepts only an object parameter of the type By.
The Property.getProperty method returns only a String typed object.
Therefore, this may be what fits your need:
WebElement element = driver.findElement(By.name(prop.getProperty("username")));
You can't force a String typed object into a method that accepts only a By typed object. When you ask Selenium to find a String "username" you have to tell it more than just the string's value.
The method By.[method] you choose all depends on what you are looking for in the page that Selenium is searching. "username" is most likely the "name" (By.name) or "id" (By.Id) of the field you are looking for. The By class refines the search to where you expect the String "username" to be: in a name, id, tag, class, etc. See the By class definition.
Also, take caution as the getProperty method could return a null, and the By methods with throw an IllegalArgumentException if you pass it a null string. So providing a default return value ("") for getProperty is usually safer.
WebElement element = driver.findElement(By.name(prop.getProperty("username", "")));
You have to evaluate the entire expression within the context of existing code. You should framework such as JEXL or expressionoasis
Code below uses JEXL
// Create JexlEngine instance
JexlEngine jexl = new JexlEngine();
// Create Expression object for the string
Expression e = jexl.createExpression(prop.getProperty("username"));
// Create a context. You can pass reference to any object you want to use inside the expression
JexlContext jc = new MapContext();
// Now evaluate the expression, getting the result
driver.findElement((By)e.evaluate(jc));
I think you are trying to implement page object model using properties file. What I would suggest here to use xml file instead of java properties file. for example sample xml for page elements would look like below.
<Pages>
<LoginPage>
<txtUserName by="id">username</txtUserName>
<txtPassword by="id">password</txtPassword>
</LoginPage>
</Pages>
Now you can write methods retrieve nodes from the xml file and node attribute as id,xpath etc... further write methods to find elements using xml node value and attribute. I have used same method in my project as it works great for me.

Java Fluent API - better method?

Say I have an object that I've created to further simplify reading an XML document using the DOM parser. In order to "step into" a node or element, I'd like to use a single line to go from the start of the document to my target data, buried somewhere within the document, while bypassing the extra "fluff" of the DOM parser (such as doc.getElementsByTagName("data").item(0) when there is only one item inside the "data" element).
For the sake of this question, let's just assume there are no duplicate element tags and I know where I need to navigate to to get the data I need from the document, of which the data is a simple string. The idea is to set the simplified reader up so that it can be used for other data in other locations in the document, as well, without having to write new methods all the time. Below is some example code I've tried:
public class SimplifiedReader {
Document doc;
Element ele;
public SimplifiedReader(Document doc) {
this.doc = doc;
ele = doc.getDocumentElement();
}
public SimplifiedReader fromRoot() {
ele = doc.getDocumentElement();
return this;
}
public SimplifiedReader withEle(String elementName) {
ele = ele.getElementsByTagName(elementName).item(0);
return this;
}
public String getTheData(String elementName) {
return ele.getTextContent();
}
}
Example XML File:
<?xml version="1.0" encoding="UTF-8"?>
<fileData>
<subData>
<targetData>Hello World!</targetData>
<otherData>FooBar!</otherData>
</subData>
</fileData>
This results in me being able to navigate the XML file, and retrieve the Strings "Hello World!" and "FooBar!" using this code:
SimplifiedReader sr = new SimplifiedReader(doc);
String hwData = sr.withEle("fileData").withEle("subData").getTheData("targetData");
String fbData = sr.getTheData("otherData");
Or, if I had to go to another thread to get the data "FooBar!", I would just do:
String fbData = sr.fromRoot().withEle("fileData2").withEle("subData2").getTheData("otherData");
Is there a better/more correct way to do this? Edit: Note: This question is more about the method of returning an object from a method inside of it (return this;) in order to reduce the amount of code written to access specific data stored within a tree format and not so much about how to read an XML file. (I originally thought this was the Singleton Pattern until William corrected me... thank you William).
Thanks in advance for any help.
I don't see any trace of the Singleton pattern here. It mostly resembles the Builder pattern, but isn't it, either. It just implements a fluent API.
Your approach seems very nice and practical.
I would perhaps advise not using fromRoot() but instead constructing a new instance each time. The instance is quite lightweight since all the heavyweight stuff resides in the Document instance it wraps.
You could even go immutable all the way, returning a new instance from withEle(). This buys you many cool properties, like the freedom to share the object around, each code path being free to use it as a starting point to fetch something specific relative to it, share it across threads, etc. The underlying Document is mutable, but usually this doesn't create real-life problems when the code is all about reading.
Is there a better/more correct way to do this?
Yes, there are many better ways to extract values from XML.
One would be to use XPath, for example with XMLBeam.
import java.io.IOException;
import org.xmlbeam.XBProjector;
import org.xmlbeam.annotation.XBDocURL;
import org.xmlbeam.annotation.XBRead;
public class App {
public static void main(String[] args) throws IOException {
FileDate fileDate = new XBProjector().io().fromURLAnnotation(FileDate.class);
System.out.println(fileDate.getTargetDate());
// Hello World!
System.out.println(fileDate.getOtherDate());
// FooBar!
}
#XBDocURL("resource://filedate.xml")
public interface FileDate {
#XBRead("/fileData/subData/targetData")
String getTargetDate();
#XBRead("/fileData/subData/otherData")
String getOtherDate();
}
}

Use fields in one tuplestream as part of regex in RegexParser on second tuplestream

I'm trying to read in a csv in the hdfs, parse it with cascading, and then use the resulting tuple stream to form the basis of regex expressions in another tuple stream using RegexParser. As far as I can tell, the only way to do this would be to write a custom Function of my own, and I was wondering if anybody knew how to use the Java API to do this instead.
Pointers on how to write my own function to do this inside the cascading framework would be welcome, too.
I'm running Cascading 2.5.1
The best resource for this question is the Palo Alto cascading example tutorial. It's in java and provides examples of a lot of use cases, including writing custom functions.
https://github.com/Cascading/CoPA/wiki
And yes, writing a function that allows an input regex that references other argument inputs is your best option.
public class SampleFunction extends BaseOperation implements Function
{
public void operate( FlowProcess flowProcess, FunctionCall functionCall )
{
TupleEntry argument = functionCall.getArguments();
String regex = argument.getString( 0 );
String argument = argument.getString( 1 );
String parsed = someRegexOperation();
Tuple result = new Tuple();
result.add( parsed );
functionCall.getOutputCollector().add( result );
}
}

How can I modify or remove properties values? - Jena API

I'm using Jena. I would like to know if there is a method that allows to modify or remove properties values of an instance?
Thanks
Statements in Jena are, by design, immutable. To change the value of a property p of some subject s, you need to add a new statement with the same subject and predicate, and remove the old statement. This is always true in Jena, even if the API sometimes hides this from you. For example, OntResource and its subclasses have a variety of setProperty variants, but under the hood these are performing the same add-the-new-triple-and-delete-the-old process.
It depends which Jena API you are using. For instance, if you are using Jena 3.0 and the Model API, you can use Model.remove(Statement) to remove a property by choosing the appropriate subject/predicate/object for the Statement. Modification can be achieved by removing the old version of a Statement and adding the new version.
To only remove the statement itself, i.e. the relation between the instance and the property value, you can use:
OntResource.removeProperty(Property, RDFNode)
If you want to remove the property value altogether, i.e. the value and all relations to it, you can use: OntResource.remove()
I had the similar task: I need to delete the property with the specified value. Hope the following code snippet will help someone.
public void removeLabel(String language, String value) {
NodeIterator nodeIterator = resource.getModel().listObjectsOfProperty(RDFS.label);
RDFNode foundToDelete = null;
while (nodeIterator.hasNext()) {
RDFNode next = nodeIterator.next();
boolean langsAreIdentical = next.asLiteral().getLanguage().equals(language);
boolean valuesAreIdentical = next.asLiteral().getLexicalForm().equals(value);
if (langsAreIdentical && valuesAreIdentical) {
foundToDelete = next;
break;
}
}
resource.getModel().remove(resource, RDFS.label, foundToDelete);
}

Get declared methods in order they appear in source code

The situation seems to be abnormal, but I was asked to build serializer that will parse an object into string by concatenating results of "get" methods. The values should appear in the same order as their "get" equivalent is declared in source code file.
So, for example, we have
Class testBean1{
public String getValue1(){
return "value1";
}
public String getValue2(){
return "value2";
}
}
The result should be:
"value1 - value2"
An not
"value2 - value1"
It can't be done with Class object according to the documentation. But I wonder if I can find this information in "*.class" file or is it lost? If such data exists, maybe, someone knows a ready to use tool for that purpose? If such information can't be found, please, suggest the most professional way of achieving the goal. I thought about adding some kind of custom annotations to the getters of the class that should be serialized.
If you want that you have to parse the source code, not the byte code.
There are a number of libraries that parse a source file into a node tree, my favorite is the javaparser (hosted at code.google.com), which, in a slightly modified version, is also used by spring roo.
On the usage page you can find some samples. Basically you will want to use a Visitor that listens for MethodDefinitions.
Although reflection does not anymore (as of java 7 I think) give you the methods in the order in which they appear in the source code, the class file appears to still (as of Java 8) contain the methods in the order in which they appear in the source code.
So, you can parse the class file looking for method names and then sort the methods based on the file offset in which each method was found.
If you want to do it in a less hacky way you can use Javassist, which will give you the line number of each declared method, so you can sort methods by line number.
I don't think the information is retained.
JAXB, for example, has #XmlType(propOrder="field1, field2") where you define the order of the fields when they are serialized to xml. You can implemenet something similar
Edit: This works only on concrete classes (the class to inspect has its own .class file). I changed the code below to reflect this. Until diving deeper into the ClassFileAnalyzer library to work with classes directly instead of reading them from a temporary file this limitation exists.
Following approach works for me:
Download and import following libarary ClassFileAnalyzer
Add the following two static methods (Attention! getClussDump() needs a little modification for writing out the class file to a temporary file: I removed my code here because it's very special at this point):
public static String getClassDump(Class<?> c) throws Exception {
String classFileName = c.getSimpleName() + ".class";
URL resource = c.getResource(classFileName);
if (resource == null) {
throw new RuntimeException("Works only for concreate classes!");
}
String absolutePath = ...; // write to temp file and get absolute path
ClassFile classFile = new ClassFile(absolutePath);
classFile.parse();
Info infos = new Info(classFile, absolutePath);
StringBuffer infoBuffer = infos.getInfos();
return infoBuffer.toString();
}
public static <S extends List<Method>> S sortMethodsBySourceOrder(Class<?> c, S methods) throws Exception {
String classDump = getClassDump(c);
int index = classDump.indexOf("constant_pool_count:");
final String dump = classDump.substring(index);
Collections.sort(methods, new Comparator<Method>() {
public int compare(Method o1, Method o2) {
Integer i1 = Integer.valueOf(dump.indexOf(" " + o1.getName() + lineSeparator));
Integer i2 = Integer.valueOf(dump.indexOf(" " + o2.getName() + lineSeparator));
return i1.compareTo(i2);
}});
return methods;
}
Now you can call the sortMethodsBySourceOrder with any List of methods (because sorting arrays is not very comfortable) and you will get the list back sorted.
It works by looking at the class dumps constant pool which in turn can be determined by the library.
Greetz,
GHad
Write your custom annotation to store ordering data, then use Method.getAnnotation(Class annotationClass)

Categories