Source code: com/obinary/cms/taglibs/util/FileSrc.java
1 /**
2 *
3 * Magnolia and its source-code is licensed under the LGPL.
4 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6 * you are required to provide proper attribution to obinary.
7 * If you reproduce or distribute the document without making any substantive modifications to its content,
8 * please use the following attribution line:
9 *
10 * Copyright 1993-2003 obinary Ltd. (http://www.obinary.com) All rights reserved.
11 *
12 * */
13
14
15
16
17
18 package com.obinary.cms.taglibs.util;
19
20
21
22 import com.obinary.cms.core.Container;
23 import com.obinary.cms.core.Atom;
24 import com.obinary.cms.core.Content;
25 import com.obinary.cms.util.Resource;
26 import com.obinary.cms.taglibs.ContainerLoop;
27 import com.obinary.cms.beans.ServerInfo;
28
29 import javax.servlet.jsp.tagext.TagSupport;
30 import javax.servlet.jsp.JspException;
31 import javax.servlet.jsp.JspWriter;
32 import javax.servlet.jsp.PageContext;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.jcr.RepositoryException;
35
36
37 /**
38 * Date: Apr 28, 2003
39 * Time: 11:20:59 AM
40 * @author Marcel Salathe
41 * @version 1.0
42 */
43
44
45 public class FileSrc extends TagSupport {
46
47 private Atom atom;
48 private String atomName = "";
49 private Container container;
50 private String containerName = "";
51
52 private Content actpage;
53 private HttpServletRequest request;
54
55 private String fileExtension;
56
57 public void setAtomName(String atomName) {
58 this.atomName = atomName;
59 }
60
61 public void setContainerName(String containerName) {
62 this.containerName = containerName;
63 }
64
65 public int doStartTag() {
66 this.request = (HttpServletRequest)pageContext.getRequest();
67 this.actpage = Resource.getCurrentActivePage(request);
68 if (!this.containerName.equals("")) {
69 try {
70 this.container = this.actpage.getContainer(this.containerName);
71 }
72 catch (RepositoryException re) {
73 writeSrc("");
74 re.printStackTrace();
75 }
76 if (this.container == null) {
77 writeSrc("");
78 return SKIP_BODY;
79 }
80 }
81 else {
82 this.container = Resource.getLocalContainer(request);
83 if (this.container == null) {
84 this.container = Resource.getGlobalContainer(request);
85 }
86 if (this.container != null) this.containerName = this.container.getName();
87 else {
88 writeSrc("");
89 return SKIP_BODY;
90 }
91 }
92
93 if (this.atomName.equals("")) {
94 writeSrc("");
95 return SKIP_BODY;
96 }
97 try {
98 this.atom = this.container.getAtom(this.containerName);
99 }
100 catch (Exception e) {
101 writeSrc("");
102 return SKIP_BODY;
103 }
104 if (this.atom == null) {
105 writeSrc("");
106 return SKIP_BODY;
107 }
108 setFileExtension();
109 String actHandle = "";
110 try {
111 actHandle = this.actpage.getHandle();
112 }
113 catch (Exception e) {};
114 String containerListName = (String) pageContext.getAttribute(ContainerLoop.CONTAINER_LIST_NAME,PageContext.REQUEST_SCOPE);
115 if (containerListName == null) {
116 // we are not in a loop
117 try {
118 writeSrc(this.container.getHandle()+"/"+this.atomName+"."+this.fileExtension);
119 }
120 catch (Exception e) {}
121 }
122 else {
123 try {
124 writeSrc(Resource.getLocalContainer(request).getHandle()+"/"+this.atomName+"."+this.fileExtension);
125 }
126 catch (Exception e) {}
127 }
128 return EVAL_PAGE;
129 }
130
131 private void writeSrc(String src) {
132 JspWriter out = pageContext.getOut();
133 try {
134 out.print(src);
135 }
136 catch (Exception e) {}
137 }
138
139 private void setFileExtension() {
140 this.fileExtension = ServerInfo.getDefaultExtension();
141 Container properties = null;
142 String containerListName = (String) pageContext.getAttribute(ContainerLoop.CONTAINER_LIST_NAME,PageContext.REQUEST_SCOPE);
143 if (containerListName == null) {
144 // we are not in a loop
145 try {
146 properties = Resource.getGlobalContainer(this.request).getContainer(this.atomName+"_properties");
147 }
148 catch (Exception e) {
149 e.printStackTrace();
150 }
151 }
152 else {
153 try {
154 properties = Resource.getLocalContainer(this.request).getContainer(this.atomName+"_properties");
155 }
156 catch (Exception e) {e.printStackTrace();}
157 }
158 if (properties != null) this.fileExtension = properties.getAtom("extension").getString();
159 }
160
161 }