001: /*
002: * Copyright 2001-2007 Hippo (www.hippo.nl)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package nl.hippo.cocoon.forms;
017:
018: import java.util.Arrays;
019: import org.w3c.dom.Document;
020: import org.w3c.dom.Element;
021: import javax.xml.parsers.ParserConfigurationException;
022: import javax.xml.parsers.DocumentBuilderFactory;
023:
024: public class XSDHelper {
025: public static final String SEPARATOR = "xxxxxxxxxxxxxx";
026: public static final String REVERSESEPARATOR = "zzzzzzzzzzzzzzz";
027: public static final String ATTRIBUTESEPARATOR = "yyyyyyyyyyyyyyyy";
028:
029: public static String getSeparator() {
030: return SEPARATOR;
031: }
032:
033: public static String getRevSeparator() {
034: return REVERSESEPARATOR;
035: }
036:
037: public static String getAttrSeparator() {
038: return ATTRIBUTESEPARATOR;
039: }
040:
041: public static String getParentId(String id) {
042: int index = id.lastIndexOf(SEPARATOR);
043: String newid;
044:
045: if (index > -1)
046: newid = id.substring(0, index);
047: else
048: newid = null;
049:
050: return newid;
051: }
052:
053: public static String isRecursive(String xpath, String node) {
054: String[] nodes = xpath.split("/");
055: return String.valueOf(Arrays.asList(nodes).contains(node));
056: }
057:
058: public static String getIdFromXPath(String xpath) {
059: //System.err.println("Gotten xpath:"+xpath);
060:
061: String id = xpath.replaceAll("/", SEPARATOR);
062: id = id.replaceAll("@", ATTRIBUTESEPARATOR);
063:
064: //System.err.println("ID:"+id);
065:
066: return id;
067: }
068:
069: public static String getNameFromXPath(String xpath) {
070: //System.err.println("Gotten xpath:"+xpath);
071:
072: int index = xpath.lastIndexOf("/");
073: String name;
074:
075: if (index > -1)
076: name = xpath.substring(index + 1);
077: else
078: name = xpath;
079:
080: //System.err.println("Name:"+name);
081:
082: return name;
083: }
084:
085: public static String getI18NNameFromXPath(String xpath) {
086: return xpath.replaceAll("/", ".");
087: }
088:
089: /**
090: * gets the widget type given a type from the schema
091: *
092: * may need to be configured later!
093: */
094: public static String getDefaultWidget(String xsdtypename) {
095: if ("xs:boolean".equals(xsdtypename)) {
096: return "booleanfield";
097: } else if ("xs:string".equals(xsdtypename)) {
098: return "field";
099: } else if ("xs:integer".equals(xsdtypename)) {
100: return "field";
101: } else if ("xs:date".equals(xsdtypename)) {
102: return "field";
103: }
104: return "";
105: }
106:
107: /**
108: * gets the cforms data type given a type from the schema
109: *
110: * may need to be configured later!
111: */
112: public static String getDefaultDataType(String xsdtypename) {
113: if ("xs:boolean".equals(xsdtypename)) {
114: return "boolean";
115: } else if ("xs:string".equals(xsdtypename)) {
116: return "string";
117: } else if ("xs:date".equals(xsdtypename)) {
118: return "date";
119: } else if ("xs:date".equals(xsdtypename)) {
120: return "string";
121: } else if ("xs:int".equals(xsdtypename)) {
122: return "long";
123: } else if ("xs:integer".equals(xsdtypename)) {
124: return "long";
125: }
126: return "";
127: }
128:
129: public static Document csv2nodes(String csv, String separator) {
130: Document result = null;
131: try {
132: String[] values = csv.trim().split(separator);
133: result = DocumentBuilderFactory.newInstance()
134: .newDocumentBuilder().newDocument();
135: Element root = result.createElement("values");
136: result.appendChild(root);
137: if (csv.trim().length() > 0)
138: for (int i = 0; i < values.length; i++) {
139: Element val = result.createElement("value");
140: val.appendChild(result.createTextNode(values[i]));
141: root.appendChild(val);
142: }
143: } catch (ParserConfigurationException e) {
144: // No action needed, use default result of 'null'
145: }
146: return result;
147: }
148:
149: public static String getParentFolder(String src) {
150: if (src == null || src == "")
151: return "";
152: return src.substring(0, src.lastIndexOf("/"));
153: }
154:
155: public static String getFilename(String src) {
156: if (src == null || src == "")
157: return "";
158: return src.substring(src.lastIndexOf("/") + 1);
159: }
160:
161: public static String normalizeId(String id) {
162: return id.replaceAll("[^A-Za-z0-9_]", "_");
163: }
164:
165: }
|