Skip to content
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

CDATAプロセッサの入れ子で意図せず]]>が出力されないように出力制御 #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ protected void optimizeProcessors(SequenceIDGenerator idGenerator,
}
}

// expandsとして展開された同階層のLiteralCharactersProcessorが連続している場合は結合する
List<ProcessorTreeWalker> packs = new ArrayList<>();
for (int i = 0; i < expands.size(); i++) {
ProcessorTreeWalker node = (ProcessorTreeWalker) expands.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.seasar.mayaa.impl.CONST_IMPL;
import org.seasar.mayaa.impl.builder.BuilderUtil;
import org.seasar.mayaa.impl.cycle.CycleUtil;
import org.seasar.mayaa.impl.cycle.DefaultCycleLocalInstantiator;

/**
* @author Masataka Kurihara (Gluegent, Inc.)
Expand All @@ -34,15 +35,44 @@ public class CDATAProcessor extends TemplateProcessorSupport
private static final String CDATAIN = "<![CDATA[";
private static final String CDATAOUT = "]]>";

/**
* CDATAは入れ子で出力できないため、CDATAProcessorの呼び出しネストレベルをカウントするGlobalVariableを使用する。
* 閉じタグのバランスが取れていないとCDATAが閉じないため注意する。
* テンプレートに元々記載されている<![CDATA[ ]]> は検知できない(そもそもXMLパーサにより評価されないため期待通り)。
*/
private static final String CDATA_IN_PROCESS_KEY = CDATAProcessor.class.getName() + "#nestLevel";
static {
CycleUtil.registVariableFactory(CDATA_IN_PROCESS_KEY,
new DefaultCycleLocalInstantiator() {
@Override
public Object create(Object[] params) {
return Integer.valueOf(0);
}
}
);
}

public ProcessStatus doStartProcess(Page topLevelPage) {
ServiceCycle cycle = CycleUtil.getServiceCycle();
cycle.getResponse().write(CDATAIN);
// 入れ子のレベルを確認する。
Integer nestLevel = (Integer) CycleUtil.getGlobalVariable(CDATA_IN_PROCESS_KEY, null);
if (nestLevel.intValue() == 0) {
ServiceCycle cycle = CycleUtil.getServiceCycle();
cycle.getResponse().write(CDATAIN);
}
CycleUtil.setGlobalVariable(CDATA_IN_PROCESS_KEY, Integer.valueOf(nestLevel.intValue() + 1));
return ProcessStatus.EVAL_BODY_INCLUDE;
}

public ProcessStatus doEndProcess() {
ServiceCycle cycle = CycleUtil.getServiceCycle();
cycle.getResponse().write(CDATAOUT);
// 入れ子のレベルを確認する。
Integer nestLevel = (Integer) CycleUtil.getGlobalVariable(CDATA_IN_PROCESS_KEY, null);
CycleUtil.setGlobalVariable(CDATA_IN_PROCESS_KEY, Integer.valueOf(nestLevel.intValue() - 1));

if (nestLevel.intValue() <= 1 /* 1:最後のネストレベル */) {
ServiceCycle cycle = CycleUtil.getServiceCycle();
cycle.getResponse().write(CDATAOUT);
CycleUtil.clearGlobalVariable(CDATA_IN_PROCESS_KEY);
}
return ProcessStatus.EVAL_PAGE;
}

Expand Down
10 changes: 9 additions & 1 deletion src/test/java/org/seasar/mayaa/functional/EngineTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
Expand Down Expand Up @@ -159,7 +160,14 @@ protected MockHttpServletResponse exec(final MockHttpServletRequest request, fin
engine.doService(pageScopeAttribute, true);

// verify(servletContext).getRealPath(path);

if ("true".equals(engine.getParameter(EngineImpl.DUMP_ENABLED))) {
try {
final String content = response.getContentAsString();
System.out.println(content);
}
catch (UnsupportedEncodingException e) {
}
}
return response;
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/seasar/mayaa/functional/ProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,11 @@ public class ProcessorTest extends EngineTestBase {
}

}

@Test
public void プロセッサcdataで子要素をCDATAで囲む() throws IOException {
enableDump();
execAndVerify(BASE_PATH + "cdata/target.html", BASE_PATH + "cdata/expected.html", null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<body>
<section id="1">
<![CDATA[
var val_int = 0;
]]>
</section>
<section id="2">
<![CDATA[
<p>子要素のテキスト1</p>
TEXT
]]>
</section>
<section id="3">
<![CDATA[
<p>子要素のテキスト1</p>

<p>子要素のテキスト2</p>
TEXT

]]>
</section>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html xmlns:m="http://mayaa.seasar.org">
<body>
<section id="1" m:comment="子要素は単純テキスト">
<div m:id="CDATA:">
var val_int = 0;
</div>
</section>
<section id="2" m:comment="子要素にプロセッサを含む">
<div m:id="CDATA:">
<p>子要素のテキスト1</p>
<p m:id="WRITE:TEXT">子要素のテキスト2</p>
</div>
</section>
<section id="3" m:comment="子要素にCDATAプロセッサを入れ子で含む">
<div m:id="CDATA:">
<p>子要素のテキスト1</p>
<div m:id="CDATA:">
<p>子要素のテキスト2</p>
<p m:id="WRITE:TEXT">子要素のテキスト2</p>
</div>
</div>
</section>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<m:mayaa xmlns:m="http://mayaa.seasar.org">

<m:cdata m:id="CDATA:" />

<m:write m:id="WRITE:TEXT" value="TEXT" />

</m:mayaa>