Skip to content

Thread Safety Problem with Preprocessor #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
subbudvk opened this issue Jan 15, 2025 · 1 comment
Open

Thread Safety Problem with Preprocessor #353

subbudvk opened this issue Jan 15, 2025 · 1 comment

Comments

@subbudvk
Copy link
Contributor

subbudvk commented Jan 15, 2025

I am using Preprocessor to change content of a specific tag. The implementation looks like follows,

I will listen for openTag event and when current tag is style, I handle it in a boolean and in the text() callback, I am checking whether current tag is style.

The problem, is in a multi-threaded environment, when openTag set current tag as style and before text() resets the boolean anything called text() is considered as style tag.

I moved the boolean below asnew HtmlStreamEventReceiverWrapper(eventReceiver) { boolean isCurrentTagStyle = false; }and it seems to be working fine. Any better suggestions to handle this without a preprocessor or is there a threadsafe event processor i can use?

(new HtmlStreamEventProcessor() {
			boolean isCurrentTagStyle = false;
                        @Override
			public HtmlStreamEventReceiver wrap(HtmlStreamEventReceiver eventReceiver) {
				return new HtmlStreamEventReceiverWrapper(eventReceiver) {
					@Override
					public void openTag(String elementName, List<String> attrs) {
						if (STYLE.equals(elementName))
							isCurrentTagStyle = true;
						super.openTag(elementName, attrs);
					}

					@Override
					public void text(String text) {
						String textContent = new String(text);
						if (isCurrentTagStyle) {	
							try { 
								//Change style content here	
							} catch (Exception e) {
								textContent = "";
							}
	                                           isCurrentTagStyle = false;
						}
						super.text(textContent);
					}
				};

			}
		});
@subbudvk
Copy link
Contributor Author

@mikesamuel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant