Skip to content

Commit 042b0d4

Browse files
Implemented new feature insertBefore in AbstractHtml
It also contains bug fix for addInnerHtml method and other minor improvements
1 parent 25ba4dd commit 042b0d4

File tree

13 files changed

+592
-20
lines changed

13 files changed

+592
-20
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2014-2016 Web Firm Framework
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* @author WFF
16+
*/
17+
package com.webfirmframework.wffweb;
18+
19+
/**
20+
*
21+
* @author WFF
22+
* @since 2.1.1
23+
*/
24+
public class NoParentException extends WffRuntimeException {
25+
26+
/**
27+
*
28+
*/
29+
private static final long serialVersionUID = 1_0_0L;
30+
31+
public NoParentException() {
32+
super();
33+
}
34+
35+
public NoParentException(final String message, final Throwable cause,
36+
final boolean enableSuppression, final boolean writableStackTrace) {
37+
super(message, cause, enableSuppression, writableStackTrace);
38+
}
39+
40+
public NoParentException(final String message, final Throwable cause) {
41+
super(message, cause);
42+
}
43+
44+
public NoParentException(final String message) {
45+
super(message);
46+
}
47+
48+
public NoParentException(final Throwable cause) {
49+
super(cause);
50+
}
51+
52+
}

wffweb/src/main/java/com/webfirmframework/wffweb/server/page/BrowserPage.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,12 @@ private void addInnerHtmlAddListener(final AbstractHtml abstractHtml) {
578578
ACCESS_OBJECT);
579579
}
580580

581+
private void addInsertBeforeListener(final AbstractHtml abstractHtml) {
582+
abstractHtml.getSharedObject().setInsertBeforeListener(
583+
new InsertBeforeListenerImpl(this, ACCESS_OBJECT, tagByWffId),
584+
ACCESS_OBJECT);
585+
}
586+
581587
/**
582588
* @return {@code String} equalent to the html string of the tag including
583589
* the child tags.
@@ -651,6 +657,7 @@ private void initAbstractHtml() {
651657
addAttributeAddListener(abstractHtml);
652658
addAttributeRemoveListener(abstractHtml);
653659
addInnerHtmlAddListener(abstractHtml);
660+
addInsertBeforeListener(abstractHtml);
654661

655662
} else {
656663
wffBMBytesQueue.clear();

wffweb/src/main/java/com/webfirmframework/wffweb/server/page/ChildTagAppendListenerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public void childMoved(final ChildMovedEvent event) {
252252

253253
//@formatter:off
254254
// moved children tags from some parents to another task format (in this method moving only one child) :-
255-
// { "name": task_byte, "values" : [REMOVED_TAGS_byte_from_Task_enum]}, { "name": new_parent_data-wff-id, "values" : [ new_parent_tag_name, child_data-wff-id, child_tag_name ]}
255+
// { "name": task_byte, "values" : [MOVED_CHILDREN_TAGS_byte_from_Task_enum]}, { "name": new_parent_data-wff-id, "values" : [ new_parent_tag_name, child_data-wff-id, child_tag_name ]}
256256
// { "name": 2, "values" : [[3]]}, { "name":"C55", "values" : ["div", "S255", "span"]}
257257
//@formatter:on
258258

@@ -310,7 +310,7 @@ public void childrendAppendedOrMoved(final List<ChildMovedEvent> events) {
310310

311311
//@formatter:off
312312
// moved children tags from some parents to another task format (in this method moving only one child) :-
313-
// { "name": task_byte, "values" : [REMOVED_TAGS_byte_from_Task_enum]}, { "name": new_parent_data-wff-id, "values" : [ new_parent_tag_name, child_data-wff-id, child_tag_name ]}
313+
// { "name": task_byte, "values" : [MOVED_CHILDREN_TAGS_byte_from_Task_enum]}, { "name": new_parent_data-wff-id, "values" : [ new_parent_tag_name, child_data-wff-id, child_tag_name ]}
314314
// { "name": 2, "values" : [[3]]}, { "name":"C55", "values" : ["div", "S255", "span"]}
315315
//@formatter:on
316316

wffweb/src/main/java/com/webfirmframework/wffweb/server/page/InnerHtmlAddListenerImpl.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,17 @@ public void innerHtmlAdded(final Event event) {
9595

9696
final AbstractHtml parentTag = event.getParentTag();
9797
final AbstractHtml innerHtmlTag = event.getInnerHtmlTag();
98+
final AbstractHtml previousParentTag = event.getPreviousParentTag();
9899

99100
//@formatter:off
100101
// removed all children tags task format :-
101-
// { "name": task_byte, "values" : [ADDED_INNER_HTML_byte_from_Task_enum]}, { "name": data-wff-id, "values" : [ parent_tag_name, html_string ]}
102-
// { "name": 2, "values" : [[3]]}, { "name":"C55", "values" : ["div", "<span></span>]}
102+
// { "name": task_byte, "values" : [ADDED_INNER_HTML_byte_from_Task_enum]}, { "name": data-wff-id, "values" : [ parent_tag_name, html_string, 1_if_there_was_a_previous_parent ]}
103+
// { "name": 2, "values" : [[3]]}, { "name":"C55", "values" : ["div", "<span></span>", 1]}
103104
//@formatter:on
104105

105106
try {
106107
final NameValue task = Task.ADDED_INNER_HTML.getTaskNameValue();
107108

108-
final Deque<NameValue> nameValues = new ArrayDeque<NameValue>();
109-
nameValues.add(task);
110-
111109
final DataWffId dataWffId = parentTag.getDataWffId();
112110

113111
if (dataWffId != null) {
@@ -123,8 +121,14 @@ public void innerHtmlAdded(final Event event) {
123121

124122
final byte[] parentTagName = tagNameAndWffId[0];
125123

126-
nameValue.setValues(parentTagName,
127-
innerHtmlTag.toWffBMBytes("UTF-8"));
124+
if (previousParentTag != null) {
125+
nameValue.setValues(parentTagName,
126+
innerHtmlTag.toWffBMBytes("UTF-8"),
127+
new byte[] { 1 });
128+
} else {
129+
nameValue.setValues(parentTagName,
130+
innerHtmlTag.toWffBMBytes("UTF-8"));
131+
}
128132

129133
browserPage.push(task, nameValue);
130134

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Copyright 2014-2016 Web Firm Framework
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.webfirmframework.wffweb.server.page;
17+
18+
import java.io.UnsupportedEncodingException;
19+
import java.util.ArrayDeque;
20+
import java.util.Arrays;
21+
import java.util.Deque;
22+
import java.util.HashSet;
23+
import java.util.Map;
24+
import java.util.Set;
25+
import java.util.logging.Level;
26+
import java.util.logging.Logger;
27+
28+
import com.webfirmframework.wffweb.tag.html.AbstractHtml;
29+
import com.webfirmframework.wffweb.tag.html.html5.attribute.global.DataWffId;
30+
import com.webfirmframework.wffweb.tag.html.listener.InsertBeforeListener;
31+
import com.webfirmframework.wffweb.util.data.NameValue;
32+
33+
/**
34+
* @author WFF
35+
* @since 2.1.1
36+
*/
37+
class InsertBeforeListenerImpl implements InsertBeforeListener {
38+
39+
private static final long serialVersionUID = 1L;
40+
41+
public static final Logger LOGGER = Logger
42+
.getLogger(InsertBeforeListenerImpl.class.getName());
43+
44+
private BrowserPage browserPage;
45+
46+
private Object accessObject;
47+
48+
private Map<String, AbstractHtml> tagByWffId;
49+
50+
@SuppressWarnings("unused")
51+
private InsertBeforeListenerImpl() {
52+
throw new AssertionError();
53+
}
54+
55+
InsertBeforeListenerImpl(final BrowserPage browserPage,
56+
final Object accessObject,
57+
final Map<String, AbstractHtml> tagByWffId) {
58+
this.browserPage = browserPage;
59+
this.accessObject = accessObject;
60+
this.tagByWffId = tagByWffId;
61+
62+
}
63+
64+
/**
65+
* adds to wffid map
66+
*
67+
* @param tag
68+
* @since 2.0.0
69+
* @author WFF
70+
*/
71+
private void addInWffIdMap(final AbstractHtml tag) {
72+
73+
final Deque<Set<AbstractHtml>> childrenStack = new ArrayDeque<Set<AbstractHtml>>();
74+
childrenStack.push(new HashSet<AbstractHtml>(Arrays.asList(tag)));
75+
76+
while (childrenStack.size() > 0) {
77+
final Set<AbstractHtml> children = childrenStack.pop();
78+
for (final AbstractHtml child : children) {
79+
80+
final DataWffId wffIdAttr = child.getDataWffId();
81+
82+
if (wffIdAttr != null) {
83+
tagByWffId.put(wffIdAttr.getValue(), child);
84+
}
85+
86+
final Set<AbstractHtml> subChildren = child
87+
.getChildren(accessObject);
88+
if (subChildren != null && subChildren.size() > 0) {
89+
childrenStack.push(subChildren);
90+
}
91+
92+
}
93+
}
94+
95+
}
96+
97+
@Override
98+
public void insertedBefore(final Event... events) {
99+
100+
//@formatter:off
101+
// removed all children tags task format :-
102+
// { "name": task_byte, "values" : [INSERTED_BEFORE_TAG_byte_from_Task_enum]}, { "name": parent_data-wff-id, "values" : [ parent_tag_name, inserted_tag_html, before_tag_name, before_tag_data-wff-id, 1_if_there_was_a_previous_parent ]}
103+
// { "name": 2, "values" : [[3]]}, { "name":"C55", "values" : ["div", "<span></span>", 1]}
104+
//@formatter:on
105+
106+
try {
107+
final NameValue task = Task.INSERTED_BEFORE_TAG.getTaskNameValue();
108+
109+
final Deque<NameValue> nameValues = new ArrayDeque<NameValue>();
110+
nameValues.add(task);
111+
112+
for (final Event event : events) {
113+
114+
final AbstractHtml parentTag = event.getParentTag();
115+
116+
final AbstractHtml insertedTag = event.getInsertedTag();
117+
118+
final AbstractHtml beforeTag = event.getBeforeTag();
119+
120+
final AbstractHtml previousParentTag = event
121+
.getPreviousParentTag();
122+
123+
final DataWffId dataWffId = parentTag.getDataWffId();
124+
125+
if (dataWffId != null) {
126+
127+
final NameValue nameValue = new NameValue();
128+
129+
final byte[][] parentTagNameAndWffId = DataWffIdUtil
130+
.getTagNameAndWffId(parentTag);
131+
132+
final byte[] parentWffIdBytes = parentTagNameAndWffId[1];
133+
134+
nameValue.setName(parentWffIdBytes);
135+
136+
final byte[] parentTagName = parentTagNameAndWffId[0];
137+
138+
final byte[][] beforeTagNameAndWffId = DataWffIdUtil
139+
.getTagNameAndWffId(beforeTag);
140+
141+
if (previousParentTag != null) {
142+
nameValue.setValues(parentTagName,
143+
insertedTag.toWffBMBytes("UTF-8"),
144+
beforeTagNameAndWffId[0],
145+
beforeTagNameAndWffId[1], new byte[] { 1 });
146+
} else {
147+
nameValue.setValues(parentTagName,
148+
insertedTag.toWffBMBytes("UTF-8"),
149+
beforeTagNameAndWffId[0],
150+
beforeTagNameAndWffId[1]);
151+
}
152+
153+
addInWffIdMap(insertedTag);
154+
nameValues.add(nameValue);
155+
156+
} else {
157+
LOGGER.severe("Could not find data-wff-id from owner tag");
158+
}
159+
}
160+
161+
browserPage
162+
.push(nameValues.toArray(new NameValue[nameValues.size()]));
163+
164+
} catch (final UnsupportedEncodingException e) {
165+
if (LOGGER.isLoggable(Level.SEVERE)) {
166+
LOGGER.severe(e.toString());
167+
}
168+
}
169+
170+
}
171+
172+
}

wffweb/src/main/java/com/webfirmframework/wffweb/server/page/Task.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public enum Task {
3939

4040
MOVED_CHILDREN_TAGS,
4141

42+
INSERTED_BEFORE_TAG,
43+
4244
REMOVED_ATTRIBUTES,
4345

4446
ADDED_ATTRIBUTES,

0 commit comments

Comments
 (0)