ResponseException in jaunt - exception

Here is the error
message: UserAgent.sendGET; response error
requestUrl: https://www.linkedin.com/directory/topics-c/
response:
requestURL: https://www.linkedin.com/directory/topics-c/
status: 999
here is my code
try {
Document doc = userAgent.visit(link);
Elements eles = doc.findEvery("<ul class=\"column quad-column\">");
for (int i = 0; i < eles.size(); i++) {
Elements href_keywords = eles.getElement(i).findEvery("<a href>");
for (int j = 0; j < href_keywords.size(); j++) {
keywords.add(href_keywords.getElement(j).getText());
}
}

you should find the elements like this:
Elements eles = userAgent.doc.findEvery("");
here is the full code:
package scrap;
import com.jaunt.*;
public class Scrap {
public static void main(String[] args) {
try {
UserAgent userAgent = new UserAgent();
userAgent.visit("https://www.linkedin.com/directory/topics-c/");
// System.out.println(userAgent.doc.innerHTML());
Elements eles = userAgent.doc.findEvery("<ul class=\"column quad-column\">");
for (int i = 0; i < eles.size(); i++) {
Elements href_keywords = eles.getElement(i).findEvery("<a href>");
for (int j = 0; j < href_keywords.size(); j++) {
/// here add to your LIST
System.out.println(href_keywords.getElement(j).getText());
}
}
} catch (JauntException e) {
System.err.println(e);
}
}
}

Related

How the performance of a JavaFx-MySQL application can be enhanced

In my JavaFx application, i'm loading an ObservableList when a button is clicked and then display the list in a table.
the controller code:
#FXML
private void initialize() throws SQLException, ParseException, ClassNotFoundException {
searchChoice.setItems(criteriaList);
searchChoice.getSelectionModel().selectFirst();
productIdColumn.setCellValueFactory(cellData -> cellData.getValue().productIdProperty());
unitColumn.setCellValueFactory(cellData -> cellData.getValue().unitProperty());
productTitleColumn.setCellValueFactory(cellData -> cellData.getValue().titleProperty());
productTypeColumn.setCellValueFactory(cellData -> cellData.getValue().typeProperty());
productUnitPriceColumn.setCellValueFactory(cellData -> Bindings.format("%.2f", cellData.getValue().unitPriceProperty().asObject()));
productQuantityColumn.setCellValueFactory(cellData -> cellData.getValue().quantityProperty().asObject());
productStatusColumn.setCellValueFactory(cellData -> cellData.getValue().productStatusProperty());
descriptionColumn.setCellValueFactory(cellData -> cellData.getValue().descriptionProperty());
reorderPointColumn.setCellValueFactory(cellData -> cellData.getValue().reOrderPointProperty().asObject());
surplusPointColumn.setCellValueFactory(cellData -> cellData.getValue().surplusPointProperty().asObject());
productIdColumn.setSortType(TableColumn.SortType.DESCENDING);
productTable.getSortOrder().add(productIdColumn);
productTable.setRowFactory(tv -> new TableRow<Product>() {
#Override
public void updateItem(Product item, boolean empty) {
super.updateItem(item, empty);
if (item == null) {
setStyle("");
} else if (item.getQuantity() < item.getReOrderPoint()) {
setStyle("-fx-background-color: tomato;");
} else if (item.getQuantity() > item.getSurplusPoint()) {
setStyle("-fx-background-color: darkorange;");
} else {
setStyle("-fx-background-color: skyblue;");
}
}
});
try {
ObservableList<Product> productData = ProductDAO.searchProducts();
populateProducts(productData);
String[] expireDate = new String[productData.size()];
String[] id = new String[productData.size()];
String[] existingStatus = new String[productData.size()];
for (int i = 0; i < productData.size(); i++) {
expireDate[i] = productData.get(i).getExpireDate();
id[i] = productData.get(i).getProductId();
existingStatus[i] = productData.get(i).getProductStatus();
DateFormat format = new SimpleDateFormat(app.values.getProperty("DATE_FORMAT_PATTERN"), Locale.ENGLISH);
Date expireDateString = format.parse(expireDate[i]);
Date in = new Date();
LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault());
Date today = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
if (expireDateString.before(today) && !existingStatus[i].equals(app.values.getProperty("STATUS_TYPE2"))) {
ProductDAO.updateProductStatus(id[i], app.values.getProperty("STATUS_TYPE3"));
}
if (expireDateString.after(today) && !existingStatus[i].equals(app.values.getProperty("STATUS_TYPE2"))) {
ProductDAO.updateProductStatus(id[i], app.values.getProperty("STATUS_TYPE1"));
}
}
ObservableList<Product> productDataRefreshed = ProductDAO.searchProducts();
populateProducts(productDataRefreshed);
ObservableList<Product> productCodesData = ProductDAO.getProductCodes();
ObservableList<Product> productTitlesData = ProductDAO.getProductTitles();
ObservableList<Product> productTypesData = ProductDAO.getProductTypes();
ObservableList<Product> productStatusData = ProductDAO.getProductStatus();
String possibleProducts1[] = new String[productCodesData.size()];
for (int k = 0; k < productCodesData.size(); k++) {
possibleProducts1[k] = productCodesData.get(k).getProductId();
}
String possibleProducts2[] = new String[productTitlesData.size()];
for (int k = 0; k < productTitlesData.size(); k++) {
possibleProducts2[k] = productTitlesData.get(k).getTitle();
}
String possibleProducts3[] = new String[productTypesData.size()];
for (int k = 0; k < productTypesData.size(); k++) {
possibleProducts3[k] = productTypesData.get(k).getType();
}
String possibleProducts4[] = new String[productStatusData.size()];
for (int k = 0; k < productStatusData.size(); k++) {
possibleProducts4[k] = productStatusData.get(k).getProductStatus();
}
TextFields.bindAutoCompletion(searchField, possibleProducts1);
TextFields.bindAutoCompletion(searchField, possibleProducts2);
TextFields.bindAutoCompletion(searchField, possibleProducts3);
TextFields.bindAutoCompletion(searchField, possibleProducts4);
} catch (SQLException e) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle(app.values.getProperty("ERROR_TITLE"));
alert.setHeaderText(app.values.getProperty("FAILURE_MESSAGE"));
alert.setHeaderText(app.values.getProperty("ERROR_GETTING_INFORMATION_FROM_DATABASE_MESSAGE"));
alert.showAndWait();
throw e;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
the service mysql query :
public static ObservableList<Product> searchProducts() throws SQLException, ClassNotFoundException {
String selectStmt = "SELECT * FROM product";
ResultSet rsPrdcts = DbUtil.dbExecuteQuery(selectStmt);
ObservableList<Product> productList = getProductList(rsPrdcts);
return productList;
}
The issue here is, when there are more than 200-300 items in the list the scene gets really slow to load. What countermeasures can I take regarding this matter? Any idea will be very much appreciated.
Thanks in advance.
You need to implement an ObservableList which only retrieves the data which is rqeusted by the TableView. Currently you retrive all elements in the table and cast the retrieved list to an ObservableList.
The TableView uses the .get(int idx) method of the OL to retrieve all items which should be displayed and the .size() method for determining the size of the scrollbar. When you scroll the TableView will discard all items which are not displayed and call the get method again.
To solve your problem need to create a class which implements ObservableList<E>. First you need to implement the .get(int idx) and the .size() method, for all other methods I would throw new UnsupportedOperationException() and later on see which other method is needed. So the .size() method needs to execute the following query
SELECT count(*) FROM product
and the get(int idx) something like this
int numItems = 30;
int offset = idx - (idx % numItems)
SELECT * FROM product LIMIT offset, numItems
you can create an internal list which only holds e.g. 30 items from your db and whenever the requested idx < offset || idx > offset + numItems you issue a new db request.
I used this this approach with database tables with millions of rows and had a very performant GUI. You can also add paging to the TableView because with to many rows the scrollbar gets useless, but this is a different discussion.
edit:
I forgot to mention that this is called Lazy Loading

Calling parents by name

Is there a way/plugin to call $parents["ExamViewModel"] instead of $parents[2]?
That would be very helpful for shared views that may show in different parent views.
No there is no such thing.
You could create your own method using instanceof as a cleaner way.
Something like:
function findParentOfType(parents, targetType) {
for (var i = 0; i < parents.length; i++) {
if (parents[i] instanceof targetType) return parents[i];
}
return null;
}
Usage:
findParentOfType($parents, ExamViewModel)
Example:
function Class1() {
this.name = 'MyClass1';
}
function Class2() {
this.name = 'MyClass2';
}
var parents = [new Class1(), new Class2()];
function findParentOfType(parents, targetType) {
for (var i = 0; i < parents.length; i++) {
if (parents[i] instanceof targetType) return parents[i];
}
return null;
}
console.log(findParentOfType(parents, Class2).name);

Desktop Duplication returns blank image

I'd used MS Desktop Duplication API to get the desktop image, and got a blank image.
But when run in debug mode and put a break point after AcquireNextFrame, the image can be retrived.
Did I miss something? The source code:
#include "stdafx.h"
typedef struct _DX_RESOURCES
{
ID3D11Device* Device;
ID3D11DeviceContext* Context;
} DX_RESOURCES;
HRESULT InitializeDX(DX_RESOURCES *res) {
HRESULT hr = S_OK;
// Driver types supported
D3D_DRIVER_TYPE DriverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE,
};
UINT NumDriverTypes = ARRAYSIZE(DriverTypes);
// Feature levels supported
D3D_FEATURE_LEVEL FeatureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_1
};
UINT NumFeatureLevels = ARRAYSIZE(FeatureLevels);
D3D_FEATURE_LEVEL FeatureLevel;
// Create device
for (UINT DriverTypeIndex = 0; DriverTypeIndex < NumDriverTypes; ++DriverTypeIndex)
{
hr = D3D11CreateDevice(nullptr, DriverTypes[DriverTypeIndex], nullptr, 0, FeatureLevels, NumFeatureLevels,
D3D11_SDK_VERSION, &res->Device, &FeatureLevel, &res->Context);
if (SUCCEEDED(hr))
{
break;
}
}
return hr;
}
DX_RESOURCES dxRes;
int main()
{
HRESULT hr = InitializeDX(&dxRes);
if (FAILED(hr)) {
return -1;
}
D3D11_TEXTURE2D_DESC texdes;
texdes.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
texdes.BindFlags = 0;
texdes.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
texdes.Height = 1080;
texdes.Width = 1920;
texdes.MiscFlags = 0;
texdes.MipLevels = 1;
texdes.ArraySize = 1;
texdes.SampleDesc.Count = 1;
texdes.SampleDesc.Quality = 0;
texdes.Usage = D3D11_USAGE_STAGING;
ID3D11Texture2D* screenTexture;
hr = dxRes.Device->CreateTexture2D(&texdes, NULL, &screenTexture);
if (FAILED(hr))
{
return -1;
}
IDXGIDevice* DxgiDevice = nullptr;
hr = dxRes.Device->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast<void**>(&DxgiDevice));
if (FAILED(hr))
{
return -1;
}
IDXGIAdapter* DxgiAdapter = nullptr;
hr = DxgiDevice->GetParent(__uuidof(IDXGIAdapter), reinterpret_cast<void**>(&DxgiAdapter));
DxgiDevice->Release();
DxgiDevice = nullptr;
if (FAILED(hr))
{
return -1;
}
IDXGIOutput* DxgiOutput = nullptr;
hr = DxgiAdapter->EnumOutputs(0, &DxgiOutput);
DxgiAdapter->Release();
DxgiAdapter = nullptr;
if (FAILED(hr))
{
return -1;
}
DXGI_OUTPUT_DESC OutputDesc;
DxgiOutput->GetDesc(&OutputDesc);
IDXGIOutput1* DxgiOutput1 = nullptr;
hr = DxgiOutput->QueryInterface(__uuidof(DxgiOutput1), reinterpret_cast<void**>(&DxgiOutput1));
DxgiOutput->Release();
DxgiOutput = nullptr;
if (FAILED(hr))
{
return -1;
}
IDXGIOutputDuplication* DeskDupl;
// Create desktop duplication
hr = DxgiOutput1->DuplicateOutput(dxRes.Device, &DeskDupl);
DxgiOutput1->Release();
DxgiOutput1 = nullptr;
if (FAILED(hr))
{
if (hr == DXGI_ERROR_NOT_CURRENTLY_AVAILABLE)
{
return -1;
}
return -1;
}
DXGI_OUTDUPL_FRAME_INFO duplicateFrameInformation;
IDXGIResource* screenResource;
ID3D11Texture2D* acquiredDesktopImage;
DWORD t1 = GetTickCount();
hr = DeskDupl->AcquireNextFrame(500, &duplicateFrameInformation, &screenResource);
if (hr == DXGI_ERROR_WAIT_TIMEOUT)
{
return 1;
}
if (FAILED(hr))
{
return -1;
}
hr = screenResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void **>(&acquiredDesktopImage));
screenResource->Release();
screenResource = nullptr;
if (FAILED(hr))
{
return -1;
}
ID3D11DeviceContext* context;
dxRes.Device->GetImmediateContext(&context);
context->CopyResource(screenTexture, acquiredDesktopImage);
IDXGISurface* screenSurface;
hr = screenTexture->QueryInterface(__uuidof(IDXGISurface), reinterpret_cast<void **>(&screenSurface));
if (FAILED(hr))
{
return -1;
}
DXGI_SURFACE_DESC desc;
screenSurface->GetDesc(&desc);
DXGI_MAPPED_RECT rect;
hr = screenSurface->Map(&rect, DXGI_MAP_READ);
if (FAILED(hr))
{
return -1;
}
FILE *fp;
fp = fopen("d:\\test.rgba", "wb");
fwrite(rect.pBits, 1, 1920 * 1080 * 4, fp);
fclose(fp);
screenSurface->Unmap();
acquiredDesktopImage->Release();
acquiredDesktopImage = nullptr;
DWORD t2 = GetTickCount() - t1;
printf("OK\n");
return t2;
}
afert AcquireNextFrame, The output value of [duplicateFrameInformation] should be checked.
// Get metadata
if (duplicateFrameInformation.TotalMetadataBufferSize)
{
....do some process
}

all the names are not fetching from arraylist

Json file show on arraylist, I want to show all the names from the arraylist from the json file.
try {
JSONArray root = new JSONArray(json);
for (int i = 0; i < root.length(); i++)
{
JSONObject att = (JSONObject) root.getJSONObject(i);
name = att.getString("nm");
list.add(name);
}
for(int i = 0; i<root.length(); i++){
Log.i("name", name);
}
}catch (JSONException e) {
e.printStackTrace();
}
try{
JSONArray root = new JSONArray(json);
for (int i = 0; i < root.length(); i++){
JSONObject att = (JSONObject) root.getJSONObject(i);
name = att.getString("nm");
list.add(name);
}
for(int j = 0; j<feedList.size(); j++){
Log.i("name",feedList.get(j));
}
}catch (JSONException e) {
e.printStackTrace();
}

How can I convert the decimal representation of an IP address into binary?

Does anyone knows how to convert decimal notation of an IP address into binary form in Java? Please let me know...
An IP address written as a.b.c.d can be converted to a 32-bit integer value
using shift and bit-wise inclusive OR operators as,
(a << 24) | (b << 16) | (c << 8) | d
To be safe, each of a,b,c,d has valid range 0-255 -- you can check that in your conversion.
You can further validate the IP address using this regex example.
You can use the java.net.InetAddress class. Two methods you should look at are getByName and getAddress. Here is a simple code example
import java.net.InetAddress;
import java.net.UnknownHostException;
/* ... */
String ip = "192.168.1.1";
InetAddress address = null;
try {
address = InetAddress.getByName(ip);
} catch (UnknownHostException e) {
//Your String wasn't a valid IP Address or host name
}
byte [] binaryIP = address.getAddress();
Gathering your suggestions and some other sources, I found usefull to convert an InetAdress to an array of bit, as well as BitSet, which can help to compute and(), or(), xor() out of your binary representation.
Following sample shows how to convert ip to binary and binary to ip.
Enjoy!
public class IpConverter {
public static void main(String[] args) {
String source = "192.168.1.1";
InetAddress ip = null;
try {
ip = InetAddress.getByName(source);
} catch (UnknownHostException e) {
e.printStackTrace();
return;
}
System.out.println( "source : " + ip);
// To bit sequence ------------
byte[] binaryIP = ip.getAddress();
BitSet[] bitsets = new BitSet[binaryIP.length];
int k = 0;
System.out.print("to binary: ");
for (byte b : binaryIP) {
bitsets[k] = byteToBitSet(b);
System.out.print( toString( bitsets[k] ) + ".");
k++;
}
System.out.println();
// Back to InetAdress ---------
byte[] binaryIP2 = new byte[4];
k = 0;
for (BitSet b : bitsets) {
binaryIP2[k] = bitSetToByte(b);
k++;
}
InetAddress ip2 = null;
try {
ip2 = InetAddress.getByAddress(binaryIP2);
} catch (UnknownHostException e) {
e.printStackTrace();
return;
}
System.out.println( "flipped back to : " + ip2);
}
public static BitSet byteToBitSet(byte b) {
BitSet bits = new BitSet(8);
for (int i = 0; i < 8; i++) {
bits.set(i, ((b & (1 << i)) != 0) );
}
return bits;
}
public static byte bitSetToByte(BitSet bits) {
int value = 0;
for (int i = 0; i < 8; i++) {
if (bits.get(i) == true) {
value = value | (1 << i);
}
}
return (byte) value;
}
public static byte bitsToByte(boolean[] bits) {
int value = 0;
for (int i = 0; i < 8; i++) {
if (bits[i] == true) {
value = value | (1 << i);
}
}
return (byte) value;
}
public static boolean[] byteToBits(byte b) {
boolean[] bits = new boolean[8];
for (int i = 0; i < bits.length; i++) {
bits[i] = ((b & (1 << i)) != 0);
}
return bits;
}
public static String toString(BitSet bits){
String out = "";
for (int i = 0; i < 8; i++) {
out += bits.get(i)?"1":"0";
}
return out;
}
}
The open-source IPAddress Java library can do this for you. It can parse various IP address formats, including either IPv4 or IPv6, and has methods to produce various string formats, including one for binary. Disclaimer: I am the project manager of the IPAddress library.
This code will do it:
static void convert(String str) {
IPAddressString string = new IPAddressString(str);
IPAddress addr = string.getAddress();
System.out.println(addr + " in binary is " + addr.toBinaryString());
}
Example:
convert("1.2.3.4");
convert("a:b:c:d:e:f:a:b");
The output is:
1.2.3.4 in binary is 00000001000000100000001100000100
a:b:c:d:e:f:a:b in binary is 00000000000010100000000000001011000000000000110000000000000011010000000000001110000000000000111100000000000010100000000000001011