From 049c0447441569174a98127cdb46cd43c44ed17b Mon Sep 17 00:00:00 2001 From: Scott Murphy Date: Sat, 11 Nov 2023 19:20:55 -0500 Subject: [PATCH] Cut and Copy tags. Fixes #18 --- .../resources/static/decorators/default.html | 3 +- .../src/main/webapp/WEB-INF/jsp/greeting.jsp | 15 +++++- .../tagrules/html/CoreHtmlTagRuleBundle.java | 2 + .../tagrules/html/SiteMeshCutCopyRule.java | 51 +++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 sitemesh/src/main/java/org/sitemesh/content/tagrules/html/SiteMeshCutCopyRule.java diff --git a/examples/springboot/src/main/resources/static/decorators/default.html b/examples/springboot/src/main/resources/static/decorators/default.html index 75a01c3a..2cd9b381 100644 --- a/examples/springboot/src/main/resources/static/decorators/default.html +++ b/examples/springboot/src/main/resources/static/decorators/default.html @@ -14,7 +14,7 @@

Body goes here. Blah blah blah. -
Site disclaimer. This is an example.
+
Site disclaimer. Hey you, this is just an example.
+ \ No newline at end of file diff --git a/examples/springboot/src/main/webapp/WEB-INF/jsp/greeting.jsp b/examples/springboot/src/main/webapp/WEB-INF/jsp/greeting.jsp index 753ff52a..5c70431a 100644 --- a/examples/springboot/src/main/webapp/WEB-INF/jsp/greeting.jsp +++ b/examples/springboot/src/main/webapp/WEB-INF/jsp/greeting.jsp @@ -8,7 +8,7 @@ -

Hello, ${name}!

+

Hello, ${name}!

This page demonstrates that dynamic content can be decorated in the same way as static content.

This is a simple JSP that shows the date and time on the server is now:

@@ -17,5 +17,18 @@

Of course, with SiteMesh you are not limited to JSP. Because it's a Servlet Filter, both content and decorators can be generated by any technology in your ServletEngine, including: static files, JSP, Velocity, FreeMarker, Thymeleaf, JSF, MVC frameworks, JRuby.... you get the point.

+ +
+ What is your name? + +
+ + + + \ No newline at end of file diff --git a/sitemesh/src/main/java/org/sitemesh/content/tagrules/html/CoreHtmlTagRuleBundle.java b/sitemesh/src/main/java/org/sitemesh/content/tagrules/html/CoreHtmlTagRuleBundle.java index 24f868a4..6247bd29 100644 --- a/sitemesh/src/main/java/org/sitemesh/content/tagrules/html/CoreHtmlTagRuleBundle.java +++ b/sitemesh/src/main/java/org/sitemesh/content/tagrules/html/CoreHtmlTagRuleBundle.java @@ -53,6 +53,8 @@ public void install(State defaultState, ContentProperty contentProperty, SiteMes defaultState.addRule("title", new ExportTagToContentRule(siteMeshContext, contentProperty.getChild("title"), false)); defaultState.addRule("body", new ExportTagToContentAndMergeBodyAttributesRule(siteMeshContext, contentProperty.getChild("body"), false)); defaultState.addRule("meta", new MetaTagRule(contentProperty.getChild("meta"))); + defaultState.addRule("sitemesh:copy", new SiteMeshCutCopyRule(true, contentProperty)); + defaultState.addRule("sitemesh:cut", new SiteMeshCutCopyRule(false, contentProperty)); // Ensure that while in tag, none of the other rules kick in. // For example hello should not affect the title of the page. diff --git a/sitemesh/src/main/java/org/sitemesh/content/tagrules/html/SiteMeshCutCopyRule.java b/sitemesh/src/main/java/org/sitemesh/content/tagrules/html/SiteMeshCutCopyRule.java new file mode 100644 index 00000000..a852892e --- /dev/null +++ b/sitemesh/src/main/java/org/sitemesh/content/tagrules/html/SiteMeshCutCopyRule.java @@ -0,0 +1,51 @@ +/* + * Copyright 2009-2023 SiteMesh authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sitemesh.content.tagrules.html; + +import org.sitemesh.content.ContentProperty; +import org.sitemesh.tagprocessor.BasicBlockRule; +import org.sitemesh.tagprocessor.Tag; + +import java.io.IOException; + +public class SiteMeshCutCopyRule extends BasicBlockRule { + + private final ContentProperty contentProperty; + private final boolean copy; + + public SiteMeshCutCopyRule(boolean copy, ContentProperty contentProperty) { + this.copy = copy; + this.contentProperty = contentProperty.getChild("sitemesh"); + } + + @Override + protected String processStart(Tag tag) throws IOException { + tagProcessorContext.pushBuffer(); + return tag.getAttributeValue("id", false); + } + + @Override + protected void processEnd(Tag tag, String tagId) throws IOException { + CharSequence contents = tagProcessorContext.currentBufferContents(); + contentProperty.getChild(tagId).setValue(contents); + tagProcessorContext.popBuffer(); + if (copy) { + tagProcessorContext.currentBuffer().append(contents); + } + } + +}