View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.cms.core;
17  
18  import java.io.UnsupportedEncodingException;
19  import java.util.regex.Pattern;
20  
21  public class PageImpl implements Page {
22  
23  	private Resource resource;
24  
25  	private Object content;
26  
27  	private volatile int hashcode = 0;
28  	
29  	private static final String CONTENT_ENCODING = "Windows-31J";
30  
31  	public PageImpl(Resource resource, Object content) {
32  		this.resource = resource;
33  		setContent(content);
34  	}
35  
36  	public Object getContent() {
37  		return content;
38  	}
39  
40  	public void setContent(Object content) {
41  		if (content instanceof String) {
42  			try{
43  				//FIXME: need to be fixed for internationalization
44  				resource.setContentLength(((String) content).getBytes(CONTENT_ENCODING).length);
45  			}catch(UnsupportedEncodingException e){
46  				// this should not be happened
47  			}
48  		}
49  		this.content = content;
50  	}
51  
52  	public Resource getResource() {
53  		return resource;
54  	}
55  
56  	public void setResource(Resource resource) {
57  		this.resource = resource;
58  	}
59  
60  	public boolean equals(Object obj) {
61  		if (obj == this)
62  			return true;
63  		if (!(obj instanceof PageImpl))
64  			return false;
65  
66  		PageImpl o = (PageImpl) obj;
67  		return o.resource.equals(this.resource);
68  	}
69  
70  	public int hashCode() {
71  
72  		if (this.hashcode == 0) {
73  			this.hashcode = this.resource.hashCode();
74  		}
75  		return this.hashcode;
76  	}
77  
78  	public void merge(String sectionId, String insertContent) {
79  		if (CmsConstants.CONTENTTYPE_XWIKI.equals(getResource()
80  				.getContentType())) {
81  			if (content == null || !(content instanceof String)) {
82  				return;
83  			}
84  			Pattern pattern = Pattern.compile("^//*.*//[#" + sectionId + "//]$");
85  			String[] lines = ((String) content)
86  					.split(CmsConstants.LINEBREAK_CODE);
87  
88  			StringBuffer buf = new StringBuffer();
89  
90  			int headlingLevel = 0;
91  
92  			for (int i = 0; i < lines.length; i++) {
93  				String line = lines[i];
94  
95  				if (pattern.matcher(line).find()) {
96  					headlingLevel = getHeadingLevel(line);
97  					buf.append(insertContent);
98  				} else if (headlingLevel > 0) {
99  					if (line.indexOf("*") == 0
100 							&& getHeadingLevel(line) <= headlingLevel) {
101 						headlingLevel = 0;
102 					}
103 				}
104 				if (headlingLevel  == 0) {
105 					buf.append(line);
106 					buf.append(CmsConstants.LINEBREAK_CODE);
107 				}
108 			}			
109 			setContent(buf.toString());
110 		}
111 	}	
112 		
113 	public String retrieve(String sectionId) {
114 		if (CmsConstants.CONTENTTYPE_XWIKI.equals(getResource()
115 				.getContentType())) {
116 			if (content == null || !(content instanceof String)) {
117 				return "";
118 			}
119 
120 			Pattern pattern = Pattern.compile("^//*.*//[#" + sectionId + "//]$");
121 			String[] lines = ((String) content)
122 					.split(CmsConstants.LINEBREAK_CODE);
123 
124 			StringBuffer buf = new StringBuffer();
125 
126 			int headlingLevel = 0;
127 
128 			for (int i = 0; i < lines.length; i++) {
129 				String line = lines[i];
130 
131 				if (pattern.matcher(line).find()) {
132 					headlingLevel = getHeadingLevel(line);
133 				} else if (headlingLevel > 0) {
134 					if (line.indexOf("*") == 0
135 							&& getHeadingLevel(line) <= headlingLevel) {
136 						break;
137 					}
138 				}
139 				if (headlingLevel > 0) {
140 					buf.append(line);
141 					buf.append(CmsConstants.LINEBREAK_CODE);
142 				}
143 			}
144 			return buf.toString();
145 		} else {
146 			return "";
147 		}
148 	}
149 
150 	private int getHeadingLevel(String line) {
151 		if (line == null) {
152 			return -1;
153 		}
154 		if (line.indexOf("***") == 0) {
155 			return 3;
156 		} else if (line.indexOf("**") == 0) {
157 			return 2;
158 		} else if (line.indexOf("*") == 0) {
159 			return 1;
160 		}
161 		return -1;
162 	}
163 	
164 }