Skip to content

Commit c724148

Browse files
Updated mutation observers handlers examples
1 parent bece30b commit c724148

File tree

3 files changed

+180
-62
lines changed

3 files changed

+180
-62
lines changed

content/english/java/mutation-observers-handlers/_index.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ url: /java/mutation-observers-handlers/
1010

1111
## Mutation Observers and Handlers in Aspose.HTML for Java Tutorials
1212
### [Advanced Mutation Observer with Aspose.HTML for Java](./mutation-observer/)
13-
### [Using Credential Handler in Aspose.HTML for Java](./credential-handler/)
13+
Learn how to implement an advanced Mutation Observer with Aspose.HTML for Java, tracking DOM changes seamlessly. Dive into our step-by-step guide.
14+
### [Using Credential Handler in Aspose.HTML for Java](./credential-handler/)
15+
Discover how to implement a secure Credential Handler using Aspose.HTML for Java to manage user authentication effectively.

content/english/java/mutation-observers-handlers/credential-handler/_index.md

+79-18
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,93 @@
22
title: Using Credential Handler in Aspose.HTML for Java
33
linktitle: Using Credential Handler in Aspose.HTML for Java
44
second_title: Java HTML Processing with Aspose.HTML
5-
description:
5+
description: Discover how to implement a secure Credential Handler using Aspose.HTML for Java to manage user authentication effectively.
66
type: docs
77
weight: 11
88
url: /java/mutation-observers-handlers/credential-handler/
99
---
10-
11-
## Complete Source Code
10+
## Introduction
11+
When working with web applications that require user credentials for authentication, managing those credentials effectively is crucial. That’s where Aspose.HTML for Java comes into play, providing tools to streamline this process. In this guide, we'll delve into how to implement a Credential Handler with Aspose.HTML for Java, ensuring secure operations in your applications.
12+
## Prerequisites
13+
Before jumping into the implementation, it’s essential to ensure you have everything set up correctly. Let’s go over what you need:
14+
### 1. Java Development Environment
15+
- Make sure you have JDK installed on your machine. A good IDE like IntelliJ IDEA or Eclipse can facilitate your coding journey.
16+
### 2. Aspose.HTML for Java
17+
- Download the Aspose.HTML for Java library from [here](https://releases.aspose.com/html/java/). This library will provide all necessary functionalities to work with HTML and web resources.
18+
### 3. Basic Knowledge of Java
19+
- Familiarity with Java programming, Object-Oriented principles, and networking concepts will be beneficial.
20+
### 4. Access to Credentials
21+
- Ensure you have valid user credentials for testing. For security reasons, do not store them in plain text.
22+
## Import Packages
23+
Let’s start by importing the necessary packages that your Java file will require. Here’s how you can set it up:
1224
```java
13-
package com.aspose.html.documentation.examples;
14-
1525
import com.aspose.html.net.INetworkOperationContext;
1626
import com.aspose.html.net.MessageHandler;
17-
18-
// START_SNIPPET MessageHandlers_CredentialHandler
19-
27+
```
28+
With the required packages imported, you are now ready to implement a credential handler. Below is a step-by-step guide to creating one.
29+
## Step 1: Create a Custom Credential Handler Class
30+
In this step, you will create a new Java class that extends the `MessageHandler` abstract class.
31+
```java
2032
public class CredentialHandler extends MessageHandler {
21-
@Override
22-
public void invoke(INetworkOperationContext context) {
23-
// TODO: NetworkCredential is hidden class
24-
// context.getRequest().setCredentials(new com.aspose.ms.System.Net.NetworkCredential("username", "securelystoredpassword");
25-
context.getRequest().setPreAuthenticate(true);
26-
27-
invoke(context);
33+
...
34+
}
35+
```
36+
This class will override the `invoke` method, enabling you to modify how network requests are handled.
37+
## Step 2: Override the `invoke` Method
38+
You’ll need to implement the logic for handling network requests and credentials within this method.
39+
```java
40+
@Override
41+
public void invoke(INetworkOperationContext context) {
42+
// Setting credentials
43+
context.getRequest().setCredentials(new com.aspose.ms.System.Net.NetworkCredential("username", "securelystoredpassword"));
44+
context.getRequest().setPreAuthenticate(true);
45+
46+
// Call the next handler in the pipeline
47+
invoke(context);
48+
}
49+
```
50+
In this snippet, you are specifying the credentials dynamically. However, keep in mind that securely storing passwords is essential in real applications.
51+
## Step 3: Using the Credential Handler
52+
Now that you have your `CredentialHandler`, it’s time to integrate it into your application.
53+
You can create an instance of `CredentialHandler` and use it when making network requests.
54+
```java
55+
public class HtmlDocumentLoader {
56+
public void loadDocument(String url) {
57+
CredentialHandler credentialHandler = new CredentialHandler();
58+
INetworkOperationContext operationContext = new NetworkOperationContext();
59+
60+
// Set the URL you wish to access.
61+
operationContext.getRequest().setUrl(url);
62+
63+
credentialHandler.invoke(operationContext);
64+
65+
// Continue with your operation...
2866
}
2967
}
30-
31-
// END_SNIPPET
32-
3368
```
69+
## Step 4: Testing Your Implementation
70+
After setting up your credential handler, it’s essential to test if it works correctly.
71+
Create a main method for testing purposes. Here’s an example:
72+
```java
73+
public class Main {
74+
public static void main(String[] args) {
75+
HtmlDocumentLoader loader = new HtmlDocumentLoader();
76+
loader.loadDocument("http://example.com");
77+
}
78+
}
79+
```
80+
Run your application and ensure that the handler processes authentication requests as expected. Watch for any errors or issues in the console output.
81+
## Conclusion
82+
Implementing a Credential Handler in Aspose.HTML for Java takes a bit of configuration, but it streamlines the way your applications handle user authentication. Leveraging the powerful features of Aspose, you can ensure that your applications remain secure while interacting with web resources.
83+
84+
## FAQ's
85+
### What is Aspose.HTML for Java?
86+
Aspose.HTML for Java is a library designed to manipulate HTML files and handle various web-related tasks in Java applications.
87+
### How do I obtain Aspose.HTML for Java?
88+
You can download it from the [site](https://releases.aspose.com/html/java/).
89+
### Can I get a temporary license for Aspose.HTML?
90+
Yes, you can apply for a temporary license [here](https://purchase.aspose.com/temporary-license/).
91+
### Is there a support forum for Aspose.HTML users?
92+
Absolutely! You can find support and engage with the community at [Aspose forums](https://forum.aspose.com/c/html/29).
93+
### What is the purpose of the `setPreAuthenticate(true)` method?
94+
This method ensures that the credentials are used automatically in the request header for authentication without prompting the user.

content/english/java/mutation-observers-handlers/mutation-observer/_index.md

+98-43
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,109 @@
22
title: Advanced Mutation Observer with Aspose.HTML for Java
33
linktitle: Advanced Mutation Observer with Aspose.HTML for Java
44
second_title: Java HTML Processing with Aspose.HTML
5-
description:
5+
description: Learn how to implement an advanced Mutation Observer with Aspose.HTML for Java, tracking DOM changes seamlessly. Dive into our step-by-step guide.
66
type: docs
77
weight: 10
88
url: /java/mutation-observers-handlers/mutation-observer/
99
---
10-
11-
## Complete Source Code
10+
## Introduction
11+
Are you looking to deepen your understanding of DOM manipulation and tracking changes in Java using Aspose.HTML? Well, you’re in the right place! In this tutorial, we will delve into how to leverage the powerful Mutation Observer API provided by Aspose.HTML for Java. This nifty feature allows us to listen for changes in the DOM, making it a great tool for dynamic web applications. So, let’s get started!
12+
## Prerequisites
13+
Before we dive into the nitty-gritty, let’s make sure you have everything you need to follow along smoothly:
14+
1. Java Installed: Ensure that you have Java Development Kit (JDK) installed on your machine.
15+
2. Aspose.HTML for Java: Download the Aspose.HTML library. You can get it from the [Aspose Release page](https://releases.aspose.com/html/java/).
16+
3. IDE: A preferred Integrated Development Environment (IDE), like IntelliJ IDEA or Eclipse, to write and run your code.
17+
4. Basic Java Knowledge: Familiarity with Java programming and concepts like classes, methods, and objects will be helpful.
18+
Once you have these prerequisites sorted, you’re set to embark on a journey through the world of HTML manipulation!
19+
## Import Packages
20+
To kick things off, we need to import the necessary packages from Aspose.HTML. This step is crucial as these packages contain the classes and methods we’ll be using in our code.
21+
Here’s how you can do that:
1222
```java
13-
package com.aspose.html.documentation.examples;
14-
15-
public class Advanced_MutationObserver {
16-
public static void main(String [] args) throws java.io.IOException {
17-
// START_SNIPPET Advanced_MutationObserver
18-
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument();
19-
20-
com.aspose.html.dom.mutations.MutationObserver observer = new com.aspose.html.dom.mutations.MutationObserver(new com.aspose.html.dom.mutations.MutationCallback() {
21-
@Override
22-
public void invoke(com.aspose.html.utils.collections.generic.IGenericList<com.aspose.html.dom.mutations.MutationRecord> mutations, com.aspose.html.dom.mutations.MutationObserver mutationObserver) {
23-
for (int i = 0 ; i < mutations.size(); i++) {
24-
com.aspose.html.dom.mutations.MutationRecord record = mutations.get_Item(i);
25-
for (com.aspose.html.dom.Node node : record.getAddedNodes().toArray()) {
26-
System.out.println("The '" + node + "' node was added to the document.");
27-
}
28-
}
23+
import com.aspose.html.HTMLDocument;
24+
import com.aspose.html.dom.mutations.MutationObserver;
25+
import com.aspose.html.dom.mutations.MutationCallback;
26+
import com.aspose.html.dom.mutations.MutationObserverInit;
27+
import com.aspose.html.dom.Node;
28+
import com.aspose.html.dom.Element;
29+
import com.aspose.html.dom.Text;
30+
import com.aspose.html.utils.collections.generic.IGenericList;
31+
import java.io.IOException;
32+
```
33+
Now that we have our packages ready, let’s walk through building our Mutation Observer step by step.
34+
## Step 1: Create an HTML Document
35+
In this first step, we'll create an instance of an HTML document. This document is the scaffolding upon which we will build and modify our DOM elements.
36+
```java
37+
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument();
38+
```
39+
This single line of code sets up a new HTML document using Aspose.HTML's `HTMLDocument` class, giving us a blank slate to work with.
40+
## Step 2: Configure the Mutation Observer
41+
Next, we’ll configure our Mutation Observer. This observer will watch for specific changes in the DOM.
42+
### Define the Callback Function
43+
We need to define what the observer should do when it detects changes. Here’s how to do that:
44+
```java
45+
com.aspose.html.dom.mutations.MutationObserver observer = new com.aspose.html.dom.mutations.MutationObserver(new com.aspose.html.dom.mutations.MutationCallback() {
46+
@Override
47+
public void invoke(IGenericList<MutationRecord> mutations, MutationObserver mutationObserver) {
48+
for (int i = 0; i < mutations.size(); i++) {
49+
MutationRecord record = mutations.get_Item(i);
50+
for (Node node : record.getAddedNodes().toArray()) {
51+
System.out.println("The '" + node + "' node was added to the document.");
2952
}
30-
});
31-
32-
// configuration of the observer
33-
com.aspose.html.dom.mutations.MutationObserverInit config = new com.aspose.html.dom.mutations.MutationObserverInit();
34-
config.setChildList(true);
35-
config.setSubtree(true);
36-
config.setCharacterData(true);
37-
38-
// pass in the target node to observe with the specified configuration
39-
observer.observe(document.getBody(), config);
40-
41-
// Now, we are going to modify DOM tree to check
42-
// Create an paragraph element and append it to the document body
43-
com.aspose.html.dom.Element p = document.createElement("p");
44-
document.getBody().appendChild(p);
45-
// Create a text and append it to the paragraph
46-
com.aspose.html.dom.Text text = document.createTextNode("Hello World");
47-
p.appendChild(text);
48-
49-
System.out.println("Waiting for mutation. Press any key to continue...");
50-
System.in.read();
51-
// END_SNIPPET
53+
}
5254
}
53-
}
54-
55+
});
5556
```
57+
In this code, we create a new `MutationObserver` instance and provide a callback. This callback will run whenever a mutation is detected. We loop through the mutations to check for any added nodes and print a message to the console.
58+
### Configure the Mutation Observer
59+
The next part is about configuring what changes we want the observer to track:
60+
```java
61+
com.aspose.html.dom.mutations.MutationObserverInit config = new com.aspose.html.dom.mutations.MutationObserverInit();
62+
config.setChildList(true);
63+
config.setSubtree(true);
64+
config.setCharacterData(true);
65+
```
66+
Here, we configure three options:
67+
- `setChildList(true)`: Observes changes to child nodes.
68+
- `setSubtree(true)`: Observes all descendants, making the observer watch the entire subtree.
69+
- `setCharacterData(true)`: Watches for changes to the text content within elements.
70+
## Step 3: Start Observing the Document
71+
Now that our observer is configured, we need to tell it which part of the document to observe:
72+
```java
73+
observer.observe(document.getBody(), config);
74+
```
75+
With this line, we attach our observer to the body of the document and pass our configuration. At this point, the observer is ready to catch any mutations happening in the body of our HTML document!
76+
## Step 4: Modify the DOM
77+
To test our observer, we’ll make some changes in the DOM. Let’s create a new paragraph and append it to the document’s body.
78+
## Add a Paragraph Element
79+
```java
80+
com.aspose.html.dom.Element p = document.createElement("p");
81+
document.getBody().appendChild(p);
82+
```
83+
Here, we are creating a new paragraph element (`<p>`) and appending it to the body of the document. This action will trigger our mutation observer!
84+
## Add Text to the Paragraph
85+
```java
86+
com.aspose.html.dom.Text text = document.createTextNode("Hello World");
87+
p.appendChild(text);
88+
```
89+
Next, we create a text node with the content “Hello World” and append it to our newly created paragraph. This addition will also be watched by the observer.
90+
## Step 5: Keeping the Program Running
91+
Finally, we want our program to keep running so that we can see the output of our mutations.
92+
```java
93+
System.out.println("Waiting for mutation. Press any key to continue...");
94+
System.in.read();
95+
```
96+
This line waits for user input before terminating the program, giving us time to see the printouts in the console regarding any nodes added.
97+
## Conclusion
98+
And there you have it! With just a few straightforward steps, we've implemented an advanced Mutation Observer using Aspose.HTML for Java. This powerful feature allows you to track changes in the DOM dynamically, which can be extremely useful for creating interactive web applications.
99+
100+
## FAQ's
101+
### What is a Mutation Observer?
102+
A Mutation Observer is an API that allows you to watch for changes to the DOM, such as additions or deletions of nodes.
103+
### Why use Aspose.HTML for Java?
104+
Aspose.HTML provides a robust library for manipulating HTML documents and offers features like Mutation Observers, making it ideal for Java developers.
105+
### Can I use Mutation Observers with any Java project?
106+
Yes, as long as you include the Aspose.HTML library in your project, you can use Mutation Observers.
107+
### Is there any performance impact when using Mutation Observers?
108+
Mutation Observers are designed to be efficient. However, excessive or unnecessary observations may still affect performance, so it's essential to configure them wisely.
109+
### Where can I find more resources on Aspose.HTML?
110+
You can check the [Aspose Documentation](https://reference.aspose.com/html/java/) for more information and tutorials.

0 commit comments

Comments
 (0)