Java processes (p) tags in html code to ensure closure

Source: Internet
Author: User

Java processes (p) tags in html code to ensure closure

Requirements:

Processing a piece of html code

Tag, so that it is not nested, closed, and no content is exposed outside the tag

For example:

11111

22222

33333

44444

555555

66666


Mind Map:

Code:

Public static String checkpp (String content) {String Str; int time = 0; int pos = 0; int path = 0; boolean flag = true; int index1 = 0; int index2 = 0; // ensure that the start and end positions are respectively

And

Int firstindex = content. indexOf ("

"); If (firstindex! = 0) {content ="

"+ Content;} int lastindex = content. lastIndexOf ("

"); If (lastindex! = Content. length ()-4 | lastindex =-1) {content = content +"

";}Stringbuffer sb = new StringBuffer (content); do {time ++; System. out. println ("-------" + time + "----- path:" + path); switch (path) {case 0: index1 = sb. indexOf ("

", Pos + 1); index2 = sb. indexOf ("

", Pos + 1); System. out. println (sb. toString (); System. out. println ("pos:" + pos); System. out. println ("index1:" + index1); System. out. println ("index2:" + index2); if (index1! =-1) {if (index2

Path = 1; pos = index2;} else {//

(...)*

(Xxx )*

Sb = sb. insert (index1 ,"

"); Path = 0; pos = index1 + 4;} else {// no more

But it's not end yet:

(...

)*

Path = 1; pos = index2;} break; case 1: index1 = sb. indexOf ("

", Pos + 1); index2 = sb. indexOf ("

", Pos + 1); System. out. println (sb. toString (); System. out. println ("pos:" + pos); System. out. println ("index1:" + index1); System. out. println ("index2:" + index2); // no more

Exitif (index2 =-1) {flag = false; break;} // no more

But not endif (index1 =-1) {sb = sb. insert (pos + 4 ,"

"); Path = 0; pos = pos + 7; break;} else {/*

(....)*

* Optional * pos */if (index1 4 ){//

(Xxx)

(Xxx)

Sb = sb. insert (pos + 4 ,"

"); Sb = sb. insert (index1 + 3 ,"

"); Path = 0; pos = index1 + 7;} else if (index1-pos = 4 ){//

Path = 0; pos = index1;} else {//

(....)*

(...)*

Sb = sb. insert (pos + 4 ,"

"); Path = 1; pos = index2 + 3;} break;} default: break;} System. out. println ("pos:" + pos); System. out. println (sb. toString (); // exitif (pos = sb. length ()-4) break;} while (flag); Str = sb. toString (); Str = Str. replaceAll ("

(\ S )*()*()*

"," "); Return Str ;}

Log of the execution example:
11111

22222

33333

44444

555555

66666-------1----- path:0

11111

22222

33333

44444

555555

66666

pos:0index1:8index2:11pos:12

11111

22222

33333

44444

555555

66666

-------2----- path:0

11111

22222

33333

44444

555555

66666

pos:12index1:24index2:15pos:15

11111

22222

33333

44444

555555

66666

-------3----- path:1

11111

22222

33333

44444

555555

66666

pos:15index1:24index2:32pos:31

11111

22222

33333

44444

555555

66666

-------4----- path:0

11111

22222

33333

44444

555555

66666

pos:31index1:43index2:39pos:39

11111

22222

33333

44444

555555

66666

-------5----- path:1

11111

22222

33333

44444

555555

66666

pos:39index1:43index2:51pos:43

11111

22222

33333

44444

555555

66666

-------6----- path:0

11111

22222

33333

44444

555555

66666

pos:43index1:61index2:51pos:51

11111

22222

33333

44444

555555

66666

-------7----- path:1

11111

22222

33333

44444

555555

66666

pos:51index1:61index2:64pos:68

11111

22222

33333

44444

555555

66666

-------8----- path:0

11111

22222

33333

44444

555555

66666

pos:68index1:-1index2:71pos:71

11111

22222

33333

44444

555555

66666

-------9----- path:1

11111

22222

33333

44444

555555

66666

pos:71index1:-1index2:80pos:78

11111

22222

33333

44444

555555

66666

-------10----- path:0

11111

22222

33333

44444

555555

66666

pos:78index1:-1index2:83pos:83

11111

22222

33333

44444

555555

66666

11111

22222

33333

44444

555555

66666


Analysis:

1. Because it only involves

, You can

And

It is processed as two States. As shown in the Mind Map, the string is processed from left to right.

2. Convert String to StringBuffer to facilitate insertion.

3. Use the outer do while switch layer to traverse.

4. Use pos to record the current target location.

5. If flag is used, set it to false when the termination condition is triggered in swtich, and then break to terminate.

6. trigger termination signal: a. The current pos is located in the last one

This condition applies to both States. B. The current pos node does not traverse backward

Appears to prove that this is the last

, That is, the end.

Note: Because

The status is not the end state, so you cannot exit directly from case 0 of the switch. You can only jump to case 1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.