diff --git a/ccm-core/bin/ccm-hostinit b/ccm-core/bin/ccm-hostinit index 437043729..4f2768695 100644 --- a/ccm-core/bin/ccm-hostinit +++ b/ccm-core/bin/ccm-hostinit @@ -14,36 +14,6 @@ # PATH, you have to adjust the paths. CCM_LIB_DIR="../lib" CCM_HOME_DIR="../.." -# echo "CCM_LIB_DIR is " ${CCM_LIB_DIR} -# echo "CCM_HOME_DIR is " ${CCM_HOME_DIR} - -# We need CATALINA_HOME environment variable to access Tomcat's lib dir. -#if [[ -z "$CATALINA_HOME" ]] ; then -# echo CATALINE_HOME not set. Guessing ... - -# if [ -f /etc/tomcat6/tomcat6.conf ] -# then -# . /etc/tomcat6/tomcat6.conf -# echo -# echo Using CATALINA_HOME = $CATALINA_HOME -# echo If this doesn\'t work use -# echo export CATALINA_HOME=/path/to/tomcat/installation -# echo to point us to the correct location. -# else -# echo -# echo =================================================== -# echo Environment variable CATALINA_HOME not set. We need -# echo to know the location of Tomcat\'s lib directory. -# echo So use -# echo export CATALINA_HOME=/path/to/tomcat/installation -# echo to point us to the correct location and run ccm -# echo again. -# echo =================================================== -# echo -# exit 1 -# fi -#fi -#CATALINA_LIB_DIR="${CATALINA_HOME}/lib" # We need CCM_REPO environment variable to access the source dir. if [[ -z "$CCM_REPO" ]] ; then @@ -73,7 +43,7 @@ if [[ -z "$CCM_REPO" ]] ; then echo " export CCM_REPO=/path/to/repo " echo to point us to the correct location and run echo ccm-hostinit again or execute - echo " CCM_REPO=/path/to/repo ccm-hostinit [task] " + echo " CCM_REPO=/path/to/repo sh ccm-hostinit [task] " echo again echo =================================================== echo @@ -97,26 +67,24 @@ then exit 1 fi -#if [ ! -f ${CATALINA_LIB_DIR}/catalina.jar ] -#then -# echo "Error: CATALINA_LIB_DIR is invalid \(no catalina.jar in CATALINA_LIB_DIR\)." -# exit 1 -#fi - #Convert to absolute path: CCM_HOME=`cd ${CCM_HOME_DIR}; pwd` -# echo "CCM_HOME_DIR: " ${CCM_HOME_DIR} -# echo "PARAMETER before shift: " "$@" ANT_HOME_DIR="./libexec/ant" ANT_HOME=`cd ${ANT_HOME_DIR}; pwd` export ANT_HOME -# echo "ANT_HOME: " ${ANT_HOME} -# echo "Starting CCM-Tool..." -# java -cp ${CCM_LIB_DIR}/"*":${CATALINA_LIB_DIR}/"*" -Dccm.home=${CCM_HOME_DIR} "com.arsdigita.packaging.MasterTool" "$@" + TASK="$1" -# shift shift -# echo "PARAMETER after shift: " "$@" -PACKAGES="$@" ${PACKAGES} -# echo "Packages: " ${PACKAGES} -exec libexec/ant/bin/ant ${TASK} -quiet -f libexec/build.xml -Dccm.home.dir=${CCM_HOME} -Dccm.repo.dir=${CCM_REPO} -Dccm.packages="${PACKAGES}" + +if [ "$1" = "--packagefile" ] + then + # Absolute path of current dir + MY_DIR=`pwd` + # Construct absolute (canonical) filename + FILENAME=${MY_DIR}/$2 + exec libexec/ant/bin/ant ${TASK} -quiet -f libexec/build.xml -Dccm.home.dir=${CCM_HOME} -Dccm.repo.dir=${CCM_REPO} -Dccm.packages.filename="${FILENAME}" + else + PACKAGES="$@" + exec libexec/ant/bin/ant ${TASK} -quiet -f libexec/build.xml -Dccm.home.dir=${CCM_HOME} -Dccm.repo.dir=${CCM_REPO} -Dccm.packages="${PACKAGES}" +fi + diff --git a/ccm-core/bin/libexec/ant/lib/dom4j.jar b/ccm-core/bin/libexec/ant/lib/dom4j.jar new file mode 100644 index 000000000..c8c4dbb92 Binary files /dev/null and b/ccm-core/bin/libexec/ant/lib/dom4j.jar differ diff --git a/ccm-core/bin/libexec/ant/lib/webxml-mergetool.jar b/ccm-core/bin/libexec/ant/lib/webxml-mergetool.jar new file mode 100644 index 000000000..50b8c5e14 Binary files /dev/null and b/ccm-core/bin/libexec/ant/lib/webxml-mergetool.jar differ diff --git a/ccm-core/bin/libexec/ant/lib/xerces.jar b/ccm-core/bin/libexec/ant/lib/xerces.jar new file mode 100644 index 000000000..99ee39d5b Binary files /dev/null and b/ccm-core/bin/libexec/ant/lib/xerces.jar differ diff --git a/ccm-core/bin/libexec/build.xml b/ccm-core/bin/libexec/build.xml index 7c1818250..82cfb04a5 100644 --- a/ccm-core/bin/libexec/build.xml +++ b/ccm-core/bin/libexec/build.xml @@ -22,7 +22,10 @@ HostInit deploys CCM into a servlet container. Specifically it deploys ADDON packages. - + + @@ -44,59 +47,132 @@ - + - Packages requested: ${ccm.packages} + + - - + - Package working on: ${dirname} + Package working on: ${packagename} - + - + - + + + + + + + + + + + + + + + + + + + + + + Installation of package ${packagename} completed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Info requested for Package(s): - ${ccm.packages} + ${ccm.requested.packages} @@ -105,9 +181,10 @@ - + @@ -120,9 +197,13 @@ + + + - + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - Base directory is ${basedir} diff --git a/ccm-core/bin/libexec/build.xml.bak b/ccm-core/bin/libexec/build.xml.bak new file mode 100644 index 000000000..7c1818250 --- /dev/null +++ b/ccm-core/bin/libexec/build.xml.bak @@ -0,0 +1,157 @@ + + + + + + HostInit deploys CCM into a servlet container. Specifically it deploys + ADDON packages. + + + + + + + + + + + + + + + + + + + + + + + + Packages requested: ${ccm.packages} + + + + + + + + Package working on: ${dirname} + + + + + + + + + + + + + + + + + Info requested for Package(s): + ${ccm.packages} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Base directory is ${basedir} + USAGE: + ccm-host list lists avaiblable addon packages + ccm-host add {packagelist} adds one or more packages to the aplaws application directory. + ccm-host info {package} displays info about a package. + + + + diff --git a/tools-ng/common/webxml-mergetool/dist/README.TXT b/tools-ng/common/webxml-mergetool/dist/README.TXT new file mode 100644 index 000000000..c75d5353e --- /dev/null +++ b/tools-ng/common/webxml-mergetool/dist/README.TXT @@ -0,0 +1,32 @@ +======================== +BUILD OUTPUT DESCRIPTION +======================== + +When you build an Java application project that has a main class, the IDE +automatically copies all of the JAR +files on the projects classpath to your projects dist/lib folder. The IDE +also adds each of the JAR files to the Class-Path element in the application +JAR files manifest file (MANIFEST.MF). + +To run the project from the command line, go to the dist folder and +type the following: + +java -jar "webxml-mergetool.jar" + +To distribute this project, zip up the dist folder (including the lib folder) +and distribute the ZIP file. + +Notes: + +* If two JAR files on the project classpath have the same name, only the first +JAR file is copied to the lib folder. +* Only JAR files are copied to the lib folder. +If the classpath contains other types of files or folders, these files (folders) +are not copied. +* If a library on the projects classpath also has a Class-Path element +specified in the manifest,the content of the Class-Path element has to be on +the projects runtime path. +* To set a main class in a standard Java project, right-click the project node +in the Projects window and choose Properties. Then click Run and enter the +class name in the Main Class field. Alternatively, you can manually type the +class name in the manifest Main-Class element. diff --git a/tools-ng/common/webxml-mergetool/dist/lib/ant.jar b/tools-ng/common/webxml-mergetool/dist/lib/ant.jar new file mode 100644 index 000000000..7f5be4a4e Binary files /dev/null and b/tools-ng/common/webxml-mergetool/dist/lib/ant.jar differ diff --git a/tools-ng/common/webxml-mergetool/dist/lib/dom4j.jar b/tools-ng/common/webxml-mergetool/dist/lib/dom4j.jar new file mode 100644 index 000000000..c8c4dbb92 Binary files /dev/null and b/tools-ng/common/webxml-mergetool/dist/lib/dom4j.jar differ diff --git a/tools-ng/common/webxml-mergetool/dist/lib/xerces.jar b/tools-ng/common/webxml-mergetool/dist/lib/xerces.jar new file mode 100644 index 000000000..99ee39d5b Binary files /dev/null and b/tools-ng/common/webxml-mergetool/dist/lib/xerces.jar differ diff --git a/tools-ng/common/webxml-mergetool/dist/webxml-mergetool.jar b/tools-ng/common/webxml-mergetool/dist/webxml-mergetool.jar new file mode 100644 index 000000000..51ea9c2b5 Binary files /dev/null and b/tools-ng/common/webxml-mergetool/dist/webxml-mergetool.jar differ diff --git a/tools-ng/common/webxml-mergetool/howto-use.txt b/tools-ng/common/webxml-mergetool/howto-use.txt new file mode 100644 index 000000000..4db0cff70 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/howto-use.txt @@ -0,0 +1,34 @@ +webxml-mergetool + +Purpose: + +Takes an existing web.xml deployment descriptor (original file) and +a web.xml fragment (merge file) and inserts those elements of merge file +which are not already in original file into original file and save the +combined content in destination file. + +Usage: + +The tool acts as an ANT task. + +At beginning of build.xml: + + + +To use add at appropriate location: + + + + + +Eventually followed by + + + + diff --git a/tools-ng/common/webxml-mergetool/lib/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar b/tools-ng/common/webxml-mergetool/lib/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar new file mode 100644 index 000000000..1c0f59737 Binary files /dev/null and b/tools-ng/common/webxml-mergetool/lib/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar differ diff --git a/tools-ng/common/webxml-mergetool/lib/ant.jar b/tools-ng/common/webxml-mergetool/lib/ant.jar new file mode 100644 index 000000000..7f5be4a4e Binary files /dev/null and b/tools-ng/common/webxml-mergetool/lib/ant.jar differ diff --git a/tools-ng/common/webxml-mergetool/lib/chardet.jar b/tools-ng/common/webxml-mergetool/lib/chardet.jar new file mode 100644 index 000000000..a46d26f57 Binary files /dev/null and b/tools-ng/common/webxml-mergetool/lib/chardet.jar differ diff --git a/tools-ng/common/webxml-mergetool/lib/dom4j.jar b/tools-ng/common/webxml-mergetool/lib/dom4j.jar new file mode 100644 index 000000000..c8c4dbb92 Binary files /dev/null and b/tools-ng/common/webxml-mergetool/lib/dom4j.jar differ diff --git a/tools-ng/common/webxml-mergetool/lib/jackrabbit-text-extractors.jar b/tools-ng/common/webxml-mergetool/lib/jackrabbit-text-extractors.jar new file mode 100644 index 000000000..df369080f Binary files /dev/null and b/tools-ng/common/webxml-mergetool/lib/jackrabbit-text-extractors.jar differ diff --git a/tools-ng/common/webxml-mergetool/lib/jericho-html.jar b/tools-ng/common/webxml-mergetool/lib/jericho-html.jar new file mode 100644 index 000000000..97c06ee12 Binary files /dev/null and b/tools-ng/common/webxml-mergetool/lib/jericho-html.jar differ diff --git a/tools-ng/common/webxml-mergetool/lib/nblibraries.properties b/tools-ng/common/webxml-mergetool/lib/nblibraries.properties new file mode 100644 index 000000000..03b52d682 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/lib/nblibraries.properties @@ -0,0 +1,2 @@ +libs.CopyLibs.classpath=\ + ${base}/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar diff --git a/tools-ng/common/webxml-mergetool/lib/poi-ooxml.jar b/tools-ng/common/webxml-mergetool/lib/poi-ooxml.jar new file mode 100644 index 000000000..c986646e6 Binary files /dev/null and b/tools-ng/common/webxml-mergetool/lib/poi-ooxml.jar differ diff --git a/tools-ng/common/webxml-mergetool/lib/poi.jar b/tools-ng/common/webxml-mergetool/lib/poi.jar new file mode 100644 index 000000000..9972d9732 Binary files /dev/null and b/tools-ng/common/webxml-mergetool/lib/poi.jar differ diff --git a/tools-ng/common/webxml-mergetool/lib/tomcat5-jsp-2.0-api-5.5.27.jar b/tools-ng/common/webxml-mergetool/lib/tomcat5-jsp-2.0-api-5.5.27.jar new file mode 100644 index 000000000..a9b6f5596 Binary files /dev/null and b/tools-ng/common/webxml-mergetool/lib/tomcat5-jsp-2.0-api-5.5.27.jar differ diff --git a/tools-ng/common/webxml-mergetool/lib/tomcat5-servlet-2.4-api-5.5.27.jar b/tools-ng/common/webxml-mergetool/lib/tomcat5-servlet-2.4-api-5.5.27.jar new file mode 100644 index 000000000..012998c38 Binary files /dev/null and b/tools-ng/common/webxml-mergetool/lib/tomcat5-servlet-2.4-api-5.5.27.jar differ diff --git a/tools-ng/common/webxml-mergetool/lib/xbean.jar b/tools-ng/common/webxml-mergetool/lib/xbean.jar new file mode 100644 index 000000000..9983d7226 Binary files /dev/null and b/tools-ng/common/webxml-mergetool/lib/xbean.jar differ diff --git a/tools-ng/common/webxml-mergetool/lib/xerces.jar b/tools-ng/common/webxml-mergetool/lib/xerces.jar new file mode 100644 index 000000000..99ee39d5b Binary files /dev/null and b/tools-ng/common/webxml-mergetool/lib/xerces.jar differ diff --git a/tools-ng/common/webxml-mergetool/manifest.mf b/tools-ng/common/webxml-mergetool/manifest.mf new file mode 100644 index 000000000..328e8e5bc --- /dev/null +++ b/tools-ng/common/webxml-mergetool/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/tools-ng/common/webxml-mergetool/nbbuild.xml b/tools-ng/common/webxml-mergetool/nbbuild.xml new file mode 100644 index 000000000..1ef09d9fc --- /dev/null +++ b/tools-ng/common/webxml-mergetool/nbbuild.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + Builds, tests, and runs the project webxml-mergetool. + + + diff --git a/tools-ng/common/webxml-mergetool/src/com/arsdigita/ant/WebXMLMergeTask.java b/tools-ng/common/webxml-mergetool/src/com/arsdigita/ant/WebXMLMergeTask.java new file mode 100644 index 000000000..20f795d3a --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/arsdigita/ant/WebXMLMergeTask.java @@ -0,0 +1,225 @@ +/* + * Copyright (C) 2011 Peter Boy (pb@zes.uni-bremen.de) All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package com.arsdigita.ant; + + +import com.liferay.portal.kernel.util.GetterUtil; +import com.liferay.portal.kernel.util.StringUtil; +import com.liferay.portal.kernel.xml.Document; +import com.liferay.portal.kernel.xml.DocumentException; +import com.liferay.portal.kernel.xml.Element; +import com.liferay.portal.kernel.xml.SAXReaderUtil; +import com.liferay.portal.xml.DocumentImpl; +import com.liferay.portal.xml.SAXReaderImpl; +import com.liferay.util.xml.XMLMerger; +import com.liferay.util.xml.descriptor.WebXML23Descriptor; +import com.liferay.util.xml.descriptor.WebXML24Descriptor; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.io.IOException; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; +// import org.apache.tools.ant.Project; + + + +/** + * + * @author pb + */ +public class WebXMLMergeTask extends Task { + + // Instance Variables ------------------------------------------------------ + + /** Location of the original web.xml. */ + private String originalFile; + + /** Location of the file to merge in web.xml. */ + private String mergeFile; + + /** Location of the resulting (destination) web.xml. */ + private String destFile; + + // Public Methods ---------------------------------------------------------- + + /** + * {@inheritDoc} + * @see Task#execute() + */ + @Override + public void execute() throws BuildException { + +// /* Check parameters */ +// if ((this.originalFile == null) || !this.originalFile.isFile()) +// { +// throw new BuildException("The [originalfile] attribute is required"); +// } +// if (this.mergeFile == null) +// { +// throw new BuildException("The [mergefile] attribute is required"); +// } +// if (this.destFile == null) +// { +// throw new BuildException("The [destfile] attribute is required"); +// } + + BuildWebXML(originalFile, mergeFile, destFile); + + + } + + private void BuildWebXML(String originalWebXML, String customWebXML, + String mergedWebXML) { + + try { + + String customContent = readFileIntoString(customWebXML); + + int x = customContent.indexOf("", x) + 1; + int y = customContent.indexOf(""); + customContent = customContent.substring(x, y); + + + String originalContent = readFileIntoString(originalWebXML); + + int z = originalContent.indexOf("", z) + 1; + + + String mergedContent = + originalContent.substring(0, z) + customContent + + originalContent.substring(z, originalContent.length()); + + mergedContent = processContent(mergedContent); + + + writeFileFromString(mergedWebXML, mergedContent); + + } + catch (Exception e) { + e.printStackTrace(); + } + } + + private String processContent(String webXML) + throws DocumentException, IOException { + + webXML = stripHtmlComments(webXML); + new SAXReaderUtil().setSAXReader(new SAXReaderImpl()); + + double version = 2.3; + + Document doc = SAXReaderUtil.read(webXML); + + Element root = doc.getRootElement(); + + version = GetterUtil.getDouble(root.attributeValue("version"), version); + + XMLMerger merger = null; + + if (version == 2.3) { + merger = new XMLMerger(new WebXML23Descriptor()); + } + else { + merger = new XMLMerger(new WebXML24Descriptor()); + } + + DocumentImpl docImpl = (DocumentImpl)doc; + + merger.organizeXML(docImpl.getWrappedDocument()); + + webXML = doc.formattedString(); + + return webXML; + + } + + private String readFileIntoString(String filename) + throws IOException { + + BufferedReader cbr = new BufferedReader( + new InputStreamReader( + new FileInputStream(filename))); + StringBuilder contentOfFile = new StringBuilder(); + String line; + while ((line = cbr.readLine()) != null) { + contentOfFile.append(line); + contentOfFile.append('\n'); + } + + return(contentOfFile.toString()); + } + + private void writeFileFromString(String fileName, String content) + throws IOException{ + + FileOutputStream fos = new FileOutputStream(fileName); + for (int i=0; i < content.length(); i++){ + fos.write((byte)content.charAt(i)); + } + fos.close(); + + } + + private String stripHtmlComments(String xmlContent) { + + String strippedContent = StringUtil.stripBetween(xmlContent, + ""); + return(strippedContent); + } + + /** + * The original web deployment descriptor into which the new elements will + * be merged. + * + * @param theSrcFile the original web.xml + */ + public final void setOriginalFile(String originalFile) + { + this.originalFile = originalFile; + } + + /** + * The descriptor to merge into the original file. + * + * @param theMergeFile the web.xml to merge + */ + public final void setMergeFile(String mergeFile) + { + this.mergeFile = mergeFile; + } + + /** + * The destination file where the result of the merge are stored. + * + * @param theDestFile the resulting web.xml + */ + public final void setDestFile(String destFile) + { + this.destFile = destFile; + } + + + +} diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncBufferedReader.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncBufferedReader.java new file mode 100644 index 000000000..6225f7e11 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncBufferedReader.java @@ -0,0 +1,378 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.io.unsync; + +import com.liferay.portal.kernel.util.CharPool; +import com.liferay.portal.kernel.util.StringBundler; + +import java.io.IOException; +import java.io.Reader; + +/** + *

+ * See http://issues.liferay.com/browse/LPS-6648. + *

+ * + * @author Shuyang Zhou + */ +public class UnsyncBufferedReader extends Reader { + + public UnsyncBufferedReader(Reader reader) { + this(reader, _DEFAULT_BUFFER_SIZE); + } + + public UnsyncBufferedReader(Reader reader, int size) { + this.reader = reader; + buffer = new char[size]; + } + + public void close() throws IOException { + if (reader != null) { + reader.close(); + + reader = null; + } + + buffer = null; + } + + public void mark(int markLimit) throws IOException { + if (reader == null) { + throw new IOException("Reader is null"); + } + + this.markLimit = markLimit; + markIndex = index; + } + + public boolean markSupported() { + return true; + } + + public int read() throws IOException { + if (reader == null) { + throw new IOException("Reader is null"); + } + + if (index >= firstInvalidIndex) { + readUnderlyingReader(); + + if (index >= firstInvalidIndex) { + return -1; + } + } + + return buffer[index++]; + } + + public int read(char[] charArray) throws IOException { + return read(charArray, 0, charArray.length); + } + + public int read(char[] charArray, int offset, int length) + throws IOException { + + if (reader == null) { + throw new IOException("Reader is null"); + } + + if (length <= 0) { + return 0; + } + + int read = 0; + + while (true) { + int available = firstInvalidIndex - index; + + if ((available + read) >= length) { + + // Enough data, stop reading + + int leftSize = length - read; + + System.arraycopy(buffer, index, charArray, read, leftSize); + + index += leftSize; + + return length; + } + + if (available <= 0) { + + // No more data in buffer, continue reading + + readUnderlyingReader(); + + available = firstInvalidIndex - index; + + if (available <= 0) { + + // Cannot read any more, stop reading + + if (read == 0) { + return -1; + } + else { + return read; + } + } + } + else { + + // Copy all in-memory data, continue reading + + System.arraycopy(buffer, index, charArray, read, available); + + index += available; + read += available; + } + } + } + + public String readLine() throws IOException { + if (reader == null) { + throw new IOException("Reader is null"); + } + + StringBundler sb = null; + + while (true) { + if (index >= firstInvalidIndex) { + readUnderlyingReader(); + } + + if (index >= firstInvalidIndex) { + if ((sb != null) && (sb.index() > 0)) { + return sb.toString(); + } + else { + return null; + } + } + + boolean hasLineBreak = false; + char lineEndChar = 0; + + int x = index; + int y = index; + + while (y < firstInvalidIndex) { + lineEndChar = buffer[y]; + + if ((lineEndChar == CharPool.NEW_LINE) || + (lineEndChar == CharPool.RETURN)) { + + hasLineBreak = true; + + break; + } + + y++; + } + + String line = new String(buffer, x, y - x); + + index = y; + + if (hasLineBreak) { + index++; + + if (lineEndChar == CharPool.RETURN) { + if ((index < buffer.length) && + (buffer[index] == CharPool.NEW_LINE)) { + + index++; + } + } + + if (sb == null) { + return line; + } + else { + sb.append(line); + + return sb.toString(); + } + } + + if (sb == null) { + sb = new StringBundler(); + } + + sb.append(line); + } + } + + public boolean ready() throws IOException { + if (reader == null) { + throw new IOException("Reader is null"); + } + + return (index < firstInvalidIndex) || reader.ready(); + } + + public void reset() throws IOException { + if (reader == null) { + throw new IOException("Reader is null"); + } + + if (markIndex < 0) { + throw new IOException("Resetting to invalid mark"); + } + + index = markIndex; + } + + public long skip(long skip) throws IOException { + if (reader == null) { + throw new IOException("Reader is null"); + } + + if (skip <= 0) { + return 0; + } + + long available = firstInvalidIndex - index; + + if (available > 0) { + + // Skip the data in buffer + + if (available < skip) { + skip = available; + } + } + else { + + // Skip the underlying reader + + if (markIndex < 0) { + + // No mark required, skip + + skip = reader.skip(skip); + } + else { + + // Mark required, save the skipped data + + readUnderlyingReader(); + + available = firstInvalidIndex - index; + + if (available > 0) { + + // Skip the data in buffer + + if (available < skip) { + skip = available; + } + } + } + } + + index += skip; + + return skip; + } + + protected void readUnderlyingReader() throws IOException { + if (markIndex < 0) { + + // No mark required, fill the buffer + + index = firstInvalidIndex = 0; + + int number = reader.read(buffer); + + if (number > 0) { + firstInvalidIndex = number; + } + + return; + } + + // Mark required + + if (index >= buffer.length) { + + // Buffer is full, clean up or grow + + if ((firstInvalidIndex - markIndex) > markLimit) { + + // Passed mark limit, get rid of all cache data + + markIndex = -1; + index = 0; + } + else if (markIndex > _MAX_MARK_WASTE_SIZE) { + + // There are more than _MAX_MARK_WASTE_SIZE free space at the + // beginning of buffer, clean up by shuffling the buffer + + int realDataSize = index - markIndex; + + System.arraycopy( + buffer, markIndex, buffer, 0, realDataSize); + + markIndex = 0; + index = realDataSize; + } + else { + + // Grow the buffer because we cannot get rid of cache data and + // it is inefficient to shuffle the buffer + + int newBufferSize = index << 1; + + if ((newBufferSize - _MAX_MARK_WASTE_SIZE) > markLimit) { + + // Make thew new buffer size larger than the mark limit + + newBufferSize = markLimit + _MAX_MARK_WASTE_SIZE; + } + + char[] newBuffer = new char[newBufferSize]; + + System.arraycopy(buffer, 0, newBuffer, 0, index); + + buffer = newBuffer; + } + } + + // Read underlying reader since the buffer has more space + + firstInvalidIndex = index; + + int number = reader.read(buffer, index, buffer.length - index); + + if (number > 0) { + firstInvalidIndex += number; + } + } + + protected char[] buffer; + protected int firstInvalidIndex; + protected int index; + protected int markIndex = -1; + protected int markLimit; + protected Reader reader; + + private static int _DEFAULT_BUFFER_SIZE = 8192; + + private static int _MAX_MARK_WASTE_SIZE = 4096; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncByteArrayInputStream.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncByteArrayInputStream.java new file mode 100644 index 000000000..73847ea29 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncByteArrayInputStream.java @@ -0,0 +1,111 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.io.unsync; + +import java.io.InputStream; + +/** + *

+ * See http://issues.liferay.com/browse/LPS-6648. + *

+ * + * @author Shuyang Zhou + */ +public class UnsyncByteArrayInputStream extends InputStream { + + public UnsyncByteArrayInputStream(byte[] buffer) { + this.buffer = buffer; + this.index = 0; + this.capacity = buffer.length; + } + + public UnsyncByteArrayInputStream(byte[] buffer, int offset, int length) { + this.buffer = buffer; + this.index = offset; + this.capacity = Math.min(buffer.length, offset + length); + this.markIndex = offset; + } + + public int available() { + return capacity - index; + } + + public void mark(int readAheadLimit) { + markIndex = index; + } + + public boolean markSupported() { + return true; + } + + public int read() { + if (index < capacity) { + return buffer[index++] & 0xff; + } + else { + return -1; + } + } + + public int read(byte[] byteArray) { + return read(byteArray, 0, byteArray.length); + } + + public int read(byte[] byteArray, int offset, int length) { + if (length <= 0) { + return 0; + } + + if (index >= capacity) { + return -1; + } + + int read = length; + + if ((index + read) > capacity) { + read = capacity - index; + } + + System.arraycopy(buffer, index, byteArray, offset, read); + + index += read; + + return read; + } + + public void reset() { + index = markIndex; + } + + public long skip(long skip) { + if (skip < 0) { + return 0; + } + + if ((skip + index) > capacity) { + skip = capacity - index; + } + + index += skip; + + return skip; + } + + protected byte[] buffer; + protected int capacity; + protected int index; + protected int markIndex; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncByteArrayOutputStream.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncByteArrayOutputStream.java new file mode 100644 index 000000000..4e1c13efe --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncByteArrayOutputStream.java @@ -0,0 +1,125 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.io.unsync; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; + +import java.nio.ByteBuffer; + +/** + *

+ * See http://issues.liferay.com/browse/LPS-6648. + *

+ * + * @author Shuyang Zhou + */ +public class UnsyncByteArrayOutputStream extends OutputStream { + + public UnsyncByteArrayOutputStream() { + this(32); + } + + public UnsyncByteArrayOutputStream(int size) { + buffer = new byte[size]; + } + + public void reset() { + index = 0; + } + + public int size() { + return index; + } + + public byte[] toByteArray() { + byte[] newBuffer = new byte[index]; + + System.arraycopy(buffer, 0, newBuffer, 0, index); + + return newBuffer; + } + + public String toString() { + return new String(buffer, 0, index); + } + + public String toString(String charsetName) + throws UnsupportedEncodingException { + + return new String(buffer, 0, index, charsetName); + } + + public byte[] unsafeGetByteArray() { + return buffer; + } + + public ByteBuffer unsafeGetByteBuffer() { + return ByteBuffer.wrap(buffer, 0, index); + } + + public void write(byte[] byteArray) { + write(byteArray, 0, byteArray.length); + } + + public void write(byte[] byteArray, int offset, int length) { + if (length <= 0) { + return; + } + + int newIndex = index + length; + + if (newIndex > buffer.length) { + int newBufferSize = Math.max(buffer.length << 1, newIndex); + + byte[] newBuffer = new byte[newBufferSize]; + + System.arraycopy(buffer, 0, newBuffer, 0, index); + + buffer = newBuffer; + } + + System.arraycopy(byteArray, offset, buffer, index, length); + + index = newIndex; + } + + public void write(int b) { + int newIndex = index + 1; + + if (newIndex > buffer.length) { + int newBufferSize = Math.max(buffer.length << 1, newIndex); + + byte[] newBuffer = new byte[newBufferSize]; + + System.arraycopy(buffer, 0, newBuffer, 0, buffer.length); + + buffer = newBuffer; + } + + buffer[index] = (byte)b; + + index = newIndex; + } + + public void writeTo(OutputStream outputStream) throws IOException { + outputStream.write(buffer, 0, index); + } + + protected byte[] buffer; + protected int index; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncPrintWriter.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncPrintWriter.java new file mode 100644 index 000000000..2bb7cf90a --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncPrintWriter.java @@ -0,0 +1,393 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.io.unsync; + +import com.liferay.portal.kernel.util.StringPool; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InterruptedIOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.io.Writer; + +import java.util.Formatter; +import java.util.Locale; + +/** + *

+ * See http://issues.liferay.com/browse/LPS-6648. + *

+ * + * @author Shuyang Zhou + */ +public class UnsyncPrintWriter extends PrintWriter { + + public UnsyncPrintWriter(File file) throws IOException { + this(new FileWriter(file), false); + } + + public UnsyncPrintWriter(File file, String csn) + throws FileNotFoundException, UnsupportedEncodingException { + + this(new OutputStreamWriter(new FileOutputStream(file), csn), false); + } + + public UnsyncPrintWriter(OutputStream outputStream) { + this(outputStream, false); + } + + public UnsyncPrintWriter(OutputStream outputStream, boolean autoFlush) { + this(new OutputStreamWriter(outputStream), autoFlush); + } + + public UnsyncPrintWriter(String fileName) throws IOException { + this(new FileWriter(fileName), false); + } + + public UnsyncPrintWriter(String fileName, String csn) + throws FileNotFoundException, UnsupportedEncodingException { + + this( + new OutputStreamWriter(new FileOutputStream(fileName), csn), false); + } + + public UnsyncPrintWriter(Writer writer) { + this(writer, false); + } + + public UnsyncPrintWriter(Writer writer, boolean autoFlush) { + super(writer); + + _writer = writer; + _autoFlush = autoFlush; + } + + public PrintWriter append(char c) { + write(c); + + return this; + } + + public PrintWriter append(CharSequence charSequence) { + if (charSequence == null) { + write(StringPool.NULL); + } + else { + write(charSequence.toString()); + } + + return this; + } + + public PrintWriter append(CharSequence charSequence, int start, int end) { + if (charSequence == null) { + charSequence = StringPool.NULL; + } + + write(charSequence.subSequence(start, end).toString()); + + return this; + } + + public boolean checkError() { + if (_writer != null) { + flush(); + } + + return _hasError; + } + + public void close() { + try { + if (_writer == null) { + return; + } + + _writer.close(); + + _writer = null; + } + catch (IOException ioe) { + _hasError = true; + } + } + + public void flush() { + if (_writer == null) { + _hasError = true; + } + else { + try { + _writer.flush(); + } + catch (IOException ioe) { + _hasError = true; + } + } + } + + public PrintWriter format( + Locale locale, String format, Object... arguments) { + + if (_writer == null) { + _hasError = true; + } + else { + try { + if ((_formatter == null) || (_formatter.locale() != locale)) { + _formatter = new Formatter(this, locale); + } + + _formatter.format(locale, format, arguments); + + if (_autoFlush) { + _writer.flush(); + } + } + catch (InterruptedIOException iioe) { + Thread currentThread = Thread.currentThread(); + + currentThread.interrupt(); + } + catch (IOException ioe) { + _hasError = true; + } + } + + return this; + } + + public PrintWriter format(String format, Object... arguments) { + return format(Locale.getDefault(), format, arguments); + } + + public void print(boolean b) { + if (b) { + write(StringPool.TRUE); + } + else { + write(StringPool.FALSE); + } + } + + public void print(char c) { + write(c); + } + + public void print(char[] charArray) { + write(charArray); + } + + public void print(double d) { + write(String.valueOf(d)); + } + + public void print(float f) { + write(String.valueOf(f)); + } + + public void print(int i) { + write(String.valueOf(i)); + } + + public void print(long l) { + write(String.valueOf(l)); + } + + public void print(Object object) { + write(String.valueOf(object)); + } + + public void print(String string) { + if (string == null) { + string = StringPool.NULL; + } + + write(string); + } + + public PrintWriter printf( + Locale locale, String format, Object... arguments) { + + return format(locale, format, arguments); + } + + public PrintWriter printf(String format, Object... arguments) { + return format(format, arguments); + } + + public void println() { + if (_writer == null) { + _hasError = true; + } + else { + try { + _writer.write(_LINE_SEPARATOR); + + if (_autoFlush) { + _writer.flush(); + } + } + catch (InterruptedIOException iioe) { + Thread currentThread = Thread.currentThread(); + + currentThread.interrupt(); + } + catch (IOException ioe) { + _hasError = true; + } + } + } + + public void println(boolean b) { + print(b); + println(); + } + + public void println(char c) { + print(c); + println(); + } + + public void println(char[] charArray) { + print(charArray); + println(); + } + + public void println(double d) { + print(d); + println(); + } + + public void println(float f) { + print(f); + println(); + } + + public void println(int i) { + print(i); + println(); + } + + public void println(long l) { + print(l); + println(); + } + + public void println(Object object) { + print(object); + println(); + } + + public void println(String string) { + print(string); + println(); + } + + public void write(char[] charArray) { + write(charArray, 0, charArray.length); + } + + public void write(char[] charArray, int offset, int length) { + if (_writer == null) { + _hasError = true; + } + else { + try { + _writer.write(charArray, offset, length); + } + catch (InterruptedIOException iioe) { + Thread currentThread = Thread.currentThread(); + + currentThread.interrupt(); + } + catch (IOException ioe) { + _hasError = true; + } + } + } + + public void write(int c) { + if (_writer == null) { + _hasError = true; + } + else { + try { + _writer.write(c); + } + catch (InterruptedIOException iioe) { + Thread currentThread = Thread.currentThread(); + + currentThread.interrupt(); + } + catch (IOException ioe) { + _hasError = true; + } + } + } + + public void write(String string) { + if (_writer == null) { + _hasError = true; + } + else { + try { + _writer.write(string); + } + catch (InterruptedIOException iioe) { + Thread currentThread = Thread.currentThread(); + + currentThread.interrupt(); + } + catch (IOException ioe) { + _hasError = true; + } + } + } + + public void write(String string, int offset, int length) { + if (_writer == null) { + _hasError = true; + } + else { + try { + _writer.write(string, offset, length); + } + catch (InterruptedIOException iioe) { + Thread currentThread = Thread.currentThread(); + + currentThread.interrupt(); + } + catch (IOException ioe) { + _hasError = true; + } + } + } + + private static String _LINE_SEPARATOR = System.getProperty( + "line.separator"); + + private boolean _autoFlush; + private Formatter _formatter; + private boolean _hasError; + private Writer _writer; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncStringReader.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncStringReader.java new file mode 100644 index 000000000..e3000eacd --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncStringReader.java @@ -0,0 +1,144 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.io.unsync; + +import java.io.IOException; +import java.io.Reader; + +import java.nio.CharBuffer; + +/** + *

+ * See http://issues.liferay.com/browse/LPS-6648. + *

+ * + * @author Shuyang Zhou + */ +public class UnsyncStringReader extends Reader { + + public UnsyncStringReader(String string) { + this.string = string; + stringLength = string.length(); + } + + public void close() { + string = null; + } + + public void mark(int readAheadLimit) throws IOException { + if (string == null) { + throw new IOException("String is null"); + } + markIndex = index; + } + + public boolean markSupported() { + return true; + } + + public int read() throws IOException { + if (string == null) { + throw new IOException("String is null"); + } + + if (index >= stringLength) { + return -1; + } + + return string.charAt(index++); + } + + public int read(char[] charArray) throws IOException { + return read(charArray, 0, charArray.length); + } + + public int read(char[] charArray, int offset, int length) + throws IOException { + + if (string == null) { + throw new IOException("String is null"); + } + + if (length <= 0) { + return 0; + } + + if (index >= stringLength) { + return -1; + } + + int read = length; + + if ((index + read) > stringLength) { + read = stringLength - index; + } + + string.getChars(index, index + read, charArray, offset); + + index += read; + + return read; + } + + public int read(CharBuffer charBuffer) throws IOException { + int remaining = charBuffer.remaining(); + + char[] charArray = new char[remaining]; + + int read = read(charArray, 0, remaining); + + if (read > 0) { + charBuffer.put(charArray, 0, read); + } + + return read; + } + + public boolean ready() throws IOException { + if (string == null) { + throw new IOException("String is null"); + } + + return true; + } + + public void reset() throws IOException { + if (string == null) { + throw new IOException("String is null"); + } + + index = markIndex; + } + + public long skip(long skip) { + if (index >= stringLength) { + return 0; + } + + if ((skip + index) > stringLength) { + skip = stringLength - index; + } + + index += skip; + + return skip; + } + + protected int index; + protected int stringLength; + protected int markIndex; + protected String string; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncStringWriter.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncStringWriter.java new file mode 100644 index 000000000..8ddd6def0 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/io/unsync/UnsyncStringWriter.java @@ -0,0 +1,166 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.io.unsync; + +import com.liferay.portal.kernel.util.StringBundler; +import com.liferay.portal.kernel.util.StringPool; + +import java.io.Writer; + +/** + *

+ * See http://issues.liferay.com/browse/LPS-6648. + *

+ * + * @author Shuyang Zhou + */ +public class UnsyncStringWriter extends Writer { + + public UnsyncStringWriter() { + this(true); + } + + public UnsyncStringWriter(boolean useStringBundler) { + if (useStringBundler) { + stringBundler = new StringBundler(); + } + else { + stringBuilder = new StringBuilder(); + } + } + + public UnsyncStringWriter(boolean useStringBundler, int initialCapacity) { + if (useStringBundler) { + stringBundler = new StringBundler(initialCapacity); + } + else { + stringBuilder = new StringBuilder(initialCapacity); + } + } + + public UnsyncStringWriter(int initialCapacity) { + this(true, initialCapacity); + } + + public UnsyncStringWriter append(char c) { + write(c); + + return this; + } + + public UnsyncStringWriter append(CharSequence charSequence) { + if (charSequence == null) { + write(StringPool.NULL); + } + else { + write(charSequence.toString()); + } + + return this; + } + + public UnsyncStringWriter append( + CharSequence charSequence, int start, int end) { + + if (charSequence == null) { + charSequence = StringPool.NULL; + } + + write(charSequence.subSequence(start, end).toString()); + + return this; + } + + public void close() { + } + + public void flush() { + } + + public StringBuilder getStringBuilder() { + return stringBuilder; + } + + public StringBundler getStringBundler() { + return stringBundler; + } + + public void reset() { + if (stringBundler != null) { + stringBundler.setIndex(0); + } + else { + stringBuilder.setLength(0); + } + } + + public String toString() { + if (stringBundler != null) { + return stringBundler.toString(); + } + else { + return stringBuilder.toString(); + } + } + + public void write(char[] charArray, int offset, int length) { + if (length <= 0) { + return; + } + + if (stringBundler != null) { + stringBundler.append(new String(charArray, offset, length)); + } + else { + stringBuilder.append(charArray, offset, length); + } + } + + public void write(char[] charArray) { + write(charArray, 0, charArray.length); + + } + + public void write(int c) { + if (stringBundler != null) { + stringBundler.append(String.valueOf((char)c)); + } + else { + stringBuilder.append((char)c); + } + } + + public void write(String string) { + if (stringBundler != null) { + stringBundler.append(string); + } + else { + stringBuilder.append(string); + } + } + + public void write(String string, int offset, int length) { + if (stringBundler != null) { + stringBundler.append(string.substring(offset, offset + length)); + } + else { + stringBuilder.append(string.substring(offset, offset + length)); + } + } + + protected StringBuilder stringBuilder; + protected StringBundler stringBundler; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/Jdk14LogFactoryImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/Jdk14LogFactoryImpl.java new file mode 100644 index 000000000..c92a072eb --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/Jdk14LogFactoryImpl.java @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.log; + +import java.util.logging.Logger; + +/** + * @author Brian Wing Shun Chan + */ +public class Jdk14LogFactoryImpl implements LogFactory { + + public Log getLog(Class c) { + return getLog(c.getName()); + } + + public Log getLog(String name) { + return new Jdk14LogImpl(Logger.getLogger(name)); + } + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/Jdk14LogImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/Jdk14LogImpl.java new file mode 100644 index 000000000..81fc171c0 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/Jdk14LogImpl.java @@ -0,0 +1,127 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.log; + +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * @author Brian Wing Shun Chan + */ +public class Jdk14LogImpl implements Log { + + public Jdk14LogImpl(Logger log) { + _log = log; + } + + public void debug(Object msg) { + _log.log(Level.FINE, msg.toString()); + } + + public void debug(Throwable t) { + _log.log(Level.FINE, t.getMessage(), t); + } + + public void debug(Object msg, Throwable t) { + _log.log(Level.FINE, msg.toString(), t); + } + + public void error(Object msg) { + _log.log(Level.SEVERE, msg.toString()); + } + + public void error(Throwable t) { + _log.log(Level.SEVERE, t.getMessage(), t); + } + + public void error(Object msg, Throwable t) { + _log.log(Level.SEVERE, msg.toString(), t); + } + + public void fatal(Object msg) { + _log.log(Level.SEVERE, msg.toString()); + } + + public void fatal(Throwable t) { + _log.log(Level.SEVERE, t.getMessage(), t); + } + + public void fatal(Object msg, Throwable t) { + _log.log(Level.SEVERE, msg.toString(), t); + } + + public void info(Object msg) { + _log.log(Level.INFO, msg.toString()); + } + + public void info(Throwable t) { + _log.log(Level.INFO, t.getMessage(), t); + } + + public void info(Object msg, Throwable t) { + _log.log(Level.INFO, msg.toString(), t); + } + + public boolean isDebugEnabled() { + return _log.isLoggable(Level.FINE); + } + + public boolean isErrorEnabled() { + return _log.isLoggable(Level.SEVERE); + } + + public boolean isFatalEnabled() { + return _log.isLoggable(Level.SEVERE); + } + + public boolean isInfoEnabled() { + return _log.isLoggable(Level.INFO); + } + + public boolean isTraceEnabled() { + return _log.isLoggable(Level.FINEST); + } + + public boolean isWarnEnabled() { + return _log.isLoggable(Level.WARNING); + } + + public void trace(Object msg) { + _log.log(Level.FINEST, msg.toString()); + } + + public void trace(Throwable t) { + _log.log(Level.FINEST, t.getMessage(), t); + } + + public void trace(Object msg, Throwable t) { + _log.log(Level.FINEST, msg.toString(), t); + } + + public void warn(Object msg) { + _log.log(Level.WARNING, msg.toString()); + } + + public void warn(Throwable t) { + _log.log(Level.WARNING, t.getMessage(), t); + } + + public void warn(Object msg, Throwable t) { + _log.log(Level.WARNING, msg.toString(), t); + } + + private Logger _log; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/Log.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/Log.java new file mode 100644 index 000000000..8723719c5 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/Log.java @@ -0,0 +1,70 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.log; + +/** + * @author Brian Wing Shun Chan + */ +public interface Log { + + public void debug(Object msg); + + public void debug(Throwable t); + + public void debug(Object msg, Throwable t); + + public void error(Object msg); + + public void error(Throwable t); + + public void error(Object msg, Throwable t); + + public void fatal(Object msg); + + public void fatal(Throwable t); + + public void fatal(Object msg, Throwable t); + + public void info(Object msg); + + public void info(Throwable t); + + public void info(Object msg, Throwable t); + + public boolean isDebugEnabled(); + + public boolean isErrorEnabled(); + + public boolean isFatalEnabled(); + + public boolean isInfoEnabled(); + + public boolean isTraceEnabled(); + + public boolean isWarnEnabled(); + + public void trace(Object msg); + + public void trace(Throwable t); + + public void trace(Object msg, Throwable t); + + public void warn(Object msg); + + public void warn(Throwable t); + + public void warn(Object msg, Throwable t); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/LogFactory.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/LogFactory.java new file mode 100644 index 000000000..ecb76ab95 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/LogFactory.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.log; + +/** + * @author Brian Wing Shun Chan + */ +public interface LogFactory { + + public Log getLog(Class c); + + public Log getLog(String name); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/LogFactoryUtil.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/LogFactoryUtil.java new file mode 100644 index 000000000..5d34326dd --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/LogFactoryUtil.java @@ -0,0 +1,74 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.log; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * @author Brian Wing Shun Chan + * @author Shuyang Zhou + */ +public class LogFactoryUtil { + + public static Log getLog(Class c) { + return getLog(c.getName()); + } + + public static Log getLog(String name) { + + // The following concurrent collection retrieve has the side effect of a + // memory fence read. This will invalidate all dirty cache data if there + // are any. If the LogWrapper swap happens before this, the new Log will + // be visible to the current Thread. + + LogWrapper logWrapper = _logWrappers.get(name); + + if (logWrapper == null) { + logWrapper = new LogWrapper(_logFactory.getLog(name)); + + LogWrapper previousLogWrapper = _logWrappers.putIfAbsent( + name, logWrapper); + + if (previousLogWrapper != null) { + logWrapper = previousLogWrapper; + } + } + + return logWrapper; + } + + public static void setLogFactory(LogFactory logFactory) { + for (Map.Entry entry : _logWrappers.entrySet()) { + String name = entry.getKey(); + + LogWrapper logWrapper = entry.getValue(); + + logWrapper.setLog(logFactory.getLog(name)); + } + + // The following volatile write will flush out all cache data. All + // previously swapped LogWrappers will be visible for any reads after a + // memory fence read according to the happens-before rules. + + _logFactory = logFactory; + } + + private static volatile LogFactory _logFactory = new Jdk14LogFactoryImpl(); + private static final ConcurrentMap _logWrappers = + new ConcurrentHashMap(); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/LogWrapper.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/LogWrapper.java new file mode 100644 index 000000000..e0fb6ae32 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/log/LogWrapper.java @@ -0,0 +1,222 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.log; + +/** + * @author Brian Wing Shun Chan + */ +public class LogWrapper implements Log { + + public LogWrapper(Log log) { + _log = log; + } + + public void setLog(Log log) { + _log = log; + } + + public void debug(Object msg) { + try { + _log.debug(msg); + } + catch (Exception e) { + printMsg(msg); + } + } + + public void debug(Throwable t) { + try { + _log.debug(t); + } + catch (Exception e) { + printMsg(t.getMessage()); + } + } + + public void debug(Object msg, Throwable t) { + try { + _log.debug(msg, t); + } + catch (Exception e) { + printMsg(msg); + } + } + + public void error(Object msg) { + try { + _log.error(msg); + } + catch (Exception e) { + printMsg(msg); + } + } + + public void error(Throwable t) { + try { + _log.error(t); + } + catch (Exception e) { + printMsg(t.getMessage()); + } + } + + public void error(Object msg, Throwable t) { + try { + _log.error(msg, t); + } + catch (Exception e) { + printMsg(msg); + } + } + + public void fatal(Object msg) { + try { + _log.fatal(msg); + } + catch (Exception e) { + printMsg(msg); + } + } + + public void fatal(Throwable t) { + try { + _log.fatal(t); + } + catch (Exception e) { + printMsg(t.getMessage()); + } + } + + public void fatal(Object msg, Throwable t) { + try { + _log.fatal(msg, t); + } + catch (Exception e) { + printMsg(msg); + } + } + + public void info(Object msg) { + try { + _log.info(msg); + } + catch (Exception e) { + printMsg(msg); + } + } + + public void info(Throwable t) { + try { + _log.info(t); + } + catch (Exception e) { + printMsg(t.getMessage()); + } + } + + public void info(Object msg, Throwable t) { + try { + _log.info(msg, t); + } + catch (Exception e) { + printMsg(msg); + } + } + + public boolean isDebugEnabled() { + return _log.isDebugEnabled(); + } + + public boolean isErrorEnabled() { + return _log.isErrorEnabled(); + } + + public boolean isFatalEnabled() { + return _log.isFatalEnabled(); + } + + public boolean isInfoEnabled() { + return _log.isInfoEnabled(); + } + + public boolean isTraceEnabled() { + return _log.isTraceEnabled(); + } + + public boolean isWarnEnabled() { + return _log.isWarnEnabled(); + } + + public void trace(Object msg) { + try { + _log.trace(msg); + } + catch (Exception e) { + printMsg(msg); + } + } + + public void trace(Throwable t) { + try { + _log.trace(t); + } + catch (Exception e) { + printMsg(t.getMessage()); + } + } + + public void trace(Object msg, Throwable t) { + try { + _log.trace(msg, t); + } + catch (Exception e) { + printMsg(msg); + } + } + + public void warn(Object msg) { + try { + _log.warn(msg); + } + catch (Exception e) { + printMsg(msg); + } + } + + public void warn(Throwable t) { + try { + _log.warn(t); + } + catch (Exception e) { + printMsg(t.getMessage()); + } + } + + public void warn(Object msg, Throwable t) { + try { + _log.warn(msg, t); + } + catch (Exception e) { + printMsg(msg); + } + } + + protected void printMsg(Object msg) { + System.err.println(msg); + } + + private Log _log; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/CharPool.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/CharPool.java new file mode 100644 index 000000000..f85518f6c --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/CharPool.java @@ -0,0 +1,216 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +/** + * @author Brian Wing Shun Chan + */ +public class CharPool { + + public static final char AMPERSAND = '&'; + + public static final char APOSTROPHE = '\''; + + public static final char AT = '@'; + + public static final char BACK_SLASH = '\\'; + + public static final char CLOSE_BRACKET = ']'; + + public static final char CLOSE_CURLY_BRACE = '}'; + + public static final char CLOSE_PARENTHESIS = ')'; + + public static final char COLON = ':'; + + public static final char COMMA = ','; + + public static final char DASH = '-'; + + public static final char EQUAL = '='; + + public static final char GREATER_THAN = '>'; + + public static final char FORWARD_SLASH = '/'; + + public static final char LESS_THAN = '<'; + + public static final char LOWER_CASE_A = 'a'; + + public static final char LOWER_CASE_B = 'b'; + + public static final char LOWER_CASE_C = 'c'; + + public static final char LOWER_CASE_D = 'd'; + + public static final char LOWER_CASE_E = 'e'; + + public static final char LOWER_CASE_F = 'f'; + + public static final char LOWER_CASE_G = 'g'; + + public static final char LOWER_CASE_H = 'h'; + + public static final char LOWER_CASE_I = 'i'; + + public static final char LOWER_CASE_J = 'j'; + + public static final char LOWER_CASE_K = 'k'; + + public static final char LOWER_CASE_L = 'l'; + + public static final char LOWER_CASE_M = 'm'; + + public static final char LOWER_CASE_N = 'n'; + + public static final char LOWER_CASE_O = 'o'; + + public static final char LOWER_CASE_P = 'p'; + + public static final char LOWER_CASE_Q = 'q'; + + public static final char LOWER_CASE_R = 'r'; + + public static final char LOWER_CASE_S = 's'; + + public static final char LOWER_CASE_T = 't'; + + public static final char LOWER_CASE_U = 'u'; + + public static final char LOWER_CASE_V = 'v'; + + public static final char LOWER_CASE_W = 'w'; + + public static final char LOWER_CASE_X = 'x'; + + public static final char LOWER_CASE_Y = 'y'; + + public static final char LOWER_CASE_Z = 'z'; + + public static final char MINUS = '-'; + + public static final char NEW_LINE = '\n'; + + public static final char NUMBER_0 = '0'; + + public static final char NUMBER_1 = '1'; + + public static final char NUMBER_2 = '2'; + + public static final char NUMBER_3 = '3'; + + public static final char NUMBER_4 = '4'; + + public static final char NUMBER_5 = '5'; + + public static final char NUMBER_6 = '6'; + + public static final char NUMBER_7 = '7'; + + public static final char NUMBER_8 = '8'; + + public static final char NUMBER_9 = '9'; + + public static final char OPEN_BRACKET = '['; + + public static final char OPEN_CURLY_BRACE = '{'; + + public static final char OPEN_PARENTHESIS = '('; + + public static final char PERCENT = '%'; + + public static final char PERIOD = '.'; + + public static final char PIPE = '|'; + + public static final char PLUS = '+'; + + public static final char POUND = '#'; + + public static final char QUESTION = '?'; + + public static final char QUOTE = '\"'; + + public static final char RAQUO = '\u00bb'; // '�' + + public static final char RETURN = '\r'; + + public static final char SEMICOLON = ';'; + + public static final char SLASH = FORWARD_SLASH; + + public static final char SPACE = ' '; + + public static final char STAR = '*'; + + public static final char TAB = '\t'; + + public static final char TILDE = '~'; + + public static final char UNDERLINE = '_'; + + public static final char UPPER_CASE_A = 'A'; + + public static final char UPPER_CASE_B = 'B'; + + public static final char UPPER_CASE_C = 'C'; + + public static final char UPPER_CASE_D = 'D'; + + public static final char UPPER_CASE_E = 'E'; + + public static final char UPPER_CASE_F = 'F'; + + public static final char UPPER_CASE_G = 'G'; + + public static final char UPPER_CASE_H = 'H'; + + public static final char UPPER_CASE_I = 'I'; + + public static final char UPPER_CASE_J = 'J'; + + public static final char UPPER_CASE_K = 'K'; + + public static final char UPPER_CASE_L = 'L'; + + public static final char UPPER_CASE_M = 'M'; + + public static final char UPPER_CASE_N = 'N'; + + public static final char UPPER_CASE_O = 'O'; + + public static final char UPPER_CASE_P = 'P'; + + public static final char UPPER_CASE_Q = 'Q'; + + public static final char UPPER_CASE_R = 'R'; + + public static final char UPPER_CASE_S = 'S'; + + public static final char UPPER_CASE_T = 'T'; + + public static final char UPPER_CASE_U = 'U'; + + public static final char UPPER_CASE_V = 'V'; + + public static final char UPPER_CASE_W = 'W'; + + public static final char UPPER_CASE_X = 'X'; + + public static final char UPPER_CASE_Y = 'Y'; + + public static final char UPPER_CASE_Z = 'Z'; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/ClassUtil.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/ClassUtil.java new file mode 100644 index 000000000..059057e4b --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/ClassUtil.java @@ -0,0 +1,332 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader; +import com.liferay.portal.kernel.io.unsync.UnsyncStringReader; +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; +import java.io.StreamTokenizer; + +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author Brian Wing Shun Chan + * @author Sandeep Soni + */ +public class ClassUtil { + + public static Set getClasses(File file) throws IOException { + String fileName = file.getName(); + + if (fileName.endsWith(".java")) { + fileName = fileName.substring(0, fileName.length() - 5); + } + + return getClasses(new FileReader(file), fileName); + } + + public static Set getClasses(Reader reader, String className) + throws IOException { + + Set classes = new HashSet(); + + StreamTokenizer st = new StreamTokenizer( + new UnsyncBufferedReader(reader)); + + _setupParseTableForAnnotationProcessing(st); + + while (st.nextToken() != StreamTokenizer.TT_EOF) { + if (st.ttype == StreamTokenizer.TT_WORD) { + if (st.sval.equals("class") || st.sval.equals("enum") || + st.sval.equals("interface") || + st.sval.equals("@interface")) { + + break; + } + else if (st.sval.startsWith("@")) { + st.ordinaryChar(' '); + st.wordChars('=', '='); + + String[] las = _processAnnotation(st.sval, st); + + for (int i = 0; i < las.length; i++) { + classes.add(las[i]); + } + + _setupParseTableForAnnotationProcessing(st); + } + } + } + + _setupParseTable(st); + + while (st.nextToken() != StreamTokenizer.TT_EOF) { + if (st.ttype == StreamTokenizer.TT_WORD) { + if (st.sval.indexOf('.') >= 0) { + classes.add(st.sval.substring(0, st.sval.indexOf('.'))); + } + else { + classes.add(st.sval); + } + } + else if (st.ttype != StreamTokenizer.TT_NUMBER && + st.ttype != StreamTokenizer.TT_EOL) { + + if (Character.isUpperCase((char)st.ttype)) { + classes.add(String.valueOf((char)st.ttype)); + } + } + } + + classes.remove(className); + + return classes; + } + + public static String getParentPath( + ClassLoader classLoader, String className) { + + if (_log.isDebugEnabled()) { + _log.debug("Class name " + className); + } + + if (!className.endsWith(_CLASS_EXTENSION)) { + className += _CLASS_EXTENSION; + } + + className = StringUtil.replace( + className, StringPool.PERIOD, StringPool.SLASH); + + className = StringUtil.replace(className, "/class", _CLASS_EXTENSION); + + URL url = classLoader.getResource(className); + + String path = null; + + try { + path = new URI(url.getPath()).getPath(); + } + catch (URISyntaxException urise) { + path = url.getFile(); + } + + if (_log.isDebugEnabled()) { + _log.debug("Path " + path); + } + + int pos = path.indexOf(className); + + String parentPath = path.substring(0, pos); + + if (parentPath.startsWith("jar:")) { + parentPath = parentPath.substring(4, parentPath.length()); + } + + if (parentPath.startsWith("file:/")) { + parentPath = parentPath.substring(6, parentPath.length()); + } + + if (_log.isDebugEnabled()) { + _log.debug("Parent path " + parentPath); + } + + return parentPath; + } + + public static boolean isSubclass(Class a, Class b) { + if (a == b) { + return true; + } + + if (a == null || b == null) { + return false; + } + + for (Class x = a; x != null; x = x.getSuperclass()) { + if (x == b) { + return true; + } + + if (b.isInterface()) { + Class[] interfaces = x.getInterfaces(); + + for (int i = 0; i < interfaces.length; i++) { + if (isSubclass(interfaces[i], b)) { + return true; + } + } + } + } + + return false; + } + + public static boolean isSubclass(Class a, String s) { + if (a == null || s == null) { + return false; + } + + if (a.getName().equals(s)) { + return true; + } + + for (Class x = a; x != null; x = x.getSuperclass()) { + if (x.getName().equals(s)) { + return true; + } + + Class[] interfaces = x.getInterfaces(); + + for (int i = 0; i < interfaces.length; i++) { + if (isSubclass(interfaces[i], s)) { + return true; + } + } + } + + return false; + } + + private static String[] _processAnnotation(String s, StreamTokenizer st) + throws IOException { + + s = s.trim(); + + List tokens = new ArrayList(); + + Matcher annotationNameMatcher = _ANNOTATION_NAME_REGEXP.matcher(s); + Matcher annotationParametersMatcher = + _ANNOTATION_PARAMETERS_REGEXP.matcher(s); + + if (annotationNameMatcher.matches()) { + String annotationName = annotationNameMatcher.group(); + + tokens.add(annotationName.replace("@", "")); + } + else if (annotationParametersMatcher.matches()) { + if (!s.trim().endsWith(")")) { + while (st.nextToken() != StreamTokenizer.TT_EOF) { + if (st.ttype == StreamTokenizer.TT_WORD) { + s += st.sval; + if (s.trim().endsWith(")")) { + break; + } + } + } + } + + annotationParametersMatcher = + _ANNOTATION_PARAMETERS_REGEXP.matcher(s); + + if (annotationParametersMatcher.matches()) { + String annotationName = + annotationParametersMatcher.group(1); + String annotationParameters = + annotationParametersMatcher.group(2); + + tokens.add(annotationName.replace("@", "")); + + tokens = _processAnnotationParameters( + annotationParameters,tokens); + } + } + + return tokens.toArray(new String[tokens.size()]); + } + + private static List _processAnnotationParameters( + String s, List tokens) + throws IOException { + + StreamTokenizer st = new StreamTokenizer(new UnsyncStringReader(s)); + + _setupParseTable(st); + + while (st.nextToken() != StreamTokenizer.TT_EOF) { + if (st.ttype == StreamTokenizer.TT_WORD) { + if (st.sval.indexOf('.') >= 0) { + tokens.add(st.sval.substring(0, st.sval.indexOf('.'))); + } + else { + tokens.add(st.sval); + } + } + else if ((st.ttype != StreamTokenizer.TT_NUMBER) && + (st.ttype != StreamTokenizer.TT_EOL)) { + + if (Character.isUpperCase((char)st.ttype)) { + tokens.add(String.valueOf((char)st.ttype)); + } + } + } + + return tokens; + } + + private static void _setupParseTable(StreamTokenizer st) { + st.resetSyntax(); + st.slashSlashComments(true); + st.slashStarComments(true); + st.wordChars('a', 'z'); + st.wordChars('A', 'Z'); + st.wordChars('.', '.'); + st.wordChars('0', '9'); + st.wordChars('_', '_'); + st.lowerCaseMode(false); + st.eolIsSignificant(false); + st.quoteChar('"'); + st.quoteChar('\''); + st.parseNumbers(); + } + + private static void _setupParseTableForAnnotationProcessing( + StreamTokenizer st) { + + _setupParseTable(st); + + st.wordChars('@', '@'); + st.wordChars('(', '('); + st.wordChars(')', ')'); + st.wordChars('{', '{'); + st.wordChars('}', '}'); + st.wordChars(',',','); + } + + private static final Pattern _ANNOTATION_NAME_REGEXP = + Pattern.compile("@(\\w+)$"); + + private static final Pattern _ANNOTATION_PARAMETERS_REGEXP = + Pattern.compile("@(\\w+)\\({0,1}\\{{0,1}([^)}]+)\\}{0,1}\\){0,1}"); + + private static final String _CLASS_EXTENSION = ".class"; + + private static Log _log = LogFactoryUtil.getLog(ClassUtil.class); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/File.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/File.java new file mode 100644 index 000000000..93323561e --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/File.java @@ -0,0 +1,157 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; + +import java.util.List; +import java.util.Properties; + +/** + * @author Brian Wing Shun Chan + * @author Alexander Chow + */ +public interface File { + + public void copyDirectory(String sourceDirName, String destinationDirName); + + public void copyDirectory(java.io.File source, java.io.File destination); + + public void copyFile(String source, String destination); + + public void copyFile(String source, String destination, boolean lazy); + + public void copyFile(java.io.File source, java.io.File destination); + + public void copyFile( + java.io.File source, java.io.File destination, boolean lazy); + + public java.io.File createTempFile(); + + public java.io.File createTempFile(String extension); + + public String createTempFileName(); + + public String createTempFileName(String extension); + + public String decodeSafeFileName(String fileName); + + public boolean delete(String file); + + public boolean delete(java.io.File file); + + public void deltree(String directory); + + public void deltree(java.io.File directory); + + public String encodeSafeFileName(String fileName); + + public boolean exists(String fileName); + + public boolean exists(java.io.File file); + + public String extractText(InputStream is, String fileName); + + public String getAbsolutePath(java.io.File file); + + public byte[] getBytes(java.io.File file) throws IOException; + + public byte[] getBytes(InputStream is) throws IOException; + + public byte[] getBytes(InputStream is, int bufferSize) throws IOException; + + public String getExtension(String fileName); + + public String getPath(String fullFileName); + + public String getShortFileName(String fullFileName); + + public boolean isAscii(java.io.File file) throws IOException; + + public String[] listDirs(String fileName); + + public String[] listDirs(java.io.File file); + + public String[] listFiles(String fileName); + + public String[] listFiles(java.io.File file); + + public void mkdirs(String pathName); + + public boolean move(String sourceFileName, String destinationFileName); + + public boolean move(java.io.File source, java.io.File destination); + + public String read(String fileName) throws IOException; + + public String read(java.io.File file) throws IOException; + + public String read(java.io.File file, boolean raw) throws IOException; + + public String replaceSeparator(String fileName); + + public java.io.File[] sortFiles(java.io.File[] files); + + public String stripExtension(String fileName); + + public List toList(Reader reader); + + public List toList(String fileName); + + public Properties toProperties(java.io.FileInputStream fis); + + public Properties toProperties(String fileName); + + public void write(String fileName, String s) throws IOException; + + public void write(String fileName, String s, boolean lazy) + throws IOException; + + public void write(String fileName, String s, boolean lazy, boolean append) + throws IOException; + + public void write(String pathName, String fileName, String s) + throws IOException; + + public void write(String pathName, String fileName, String s, boolean lazy) + throws IOException; + + public void write( + String pathName, String fileName, String s, boolean lazy, + boolean append) + throws IOException; + + public void write(java.io.File file, String s) throws IOException; + + public void write(java.io.File file, String s, boolean lazy) + throws IOException; + + public void write(java.io.File file, String s, boolean lazy, boolean append) + throws IOException; + + public void write(String fileName, byte[] bytes) throws IOException; + + public void write(java.io.File file, byte[] bytes) throws IOException; + + public void write(java.io.File file, byte[] bytes, int offset, int length) + throws IOException; + + public void write(String fileName, InputStream is) throws IOException; + + public void write(java.io.File file, InputStream is) throws IOException; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/FileUtil.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/FileUtil.java new file mode 100644 index 000000000..6d7105795 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/FileUtil.java @@ -0,0 +1,313 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; + +import java.util.List; +import java.util.Properties; + +/** + * @author Brian Wing Shun Chan + * @author Alexander Chow + */ +public class FileUtil { + + public static void copyDirectory( + String sourceDirName, String destinationDirName) { + + getFile().copyDirectory(sourceDirName, destinationDirName); + } + + public static void copyDirectory(File source, File destination) { + getFile().copyDirectory(source, destination); + } + + public static void copyFile(String source, String destination) { + getFile().copyFile(source, destination); + } + + public static void copyFile( + String source, String destination, boolean lazy) { + + getFile().copyFile(source, destination, lazy); + } + + public static void copyFile(File source, File destination) { + getFile().copyFile(source, destination); + } + + public static void copyFile(File source, File destination, boolean lazy) { + getFile().copyFile(source, destination, lazy); + } + + public static File createTempFile() { + return getFile().createTempFile(); + } + + public static File createTempFile(String extension) { + return getFile().createTempFile(extension); + } + + public static String createTempFileName() { + return getFile().createTempFileName(); + } + + public static String createTempFileName(String extension) { + return getFile().createTempFileName(extension); + } + + public static String decodeSafeFileName(String fileName) { + return getFile().decodeSafeFileName(fileName); + } + + public static boolean delete(String file) { + return getFile().delete(file); + } + + public static boolean delete(File file) { + return getFile().delete(file); + } + + public static void deltree(String directory) { + getFile().deltree(directory); + } + + public static void deltree(File directory) { + getFile().deltree(directory); + } + + public static String encodeSafeFileName(String fileName) { + return getFile().encodeSafeFileName(fileName); + } + + public static boolean exists(String fileName) { + return getFile().exists(fileName); + } + + public static boolean exists(File file) { + return getFile().exists(file); + } + + /** + * Extract text from an input stream and file name. + * + * @param is input stream of file + * @param fileName full name or extension of file (e.g., "Test.doc", + * ".doc") + * @return Extracted text if it is a supported format or an empty string if + * it is an unsupported format + */ + public static String extractText(InputStream is, String fileName) { + return getFile().extractText(is, fileName); + } + + public static String getAbsolutePath(File file) { + return getFile().getAbsolutePath(file); + } + + public static byte[] getBytes(File file) throws IOException { + return getFile().getBytes(file); + } + + public static byte[] getBytes(InputStream is) throws IOException { + return getFile().getBytes(is); + } + + public static byte[] getBytes(InputStream is, int bufferSize) + throws IOException { + + return getFile().getBytes(is); + } + + public static String getExtension(String fileName) { + return getFile().getExtension(fileName); + } + + public static com.liferay.portal.kernel.util.File getFile() { + return _file; + } + + public static String getPath(String fullFileName) { + return getFile().getPath(fullFileName); + } + + public static String getShortFileName(String fullFileName) { + return getFile().getShortFileName(fullFileName); + } + + public static boolean isAscii(File file) throws IOException { + return getFile().isAscii(file); + } + + public static String[] listDirs(String fileName) { + return getFile().listDirs(fileName); + } + + public static String[] listDirs(File file) { + return getFile().listDirs(file); + } + + public static String[] listFiles(String fileName) { + return getFile().listFiles(fileName); + } + + public static String[] listFiles(File file) { + return getFile().listFiles(file); + } + + public static void mkdirs(String pathName) { + getFile().mkdirs(pathName); + } + + public static boolean move( + String sourceFileName, String destinationFileName) { + + return getFile().move(sourceFileName, destinationFileName); + } + + public static boolean move(File source, File destination) { + return getFile().move(source, destination); + } + + public static String read(String fileName) throws IOException { + return getFile().read(fileName); + } + + public static String read(File file) throws IOException { + return getFile().read(file); + } + + public static String read(File file, boolean raw) throws IOException { + return getFile().read(file, raw); + } + + public static String replaceSeparator(String fileName) { + return getFile().replaceSeparator(fileName); + } + + public static File[] sortFiles(File[] files) { + return getFile().sortFiles(files); + } + + public static String stripExtension(String fileName) { + return getFile().stripExtension(fileName); + } + + public static List toList(Reader reader) { + return getFile().toList(reader); + } + + public static List toList(String fileName) { + return getFile().toList(fileName); + } + + public static Properties toProperties(FileInputStream fis) { + return getFile().toProperties(fis); + } + + public static Properties toProperties(String fileName) { + return getFile().toProperties(fileName); + } + + public static void write(String fileName, String s) throws IOException { + getFile().write(fileName, s); + } + + public static void write(String fileName, String s, boolean lazy) + throws IOException { + + getFile().write(fileName, s, lazy); + } + + public static void write( + String fileName, String s, boolean lazy, boolean append) + throws IOException { + + getFile().write(fileName, s, lazy, append); + } + + public static void write(String pathName, String fileName, String s) + throws IOException { + + getFile().write(pathName, fileName, s); + } + + public static void write( + String pathName, String fileName, String s, boolean lazy) + throws IOException { + + getFile().write(pathName, fileName, s, lazy); + } + + public static void write( + String pathName, String fileName, String s, boolean lazy, + boolean append) + throws IOException { + + getFile().write(pathName, fileName, s, lazy, append); + } + + public static void write(File file, String s) throws IOException { + getFile().write(file, s); + } + + public static void write(File file, String s, boolean lazy) + throws IOException { + + getFile().write(file, s, lazy); + } + + public static void write(File file, String s, boolean lazy, boolean append) + throws IOException { + + getFile().write(file, s, lazy, append); + } + + public static void write(String fileName, byte[] bytes) throws IOException { + getFile().write(fileName, bytes); + } + + public static void write(File file, byte[] bytes) throws IOException { + getFile().write(file, bytes); + } + + public static void write(File file, byte[] bytes, int offset, int length) + throws IOException { + + getFile().write(file, bytes, offset, length); + } + + public static void write(String fileName, InputStream is) + throws IOException { + + getFile().write(fileName, is); + } + + public static void write(File file, InputStream is) throws IOException { + getFile().write(file, is); + } + + public void setFile(com.liferay.portal.kernel.util.File file) { + _file = file; + } + + private static com.liferay.portal.kernel.util.File _file; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/GetterUtil.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/GetterUtil.java new file mode 100644 index 000000000..4ec404632 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/GetterUtil.java @@ -0,0 +1,875 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.io.Serializable; + +import java.text.DateFormat; + +import java.util.Date; + +/** + * @author Brian Wing Shun Chan + */ +public class GetterUtil { + + public static String[] BOOLEANS = {"true", "t", "y", "on", "1"}; + + public static final boolean DEFAULT_BOOLEAN = false; + + public static final boolean[] DEFAULT_BOOLEAN_VALUES = new boolean[0]; + + public static final byte DEFAULT_BYTE = 0; + + public static final byte[] DEFAULT_BYTE_VALUES = new byte[0]; + + public static final Date[] DEFAULT_DATE_VALUES = new Date[0]; + + public static final double DEFAULT_DOUBLE = 0.0; + + public static final double[] DEFAULT_DOUBLE_VALUES = new double[0]; + + public static final float DEFAULT_FLOAT = 0; + + public static final float[] DEFAULT_FLOAT_VALUES = new float[0]; + + public static final int DEFAULT_INTEGER = 0; + + public static final int[] DEFAULT_INTEGER_VALUES = new int[0]; + + public static final long DEFAULT_LONG = 0; + + public static final long[] DEFAULT_LONG_VALUES = new long[0]; + + public static final short DEFAULT_SHORT = 0; + + public static final short[] DEFAULT_SHORT_VALUES = new short[0]; + + public static final String DEFAULT_STRING = StringPool.BLANK; + + public static boolean get(Serializable value, boolean defaultValue) { + if (value == null) { + return defaultValue; + } + + if (value instanceof String) { + return get((String)value, defaultValue); + } + else if (value.getClass().isAssignableFrom(Boolean.class)) { + return (Boolean)value; + } + + return defaultValue; + } + + public static Date get( + Serializable value, DateFormat dateFormat, Date defaultValue) { + + if (value == null) { + return defaultValue; + } + + if (value instanceof String) { + return get((String)value, dateFormat, defaultValue); + } + else if (value.getClass().isAssignableFrom(Date.class)) { + return (Date)value; + } + + return defaultValue; + } + + public static double get(Serializable value, double defaultValue) { + if (value == null) { + return defaultValue; + } + + if (value instanceof String) { + return get((String)value, defaultValue); + } + else if (value.getClass().isAssignableFrom(Double.class)) { + return (Double)value; + } + + return defaultValue; + } + + public static float get(Serializable value, float defaultValue) { + if (value == null) { + return defaultValue; + } + + if (value instanceof String) { + return get((String)value, defaultValue); + } + else if (value.getClass().isAssignableFrom(Float.class)) { + return (Float)value; + } + + return defaultValue; + } + + public static int get(Serializable value, int defaultValue) { + if (value == null) { + return defaultValue; + } + + if (value instanceof String) { + return get((String)value, defaultValue); + } + else if (value.getClass().isAssignableFrom(Integer.class)) { + return (Integer)value; + } + + return defaultValue; + } + + public static long get(Serializable value, long defaultValue) { + if (value == null) { + return defaultValue; + } + + if (value instanceof String) { + return get((String)value, defaultValue); + } + else if (value.getClass().isAssignableFrom(Long.class)) { + return (Long)value; + } + + return defaultValue; + } + + public static short get(Serializable value, short defaultValue) { + if (value == null) { + return defaultValue; + } + + if (value instanceof String) { + return get((String)value, defaultValue); + } + else if (value.getClass().isAssignableFrom(Short.class)) { + return (Short)value; + } + + return defaultValue; + } + + public static String get(Serializable value, String defaultValue) { + if (value == null) { + return defaultValue; + } + + if (value instanceof String) { + return get((String)value, defaultValue); + } + + return defaultValue; + } + + public static boolean get(String value, boolean defaultValue) { + if (value == null) { + return defaultValue; + } + + try { + value = value.trim(); + + if (value.equalsIgnoreCase(BOOLEANS[0]) || + value.equalsIgnoreCase(BOOLEANS[1]) || + value.equalsIgnoreCase(BOOLEANS[2]) || + value.equalsIgnoreCase(BOOLEANS[3]) || + value.equalsIgnoreCase(BOOLEANS[4])) { + + return true; + } + else { + return false; + } + } + catch (Exception e) { + } + + return defaultValue; + } + + public static Date get( + String value, DateFormat dateFormat, Date defaultValue) { + + if (value == null) { + return defaultValue; + } + + try { + Date date = dateFormat.parse(value.trim()); + + if (date != null) { + return date; + } + } + catch (Exception e) { + } + + return defaultValue; + } + + public static double get(String value, double defaultValue) { + if (value != null) { + try { + return Double.parseDouble(_trim(value)); + } + catch (Exception e) { + } + } + + return defaultValue; + } + + public static float get(String value, float defaultValue) { + if (value == null) { + return defaultValue; + } + + try { + return Float.parseFloat(_trim(value)); + } + catch (Exception e) { + } + + return defaultValue; + } + + public static int get(String value, int defaultValue) { + if (value == null) { + return defaultValue; + } + + return _parseInt(_trim(value), defaultValue); + } + + public static long get(String value, long defaultValue) { + if (value == null) { + return defaultValue; + } + + return _parseLong(_trim(value), defaultValue); + } + + public static short get(String value, short defaultValue) { + if (value == null) { + return defaultValue; + } + + return _parseShort(_trim(value), defaultValue); + } + + public static String get(String value, String defaultValue) { + if (value == null) { + return defaultValue; + } + + return StringUtil.replace( + value.trim(), StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE); + } + + public static boolean getBoolean(Serializable value) { + return getBoolean(value, DEFAULT_BOOLEAN); + } + + public static boolean getBoolean(Serializable value, boolean defaultValue) { + return get(value, defaultValue); + } + + public static boolean getBoolean(String value) { + return getBoolean(value, DEFAULT_BOOLEAN); + } + + public static boolean getBoolean(String value, boolean defaultValue) { + return get(value, defaultValue); + } + + public static boolean[] getBooleanValues(Serializable value) { + return getBooleanValues(value, DEFAULT_BOOLEAN_VALUES); + } + + public static boolean[] getBooleanValues( + Serializable value, boolean[] defaultValue) { + + Class classObject = value.getClass(); + + if (classObject.isArray()) { + Class componentType = classObject.getComponentType(); + + if (componentType.isAssignableFrom(String.class)) { + return getBooleanValues((String[])value, defaultValue); + } + else if (componentType.isAssignableFrom(Boolean.class)) { + return (boolean[])value; + } + } + + return defaultValue; + } + + public static boolean[] getBooleanValues(String[] values) { + return getBooleanValues(values, DEFAULT_BOOLEAN_VALUES); + } + + public static boolean[] getBooleanValues( + String[] values, boolean[] defaultValue) { + + if (values == null) { + return defaultValue; + } + + boolean[] booleanValues = new boolean[values.length]; + + for (int i = 0; i < values.length; i++) { + booleanValues[i] = getBoolean(values[i]); + } + + return booleanValues; + } + + public static Date getDate(Serializable value, DateFormat dateFormat) { + return getDate(value, dateFormat, new Date()); + } + + public static Date getDate( + Serializable value, DateFormat dateFormat, Date defaultValue) { + + return get(value, dateFormat, defaultValue); + } + + public static Date getDate(String value, DateFormat dateFormat) { + return getDate(value, dateFormat, new Date()); + } + + public static Date getDate( + String value, DateFormat dateFormat, Date defaultValue) { + + return get(value, dateFormat, defaultValue); + } + + public static Date[] getDateValues( + Serializable value, DateFormat dateFormat) { + + return getDateValues(value, dateFormat, DEFAULT_DATE_VALUES); + } + + public static Date[] getDateValues( + Serializable value, DateFormat dateFormat, Date[] defaultValue) { + + Class classObject = value.getClass(); + + if (classObject.isArray()) { + Class componentType = classObject.getComponentType(); + + if (componentType.isAssignableFrom(String.class)) { + return getDateValues((String[])value, dateFormat, defaultValue); + } + else if (componentType.isAssignableFrom(Date.class)) { + return (Date[])value; + } + } + + return defaultValue; + } + + public static Date[] getDateValues(String[] values, DateFormat dateFormat) { + return getDateValues(values, dateFormat, DEFAULT_DATE_VALUES); + } + + public static Date[] getDateValues( + String[] values, DateFormat dateFormat, Date[] defaultValue) { + + if (values == null) { + return defaultValue; + } + + Date[] dateValues = new Date[values.length]; + + for (int i = 0; i < values.length; i++) { + dateValues[i] = getDate(values[i], dateFormat); + } + + return dateValues; + } + + public static double getDouble(Serializable value) { + return getDouble(value, DEFAULT_DOUBLE); + } + + public static double getDouble(Serializable value, double defaultValue) { + return get(value, defaultValue); + } + + public static double getDouble(String value) { + return getDouble(value, DEFAULT_DOUBLE); + } + + public static double getDouble(String value, double defaultValue) { + return get(value, defaultValue); + } + + public static double[] getDoubleValues(Serializable value) { + return getDoubleValues(value, DEFAULT_DOUBLE_VALUES); + } + + public static double[] getDoubleValues( + Serializable value, double[] defaultValue) { + + Class classObject = value.getClass(); + + if (classObject.isArray()) { + Class componentType = classObject.getComponentType(); + + if (componentType.isAssignableFrom(String.class)) { + return getDoubleValues((String[])value, defaultValue); + } + else if (componentType.isAssignableFrom(Double.class)) { + return (double[])value; + } + } + + return defaultValue; + } + + public static double[] getDoubleValues(String[] values) { + return getDoubleValues(values, DEFAULT_DOUBLE_VALUES); + } + + public static double[] getDoubleValues( + String[] values, double[] defaultValue) { + + if (values == null) { + return defaultValue; + } + + double[] doubleValues = new double[values.length]; + + for (int i = 0; i < values.length; i++) { + doubleValues[i] = getDouble(values[i]); + } + + return doubleValues; + } + + public static float getFloat(Serializable value) { + return getFloat(value, DEFAULT_FLOAT); + } + + public static float getFloat(Serializable value, float defaultValue) { + return get(value, defaultValue); + } + + public static float getFloat(String value) { + return getFloat(value, DEFAULT_FLOAT); + } + + public static float getFloat(String value, float defaultValue) { + return get(value, defaultValue); + } + + public static float[] getFloatValues(Serializable value) { + return getFloatValues(value, DEFAULT_FLOAT_VALUES); + } + + public static float[] getFloatValues( + Serializable value, float[] defaultValue) { + + Class classObject = value.getClass(); + + if (classObject.isArray()) { + Class componentType = classObject.getComponentType(); + + if (componentType.isAssignableFrom(String.class)) { + return getFloatValues((String[])value, defaultValue); + } + else if (componentType.isAssignableFrom(Float.class)) { + return (float[])value; + } + } + + return defaultValue; + } + + public static float[] getFloatValues(String[] values) { + return getFloatValues(values, DEFAULT_FLOAT_VALUES); + } + + public static float[] getFloatValues( + String[] values, float[] defaultValue) { + + if (values == null) { + return defaultValue; + } + + float[] floatValues = new float[values.length]; + + for (int i = 0; i < values.length; i++) { + floatValues[i] = getFloat(values[i]); + } + + return floatValues; + } + + public static int getInteger(Serializable value) { + return getInteger(value, DEFAULT_INTEGER); + } + + public static int getInteger(Serializable value, int defaultValue) { + return get(value, defaultValue); + } + + public static int getInteger(String value) { + return getInteger(value, DEFAULT_INTEGER); + } + + public static int getInteger(String value, int defaultValue) { + return get(value, defaultValue); + } + + public static int[] getIntegerValues(Serializable value) { + return getIntegerValues(value, DEFAULT_INTEGER_VALUES); + } + + public static int[] getIntegerValues( + Serializable value, int[] defaultValue) { + + Class classObject = value.getClass(); + + if (classObject.isArray()) { + Class componentType = classObject.getComponentType(); + + if (componentType.isAssignableFrom(String.class)) { + return getIntegerValues((String[])value, defaultValue); + } + else if (componentType.isAssignableFrom(Integer.class)) { + return (int[])value; + } + } + + return defaultValue; + } + + public static int[] getIntegerValues(String[] values) { + return getIntegerValues(values, DEFAULT_INTEGER_VALUES); + } + + public static int[] getIntegerValues(String[] values, int[] defaultValue) { + if (values == null) { + return defaultValue; + } + + int[] intValues = new int[values.length]; + + for (int i = 0; i < values.length; i++) { + intValues[i] = getInteger(values[i]); + } + + return intValues; + } + + public static long getLong(Serializable value) { + return getLong(value, DEFAULT_LONG); + } + + public static long getLong(Serializable value, long defaultValue) { + return get(value, defaultValue); + } + + public static long getLong(String value) { + return getLong(value, DEFAULT_LONG); + } + + public static long getLong(String value, long defaultValue) { + return get(value, defaultValue); + } + + public static long[] getLongValues(Serializable value) { + return getLongValues(value, DEFAULT_LONG_VALUES); + } + + public static long[] getLongValues( + Serializable value, long[] defaultValue) { + + Class classObject = value.getClass(); + + if (classObject.isArray()) { + Class componentType = classObject.getComponentType(); + + if (componentType.isAssignableFrom(String.class)) { + return getLongValues((String[])value, defaultValue); + } + else if (componentType.isAssignableFrom(Long.class)) { + return (long[])value; + } + } + + return defaultValue; + } + + public static long[] getLongValues(String[] values) { + return getLongValues(values, DEFAULT_LONG_VALUES); + } + + public static long[] getLongValues(String[] values, long[] defaultValue) { + if (values == null) { + return defaultValue; + } + + long[] longValues = new long[values.length]; + + for (int i = 0; i < values.length; i++) { + longValues[i] = getLong(values[i]); + } + + return longValues; + } + + public static short getShort(Serializable value) { + return getShort(value, DEFAULT_SHORT); + } + + public static short getShort(Serializable value, short defaultValue) { + return get(value, defaultValue); + } + + public static short getShort(String value) { + return getShort(value, DEFAULT_SHORT); + } + + public static short getShort(String value, short defaultValue) { + return get(value, defaultValue); + } + + public static short[] getShortValues(Serializable value) { + return getShortValues(value, DEFAULT_SHORT_VALUES); + } + + public static short[] getShortValues( + Serializable value, short[] defaultValue) { + + Class classObject = value.getClass(); + + if (classObject.isArray()) { + Class componentType = classObject.getComponentType(); + + if (componentType.isAssignableFrom(String.class)) { + return getShortValues((String[])value, defaultValue); + } + else if (componentType.isAssignableFrom(Short.class)) { + return (short[])value; + } + } + + return defaultValue; + } + + public static short[] getShortValues(String[] values) { + return getShortValues(values, DEFAULT_SHORT_VALUES); + } + + public static short[] getShortValues( + String[] values, short[] defaultValue) { + + if (values == null) { + return defaultValue; + } + + short[] shortValues = new short[values.length]; + + for (int i = 0; i < values.length; i++) { + shortValues[i] = getShort(values[i]); + } + + return shortValues; + } + + public static String getString(Serializable value) { + return getString(value, DEFAULT_STRING); + } + + public static String getString(Serializable value, String defaultValue) { + return get(value, defaultValue); + } + + public static String getString(String value) { + return getString(value, DEFAULT_STRING); + } + + public static String getString(String value, String defaultValue) { + return get(value, defaultValue); + } + + private static int _parseInt(String value, int defaultValue) { + int length = value.length(); + + if (length <= 0) { + return defaultValue; + } + + int pos = 0; + int limit = -Integer.MAX_VALUE; + boolean negative = false; + + char c = value.charAt(0); + + if (c < CharPool.NUMBER_0) { + if (c == CharPool.MINUS) { + limit = Integer.MIN_VALUE; + negative = true; + } + else if (c != CharPool.PLUS) { + return defaultValue; + } + + if (length == 1) { + return defaultValue; + } + + pos++; + } + + int smallLimit = limit / 10; + + int result = 0; + + while (pos < length) { + if (result < smallLimit) { + return defaultValue; + } + + c = value.charAt(pos++); + + if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) { + return defaultValue; + } + + int number = c - CharPool.NUMBER_0; + + result *= 10; + + if (result < (limit + number)) { + return defaultValue; + } + + result -= number; + } + + if (negative) { + return result; + } + else { + return -result; + } + } + + private static long _parseLong(String value, long defaultValue) { + int length = value.length(); + + if (length <= 0) { + return defaultValue; + } + + int pos = 0; + long limit = -Long.MAX_VALUE; + boolean negative = false; + + char c = value.charAt(0); + + if (c < CharPool.NUMBER_0) { + if (c == CharPool.MINUS) { + limit = Long.MIN_VALUE; + negative = true; + } + else if (c != CharPool.PLUS) { + return defaultValue; + } + + if (length == 1) { + return defaultValue; + } + + pos++; + } + + long smallLimit = limit / 10; + + long result = 0; + + while (pos < length) { + if (result < smallLimit) { + return defaultValue; + } + + c = value.charAt(pos++); + + if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) { + return defaultValue; + } + + int number = c - CharPool.NUMBER_0; + + result *= 10; + + if (result < (limit + number)) { + return defaultValue; + } + + result -= number; + } + + if (negative) { + return result; + } + else { + return -result; + } + } + + private static short _parseShort(String value, short defaultValue) { + int i = _parseInt(value, defaultValue); + + if ((i < Short.MIN_VALUE) || (i > Short.MAX_VALUE)) { + return defaultValue; + } + + return (short)i; + } + + private static String _trim(String value) { + value = value.trim(); + + int length = value.length(); + + StringBuilder sb = new StringBuilder(length); + + for (int i = 0; i < length; i++) { + char c = value.charAt(i); + + if ((Character.isDigit(c)) || + ((c == CharPool.DASH) && (i == 0)) || + (c == CharPool.PERIOD) || (c == CharPool.UPPER_CASE_E) || + (c == CharPool.LOWER_CASE_E)) { + + sb.append(c); + } + } + + return sb.toString(); + } + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/JavaProps.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/JavaProps.java new file mode 100644 index 000000000..8e5badabd --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/JavaProps.java @@ -0,0 +1,126 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +// import com.liferay.portal.kernel.log.Log; +// import com.liferay.portal.kernel.log.LogFactoryUtil; +// import com.liferay.portal.kernel.log.LogUtil; + +/** + * @author Brian Wing Shun Chan + */ +public class JavaProps { + + public static final double JAVA_CLASS_VERSION_JDK_4 = 48.0; + + public static final double JAVA_CLASS_VERSION_JDK_5 = 49.0; + + public static final double JAVA_CLASS_VERSION_JDK_6 = 50.0; + + public static final double JAVA_CLASS_VERSION_JDK_7 = 51.0; + + public static String getJavaClassPath() { + return _instance._javaClassPath; + } + + public static double getJavaClassVersion() { + return _instance._javaClassVersion; + } + + public static String getJavaRuntimeVersion() { + return _instance._javaRuntimeVersion; + } + + public static double getJavaSpecificationVersion() { + return _instance._javaSpecificationVersion; + } + + public static String getJavaVersion() { + return _instance._javaVersion; + } + + public static String getJavaVmVersion() { + return _instance._javaVmVersion; + } + + public static boolean isJDK4() { + if (JavaProps.getJavaClassVersion() >= + JavaProps.JAVA_CLASS_VERSION_JDK_4) { + + return true; + } + else { + return false; + } + } + + public static boolean isJDK5() { + if (JavaProps.getJavaClassVersion() >= + JavaProps.JAVA_CLASS_VERSION_JDK_5) { + + return true; + } + else { + return false; + } + } + + public static boolean isJDK6() { + if (JavaProps.getJavaClassVersion() >= + JavaProps.JAVA_CLASS_VERSION_JDK_6) { + + return true; + } + else { + return false; + } + } + + public static boolean isJDK7() { + if (JavaProps.getJavaClassVersion() >= + JavaProps.JAVA_CLASS_VERSION_JDK_7) { + + return true; + } + else { + return false; + } + } + + private JavaProps() { + _javaClassPath = System.getProperty("java.class.path"); + _javaClassVersion = Double.parseDouble(System.getProperty( + "java.class.version")); + _javaRuntimeVersion = System.getProperty("java.runtime.version"); + _javaSpecificationVersion = Double.parseDouble(System.getProperty( + "java.specification.version")); + _javaVersion = System.getProperty("java.version"); + _javaVmVersion = System.getProperty("java.vm.version"); + +// LogUtil.debug(_log, System.getProperties()); + } + +// private static Log _log = LogFactoryUtil.getLog(JavaProps.class); + + private static JavaProps _instance = new JavaProps(); + + private String _javaClassPath; + private double _javaClassVersion; + private String _javaRuntimeVersion; + private double _javaSpecificationVersion; + private String _javaVersion; + private String _javaVmVersion; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/KeyValuePair.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/KeyValuePair.java new file mode 100644 index 000000000..71934272a --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/KeyValuePair.java @@ -0,0 +1,77 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.io.Serializable; + +/** + * @author Brian Wing Shun Chan + */ +public class KeyValuePair implements Comparable, Serializable { + + public KeyValuePair() { + this(null, null); + } + + public KeyValuePair(String key, String value) { + _key = key; + _value = value; + } + + public String getKey() { + return _key; + } + + public void setKey(String key) { + _key = key; + } + + public String getValue() { + return _value; + } + + public void setValue(String value) { + _value = value; + } + + public int compareTo(KeyValuePair kvp) { + return _key.compareTo(kvp.getKey()); + } + + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + + KeyValuePair kvp = (KeyValuePair)obj; + + String key = kvp.getKey(); + + if (_key.equals(key)) { + return true; + } + else { + return false; + } + } + + public int hashCode() { + return _key.hashCode(); + } + + private String _key; + private String _value; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/ListWrapper.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/ListWrapper.java new file mode 100644 index 000000000..040157c94 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/ListWrapper.java @@ -0,0 +1,125 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +/** + * @author Brian Wing Shun Chan + */ +public class ListWrapper implements List { + + public ListWrapper(List list) { + _list = list; + } + + public boolean add(E o) { + return _list.add(o); + } + + public void add(int index, E element) { + _list.add(index, element); + } + + public boolean addAll(Collection c) { + return _list.addAll(c); + } + + public boolean addAll(int index, Collection c) { + return _list.addAll(index, c); + } + + public void clear() { + _list.clear(); + } + + public boolean contains(Object o) { + return _list.contains(o); + } + + public boolean containsAll(Collection c) { + return _list.containsAll(c); + } + + public E get(int index) { + return _list.get(index); + } + + public int indexOf(Object o) { + return _list.indexOf(o); + } + + public boolean isEmpty() { + return _list.isEmpty(); + } + + public Iterator iterator() { + return _list.iterator(); + } + + public int lastIndexOf(Object o) { + return _list.lastIndexOf(o); + } + + public ListIterator listIterator() { + return _list.listIterator(); + } + + public ListIterator listIterator(int index) { + return _list.listIterator(index); + } + + public boolean remove(Object o) { + return _list.remove(o); + } + + public E remove(int index) { + return _list.remove(index); + } + + public boolean removeAll(Collection c) { + return _list.removeAll(c); + } + + public boolean retainAll(Collection c) { + return _list.retainAll(c); + } + + public E set(int index, E element) { + return _list.set(index, element); + } + + public int size() { + return _list.size(); + } + + public List subList(int fromIndex, int toIndex) { + return _list.subList(fromIndex, toIndex); + } + + public Object[] toArray() { + return _list.toArray(); + } + + public T[] toArray(T[] a) { + return _list.toArray(a); + } + + private List _list; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodCache.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodCache.java new file mode 100644 index 000000000..9060eb28a --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodCache.java @@ -0,0 +1,124 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.lang.reflect.Method; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Michael C. Han + */ +public class MethodCache { + + public static Method get(String className, String methodName) + throws ClassNotFoundException, NoSuchMethodException { + + return get(null, null, className, methodName); + } + + public static Method get( + String className, String methodName, Class[] parameterTypes) + throws ClassNotFoundException, NoSuchMethodException { + + return get(null, null, className, methodName, parameterTypes); + } + + public static Method get( + Map> classesMap, Map methodsMap, + String className, String methodName) + throws ClassNotFoundException, NoSuchMethodException { + + return get(className, methodName, new Class[0]); + } + + public static Method get( + Map> classesMap, Map methodsMap, + String className, String methodName, Class[] parameterTypes) + throws ClassNotFoundException, NoSuchMethodException { + + MethodKey methodKey = new MethodKey( + className, methodName, parameterTypes); + + return _instance._get(classesMap, methodsMap, methodKey); + } + + public static Method get(MethodKey methodKey) + throws ClassNotFoundException, NoSuchMethodException { + + return _instance._get(null, null, methodKey); + } + + public static Method put(MethodKey methodKey, Method method) { + return _instance._put(methodKey, method); + } + + private MethodCache() { + _classesMap = new HashMap>(); + _methodsMap = new HashMap(); + } + + private Method _get( + Map> classesMap, Map methodsMap, + MethodKey methodKey) + throws ClassNotFoundException, NoSuchMethodException { + + if (classesMap == null) { + classesMap = _classesMap; + } + + if (methodsMap == null) { + methodsMap = _methodsMap; + } + + Method method = methodsMap.get(methodKey); + + if (method == null) { + String className = methodKey.getClassName(); + String methodName = methodKey.getMethodName(); + Class[] parameterTypes = methodKey.getParameterTypes(); + + Class classObj = classesMap.get(className); + + if (classObj == null) { + Thread currentThread = Thread.currentThread(); + + ClassLoader contextClassLoader = + currentThread.getContextClassLoader(); + + classObj = contextClassLoader.loadClass(className); + + classesMap.put(className, classObj); + } + + method = classObj.getMethod(methodName, parameterTypes); + + methodsMap.put(methodKey, method); + } + + return method; + } + + private Method _put(MethodKey methodKey, Method method) { + return _methodsMap.put(methodKey, method); + } + + private static MethodCache _instance = new MethodCache(); + + private Map> _classesMap; + private Map _methodsMap; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodHandler.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodHandler.java new file mode 100644 index 000000000..dbd144355 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodHandler.java @@ -0,0 +1,99 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.io.Serializable; + +import java.lang.reflect.Method; + +import java.util.Arrays; + +/** + * @author Shuyang Zhou + */ +public class MethodHandler implements Serializable { + + public MethodHandler(Method method, Object... arguments) { + this(new MethodKey(method), arguments); + } + + public MethodHandler(MethodKey methodKey, Object... arguments) { + _methodKey = methodKey; + _arguments = arguments; + } + + public Object[] getArguments() { + Object[] arguments = new Object[_arguments.length]; + + System.arraycopy(_arguments, 0, arguments, 0, _arguments.length); + + return arguments; + } + + public Class[] getArgumentsClasses() { + return _methodKey.getParameterTypes(); + } + + public String getClassName() { + return _methodKey.getClassName(); + } + + public MethodKey getMethodKey() { + return _methodKey; + } + + public String getMethodName() { + return _methodKey.getMethodName(); + } + + public Object invoke(boolean newInstance) throws Exception { + Thread currentThread = Thread.currentThread(); + + ClassLoader contextClassLoader = currentThread.getContextClassLoader(); + + Object targetObject = null; + + if (newInstance) { + Class targetClass = contextClassLoader.loadClass( + getClassName()); + + targetObject = targetClass.newInstance(); + } + + return invoke(targetObject); + } + + public Object invoke(Object target) throws Exception { + Method method = MethodCache.get(_methodKey); + + return method.invoke(target, _arguments); + } + + public String toString() { + StringBundler sb = new StringBundler(5); + + sb.append("{arguments="); + sb.append(Arrays.toString(_arguments)); + sb.append(", methodKey="); + sb.append(_methodKey); + sb.append("}"); + + return sb.toString(); + } + + private Object[] _arguments; + private MethodKey _methodKey; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodInvoker.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodInvoker.java new file mode 100644 index 000000000..3a0dcc08a --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodInvoker.java @@ -0,0 +1,261 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; + +import java.lang.reflect.Array; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author Brian Wing Shun Chan + * @author Harry Mark + * @author Shuyang Zhou + * @deprecated + */ +public class MethodInvoker { + + public static Object invoke(MethodWrapper methodWrapper) + throws ClassNotFoundException, IllegalAccessException, + InstantiationException, InvocationTargetException, + NoSuchFieldException, NoSuchMethodException { + + return invoke(methodWrapper, true); + } + + public static Object invoke( + MethodWrapper methodWrapper, boolean newInstance) + throws ClassNotFoundException, IllegalAccessException, + InstantiationException, InvocationTargetException, + NoSuchFieldException, NoSuchMethodException { + + Object targetObject = null; + + if (newInstance) { + Thread currentThread = Thread.currentThread(); + + ClassLoader contextClassLoader = + currentThread.getContextClassLoader(); + + targetObject = contextClassLoader.loadClass( + methodWrapper.getClassName()).newInstance(); + } + + Object[] methodAndArguments = _lookupMethodAndArguments( + methodWrapper, targetObject); + + Object returnObject = null; + + if (methodAndArguments[0] != null) { + Method method = (Method)methodAndArguments[0]; + Object[] arguments = (Object[])methodAndArguments[1]; + + returnObject = method.invoke(targetObject, arguments); + } + + return returnObject; + } + + public static Object invoke( + MethodWrapper methodWrapper, Object targetObject) + throws ClassNotFoundException, IllegalAccessException, + InvocationTargetException, NoSuchFieldException, + NoSuchMethodException { + + Object[] methodAndArguments = _lookupMethodAndArguments( + methodWrapper, targetObject); + + Object returnObject = null; + + if (methodAndArguments[0] != null) { + Method method = (Method)methodAndArguments[0]; + Object[] arguments = (Object[])methodAndArguments[1]; + + returnObject = method.invoke(targetObject, arguments); + } + + return returnObject; + } + + private static Object[] _lookupMethodAndArguments( + MethodWrapper methodWrapper, Object targetObject) + throws ClassNotFoundException, IllegalAccessException, + InvocationTargetException, NoSuchFieldException, + NoSuchMethodException { + + Object[] methodAndArguments = new Object[2]; + + Thread currentThread = Thread.currentThread(); + + ClassLoader contextClassLoader = currentThread.getContextClassLoader(); + + String className = methodWrapper.getClassName(); + String methodName = methodWrapper.getMethodName(); + Object[] arguments = methodWrapper.getArguments(); + String[] argumentClassNames = methodWrapper.getArgumentClassNames(); + + List> parameterTypes = new ArrayList>(); + + for (int i = 0; i < arguments.length; i++) { + if (arguments[i] == null) { + _log.error( + "Cannot invoke " + className + " " + methodName + + " on position " + i + " because it is null"); + } + + Class argClass = null; + + if (argumentClassNames != null) { + argClass = _primitiveTypeMap.get(argumentClassNames[i]); + + if (argClass == null) { + argClass = Class.forName( + argumentClassNames[i], true, contextClassLoader); + } + } + else { + argClass = arguments[i].getClass(); + } + + if (ClassUtil.isSubclass(argClass, PrimitiveWrapper.class)) { + parameterTypes.add( + (Class)argClass.getField("TYPE").get(arguments[i])); + + MethodKey methodKey = new MethodKey( + argClass.getName(), "getValue"); + + Method method = MethodCache.get(methodKey); + + arguments[i] = method.invoke(arguments[i], (Object[])null); + } + else if (arguments[i] instanceof NullWrapper) { + NullWrapper nullWrapper = (NullWrapper)arguments[i]; + + String wrappedClassName = nullWrapper.getClassName(); + + if (wrappedClassName.startsWith(StringPool.OPEN_BRACKET) && + wrappedClassName.endsWith(StringPool.SEMICOLON)) { + + wrappedClassName = wrappedClassName.substring( + 2, wrappedClassName.length() - 1); + + Class wrappedClass = contextClassLoader.loadClass( + wrappedClassName); + + parameterTypes.add( + Array.newInstance(wrappedClass, 0).getClass()); + } + else { + Class wrappedClass = contextClassLoader.loadClass( + wrappedClassName); + + parameterTypes.add(wrappedClass); + } + + arguments[i] = null; + } + else { + parameterTypes.add(argClass); + } + } + + MethodKey methodKey = null; + + Method method = null; + + try { + methodKey = new MethodKey( + methodWrapper.getClassName(), methodWrapper.getMethodName(), + parameterTypes.toArray(new Class[parameterTypes.size()])); + + method = MethodCache.get(methodKey); + } + catch (NoSuchMethodException nsme) { + Class classObject = null; + + if (targetObject == null) { + classObject = contextClassLoader.loadClass(className); + } + else { + classObject = targetObject.getClass(); + } + + Method[] methods = classObject.getMethods(); + + for (int i = 0; i < methods.length; i++) { + Class[] methodParameterTypes = + methods[i].getParameterTypes(); + + if (methods[i].getName().equals(methodName) && + methodParameterTypes.length == parameterTypes.size()) { + + boolean correctParams = true; + + for (int j = 0; j < parameterTypes.size(); j++) { + Class a = parameterTypes.get(j); + Class b = methodParameterTypes[j]; + + if (!ClassUtil.isSubclass(a, b)) { + correctParams = false; + + break; + } + } + + if (correctParams) { + method = methods[i]; + + MethodCache.put(methodKey, method); + + break; + } + } + } + + if (method == null) { + throw nsme; + } + } + + methodAndArguments[0] = method; + methodAndArguments[1] = arguments; + + return methodAndArguments; + } + + private static Log _log = LogFactoryUtil.getLog(MethodInvoker.class); + + private static Map> _primitiveTypeMap = + new HashMap>(); + + static { + _primitiveTypeMap.put("char", char.class); + _primitiveTypeMap.put("boolean", boolean.class); + _primitiveTypeMap.put("byte", byte.class); + _primitiveTypeMap.put("double", double.class); + _primitiveTypeMap.put("float", float.class); + _primitiveTypeMap.put("int", int.class); + _primitiveTypeMap.put("long", long.class); + _primitiveTypeMap.put("short", short.class); + } + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodKey.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodKey.java new file mode 100644 index 000000000..cf16a6438 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodKey.java @@ -0,0 +1,118 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.io.Serializable; + +import java.lang.reflect.Method; + +/** + * @author Brian Wing Shun Chan + * @author Shuyang Zhou + */ +public class MethodKey implements Serializable { + + public MethodKey(Method method) { + this( + method.getDeclaringClass().getName(), method.getName(), + method.getParameterTypes()); + } + + public MethodKey( + String className, String methodName, Class... parameterTypes) { + + _className = className; + _methodName = methodName; + _parameterTypes = parameterTypes; + } + + public MethodKey( + String className, String methodName, String[] parameterTypeNames) + throws ClassNotFoundException { + + _className = className; + _methodName = methodName; + + _parameterTypes = new Class[parameterTypeNames.length]; + + for (int i = 0; i < parameterTypeNames.length; i++) { + String parameterTypeName = parameterTypeNames[i]; + + _parameterTypes[i] = Class.forName(parameterTypeName); + } + } + + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + + MethodKey methodKey = (MethodKey)obj; + + if (toString().equals(methodKey.toString())) { + return true; + } + else { + return false; + } + } + + public String getClassName() { + return _className; + } + + public String getMethodName() { + return _methodName; + } + + public Class[] getParameterTypes() { + return _parameterTypes; + } + + public int hashCode() { + return toString().hashCode(); + } + + public String toString() { + return _toString(); + } + + private String _toString() { + if (_toString == null) { + StringBundler sb = new StringBundler(); + + sb.append(_className); + sb.append(_methodName); + + if ((_parameterTypes != null) && (_parameterTypes.length > 0)) { + sb.append(StringPool.DASH); + + for (Class parameterType : _parameterTypes) { + sb.append(parameterType.getName()); + } + } + + _toString = sb.toString(); + } + + return _toString; + } + + private String _className; + private String _methodName; + private Class[] _parameterTypes; + private String _toString; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodWrapper.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodWrapper.java new file mode 100644 index 000000000..2a4be0b63 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/MethodWrapper.java @@ -0,0 +1,109 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.io.Serializable; + +import java.lang.reflect.Method; + +import java.util.Arrays; + +/** + * @author Brian Wing Shun Chan + * @deprecated + */ +public class MethodWrapper implements Serializable { + + public MethodWrapper(String className, String methodName) { + this(className, methodName, new Object[0]); + } + + public MethodWrapper(String className, String methodName, Object argument) { + this(className, methodName, new Object[] {argument}); + } + + public MethodWrapper( + String className, String methodName, Object[] arguments) { + + _className = className; + _methodName = methodName; + _arguments = arguments; + } + + public MethodWrapper(Method method, Object[] arguments) { + this(method.getDeclaringClass().getName(), method.getName(), arguments); + + _argumentClassNames = new String[arguments.length]; + + Class[] parameterTypes = method.getParameterTypes(); + + for (int i = 0; i < parameterTypes.length; i++) { + _argumentClassNames[i] = parameterTypes[i].getName(); + } + } + + public String getClassName() { + return _className; + } + + public String getMethodName() { + return _methodName; + } + + /** + * @deprecated Use getArguments. + */ + public Object[] getArgs() { + return getArguments(); + } + + public String[] getArgumentClassNames() { + return _argumentClassNames; + } + + public Object[] getArguments() { + Object[] arguments = new Object[_arguments.length]; + + System.arraycopy(_arguments, 0, arguments, 0, _arguments.length); + + return arguments; + } + + public String toString() { + StringBundler sb = new StringBundler(9); + + sb.append("{className="); + sb.append(_className); + sb.append(", methodName="); + sb.append(_methodName); + + if (_argumentClassNames != null) { + sb.append(", argumentClassNames="); + sb.append(Arrays.toString(_argumentClassNames)); + } + + sb.append(", arguments="); + sb.append(Arrays.toString(_arguments)); + sb.append("}"); + + return sb.toString(); + } + + private String _className; + private String _methodName; + private String[] _argumentClassNames; + private Object[] _arguments; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/NullWrapper.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/NullWrapper.java new file mode 100644 index 000000000..5c883126c --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/NullWrapper.java @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.io.Serializable; + +/** + * @author Brian Wing Shun Chan + */ +public class NullWrapper implements Serializable { + + public NullWrapper(String className) { + _className = className; + } + + public String getClassName() { + return _className; + } + + private String _className; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/PortalClassInvoker.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/PortalClassInvoker.java new file mode 100644 index 000000000..bdbbe5f95 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/PortalClassInvoker.java @@ -0,0 +1,229 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.lang.reflect.InvocationTargetException; + +/** + * @author Brian Wing Shun Chan + * @author Shuyang Zhou + */ +public class PortalClassInvoker { + + public static Object invoke( + boolean newInstance, MethodKey methodKey, Object... arguments) + throws Exception { + + Thread currentThread = Thread.currentThread(); + + ClassLoader contextClassLoader = currentThread.getContextClassLoader(); + + try { + currentThread.setContextClassLoader( + PortalClassLoaderUtil.getClassLoader()); + + MethodHandler methodHandler = new MethodHandler( + methodKey, arguments); + + return methodHandler.invoke(newInstance); + } + catch (InvocationTargetException ite) { + throw (Exception)ite.getCause(); + } + finally { + currentThread.setContextClassLoader(contextClassLoader); + } + } + + public static Object invoke( + boolean newInstance, String className, String methodName, + String[] parameterTypeNames, Object... arguments) + throws Exception { + + Thread currentThread = Thread.currentThread(); + + ClassLoader contextClassLoader = currentThread.getContextClassLoader(); + + try { + currentThread.setContextClassLoader( + PortalClassLoaderUtil.getClassLoader()); + + MethodKey methodKey = new MethodKey( + className, methodName, parameterTypeNames); + + MethodHandler methodHandler = new MethodHandler( + methodKey, arguments); + + return methodHandler.invoke(newInstance); + } + catch (InvocationTargetException ite) { + throw (Exception)ite.getCause(); + } + finally { + currentThread.setContextClassLoader(contextClassLoader); + } + } + + /** + * @deprecated + */ + public static Object invoke(String className, String methodName) + throws Exception { + + return invoke(className, methodName, new Object[] {}); + } + + /** + * @deprecated + */ + public static Object invoke(String className, String methodName, Object arg) + throws Exception { + + return invoke(className, methodName, new Object[] {arg}); + } + + /** + * @deprecated + */ + public static Object invoke( + String className, String methodName, Object arg1, Object arg2) + throws Exception { + + return invoke(className, methodName, new Object[] {arg1, arg2}); + } + + /** + * @deprecated + */ + public static Object invoke( + String className, String methodName, Object arg1, Object arg2, + Object arg3) + throws Exception { + + return invoke(className, methodName, new Object[] {arg1, arg2, arg3}); + } + + /** + * @deprecated + */ + public static Object invoke( + String className, String methodName, Object[] args) + throws Exception { + + return invoke(className, methodName, args, true); + } + + /** + * @deprecated + */ + public static Object invoke( + String className, String methodName, boolean newInstance) + throws Exception { + + return invoke(className, methodName, new Object[] {}, newInstance); + } + + /** + * @deprecated + */ + public static Object invoke( + String className, String methodName, Object arg, + boolean newInstance) + throws Exception { + + return invoke(className, methodName, new Object[] {arg}, newInstance); + } + + /** + * @deprecated + */ + public static Object invoke( + String className, String methodName, Object arg1, Object arg2, + boolean newInstance) + throws Exception { + + return invoke( + className, methodName, new Object[] {arg1, arg2}, newInstance); + } + + /** + * @deprecated + */ + public static Object invoke( + String className, String methodName, Object arg1, Object arg2, + Object arg3, boolean newInstance) + throws Exception { + + return invoke( + className, methodName, new Object[] {arg1, arg2, arg3}, + newInstance); + } + + /** + * @deprecated + */ + public static Object invoke( + String className, String methodName, Object arg1, Object arg2, + Object arg3, Object arg4, boolean newInstance) + throws Exception { + + return invoke( + className, methodName, new Object[] {arg1, arg2, arg3, arg4}, + newInstance); + } + + /** + * @deprecated + */ + public static Object invoke( + String className, String methodName, Object arg1, Object arg2, + Object arg3, Object arg4, Object arg5, boolean newInstance) + throws Exception { + + return invoke( + className, methodName, new Object[] {arg1, arg2, arg3, arg4, arg5}, + newInstance); + } + + /** + * @deprecated + */ + public static Object invoke( + String className, String methodName, Object[] args, + boolean newInstance) + throws Exception { + + Thread currentThread = Thread.currentThread(); + + ClassLoader contextClassLoader = currentThread.getContextClassLoader(); + + try { + currentThread.setContextClassLoader( + PortalClassLoaderUtil.getClassLoader()); + + MethodWrapper methodWrapper = new MethodWrapper( + className, methodName, args); + + return MethodInvoker.invoke(methodWrapper, newInstance); + } + catch (InvocationTargetException ite) { + throw (Exception)ite.getCause(); + } + finally { + currentThread.setContextClassLoader(contextClassLoader); + } + } + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/PortalClassLoaderUtil.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/PortalClassLoaderUtil.java new file mode 100644 index 000000000..84bc08a8f --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/PortalClassLoaderUtil.java @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.net.URL; +import java.net.URLClassLoader; + +/** + * @author Brian Wing Shun Chan + */ +public class PortalClassLoaderUtil { + + public static ClassLoader getClassLoader() { + return _classLoader; + } + + public static void setClassLoader(ClassLoader contextClassLoader) { + if (ServerDetector.isJOnAS() && JavaProps.isJDK6()) { + _classLoader = new URLClassLoader(new URL[0], contextClassLoader); + } + else { + _classLoader = contextClassLoader; + } + } + + private static ClassLoader _classLoader; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/PrimitiveWrapper.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/PrimitiveWrapper.java new file mode 100644 index 000000000..7a2d28b74 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/PrimitiveWrapper.java @@ -0,0 +1,23 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.io.Serializable; + +/** + * @author Brian Wing Shun Chan + */ +public class PrimitiveWrapper implements Serializable { +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/Randomizer.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/Randomizer.java new file mode 100644 index 000000000..1e9ab75ad --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/Randomizer.java @@ -0,0 +1,141 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; + +/** + * @author Brian Wing Shun Chan + */ +public class Randomizer extends Random { + + public static Randomizer getInstance() { + return _instance; + } + + public Randomizer() { + super(); + } + + public Randomizer(long seed) { + super(seed); + } + + public int[] nextInt(int n, int size) { + if (size > n) { + size = n; + } + + Set set = new LinkedHashSet(); + + for (int i = 0; i < size; i++) { + while (true) { + Integer value = new Integer(nextInt(n)); + + if (!set.contains(value)) { + set.add(value); + + break; + } + } + } + + int[] array = new int[set.size()]; + + Iterator itr = set.iterator(); + + for (int i = 0; i < array.length; i++) { + array[i] = itr.next().intValue(); + } + + return array; + } + + public void randomize(char array[]) { + int length = array.length; + + for (int i = 0; i < length - 1; i++) { + int x = nextInt(length); + char y = array[i]; + + array[i] = array[i + x]; + array[i + x] = y; + + length--; + } + } + + public void randomize(int array[]) { + int length = array.length; + + for (int i = 0; i < length - 1; i++) { + int x = nextInt(length); + int y = array[i]; + + array[i] = array[i + x]; + array[i + x] = y; + + length--; + } + } + + public void randomize(List list) { + int size = list.size(); + + for (int i = 0; i <= size; i++) { + Object obj = list.get(i); + + int j = nextInt(size); + + list.set(i, list.get(i + j)); + list.set(i + j, obj); + + size--; + } + } + + public void randomize(Object array[]) { + int length = array.length; + + for (int i = 0; i < length - 1; i++) { + int x = nextInt(length); + Object y = array[i]; + + array[i] = array[i + x]; + array[i + x] = y; + + length--; + } + } + + public String randomize(String s) { + if (s == null) { + return null; + } + + char[] array = s.toCharArray(); + + randomize(array); + + return new String(array); + } + + private static Randomizer _instance = new Randomizer(); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/ServerDetector.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/ServerDetector.java new file mode 100644 index 000000000..742ee6132 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/ServerDetector.java @@ -0,0 +1,256 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; + +/** + * @author Brian Wing Shun Chan + */ +public class ServerDetector { + + public static final String GERONIMO_ID = "geronimo"; + + public static final String GLASSFISH_ID = "glassfish"; + + public static final String JBOSS_ID = "jboss"; + + public static final String JETTY_ID = "jetty"; + + public static final String JONAS_ID = "jonas"; + + public static final String OC4J_ID = "oc4j"; + + public static final String RESIN_ID = "resin"; + + public static final String TOMCAT_ID = "tomcat"; + + public static final String WEBLOGIC_ID = "weblogic"; + + public static final String WEBSPHERE_ID = "websphere"; + + public static String getServerId() { + return _instance._serverId; + } + + public static boolean isGeronimo() { + return _instance._geronimo; + } + + public static boolean isGlassfish() { + return _instance._glassfish; + } + + public static boolean isJBoss() { + return _instance._jBoss; + } + + public static boolean isJetty() { + return _instance._jetty; + } + + public static boolean isJOnAS() { + return _instance._jonas; + } + + public static boolean isOC4J() { + return _instance._oc4j; + } + + public static boolean isResin() { + return _instance._resin; + } + + public static boolean isSupportsComet() { + return _instance._supportsComet; + } + + public static boolean isTomcat() { + return _instance._tomcat; + } + + public static boolean isWebLogic() { + return _instance._webLogic; + } + + public static boolean isWebSphere() { + return _instance._webSphere; + } + + private ServerDetector() { + if (_isGeronimo()) { + _serverId = GERONIMO_ID; + _geronimo = true; + } + else if (_isGlassfish()) { + _serverId = GLASSFISH_ID; + _glassfish = true; + } + else if (_isJBoss()) { + _serverId = JBOSS_ID; + _jBoss = true; + } + else if (_isJOnAS()) { + _serverId = JONAS_ID; + _jonas = true; + } + else if (_isOC4J()) { + _serverId = OC4J_ID; + _oc4j = true; + } + else if (_isResin()) { + _serverId = RESIN_ID; + _resin = true; + } + else if (_isWebLogic()) { + _serverId = WEBLOGIC_ID; + _webLogic = true; + } + else if (_isWebSphere()) { + _serverId = WEBSPHERE_ID; + _webSphere = true; + } + + if (_isJetty()) { + if (_serverId == null) { + _serverId = JETTY_ID; + _jetty = true; + } + } + else if (_isTomcat()) { + if (_serverId == null) { + _serverId = TOMCAT_ID; + _tomcat = true; + } + } + + if (System.getProperty("external-properties") == null) { + if (_log.isInfoEnabled()) { + if (_serverId != null) { + _log.info("Detected server " + _serverId); + } + else { + _log.info("No server detected"); + } + } + } + + /*if (_serverId == null) { + throw new RuntimeException("Server is not supported"); + }*/ + } + + private boolean _detect(String className) { + try { + ClassLoader systemClassLoader = + ClassLoader.getSystemClassLoader(); + + systemClassLoader.loadClass(className); + + return true; + } + catch (ClassNotFoundException cnfe) { + Class classObj = getClass(); + + if (classObj.getResource(className) != null) { + return true; + } + else { + return false; + } + } + } + + private boolean _isGeronimo() { + return _detect( + "/org/apache/geronimo/system/main/Daemon.class"); + } + + private boolean _isGlassfish() { + String value = System.getProperty("com.sun.aas.instanceRoot"); + + if (value != null) { + return true; + } + else { + return false; + } + } + + private boolean _isJBoss() { + return _detect("/org/jboss/Main.class"); + } + + private boolean _isJetty() { + return _detect("/org/mortbay/jetty/Server.class"); + } + + private boolean _isJOnAS() { + boolean jonas = _detect("/org/objectweb/jonas/server/Server.class"); + + if (!_jonas && (System.getProperty("jonas.root") != null)) { + jonas = true; + } + + return jonas; + } + + private boolean _isOC4J() { + return _detect("oracle.oc4j.util.ClassUtils"); + } + + private boolean _isResin() { + return _detect("/com/caucho/server/resin/Resin.class"); + } + + private boolean _isTomcat() { + boolean tomcat = _detect( + "/org/apache/catalina/startup/Bootstrap.class"); + + if (!tomcat) { + tomcat = _detect("/org/apache/catalina/startup/Embedded.class"); + } + + return tomcat; + } + + private boolean _isWebLogic() { + return _detect("/weblogic/Server.class"); + } + + private boolean _isWebSphere() { + return _detect( + "/com/ibm/websphere/product/VersionInfo.class"); + } + + private static Log _log = LogFactoryUtil.getLog(ServerDetector.class); + + private static ServerDetector _instance = new ServerDetector(); + + private String _serverId; + private boolean _geronimo; + private boolean _glassfish; + private boolean _jBoss; + private boolean _jetty; + private boolean _jonas; + private boolean _oc4j; + private boolean _resin; + private boolean _supportsComet; + private boolean _tomcat; + private boolean _webLogic; + private boolean _webSphere; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/StackTraceUtil.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/StackTraceUtil.java new file mode 100644 index 000000000..530826884 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/StackTraceUtil.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter; +import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter; + +import java.io.PrintWriter; + +/** + * @author Brian Wing Shun Chan + */ +public class StackTraceUtil { + + public static String getStackTrace(Throwable t) { + String stackTrace = null; + + PrintWriter printWriter = null; + + try { + UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(); + + printWriter = new UnsyncPrintWriter(unsyncStringWriter); + + t.printStackTrace(printWriter); + + printWriter.flush(); + + stackTrace = unsyncStringWriter.toString(); + } + finally { + if (printWriter != null) { + printWriter.flush(); + printWriter.close(); + } + } + + return stackTrace; + } + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/StringBundler.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/StringBundler.java new file mode 100644 index 000000000..5b2b70b4c --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/StringBundler.java @@ -0,0 +1,337 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; + +import java.io.IOException; +import java.io.Writer; + +import java.lang.reflect.Constructor; + +/** + *

+ * See http://issues.liferay.com/browse/LPS-6072. + *

+ * + * @author Shuyang Zhou + * @author Brian Wing Shun Chan + */ +public class StringBundler { + + public static final int UNSAFE_CREATE_THRESHOLD = GetterUtil.getInteger( + System.getProperty( + StringBundler.class.getName() + ".unsafe.create.threshold")); + + public StringBundler() { + _array = new String[_DEFAULT_ARRAY_CAPACITY]; + } + + public StringBundler(int initialCapacity) { + if (initialCapacity <= 0) { + throw new IllegalArgumentException(); + } + + _array = new String[initialCapacity]; + } + + public StringBundler(String s) { + _array = new String[_DEFAULT_ARRAY_CAPACITY]; + + _array[0] = s; + + _arrayIndex = 1; + } + + public StringBundler(String[] stringArray) { + this(stringArray, 0); + } + + public StringBundler(String[] stringArray, int extraSpace) { + _array = new String[stringArray.length + extraSpace]; + + for (int i = 0; i < stringArray.length; i++) { + String s = stringArray[i]; + + if ((s != null) && (s.length() > 0)) { + _array[_arrayIndex++] = s; + } + } + } + + public StringBundler append(boolean b) { + if (b) { + return append(_TRUE); + } + else { + return append(_FALSE); + } + } + + public StringBundler append(char c) { + return append(String.valueOf(c)); + } + + public StringBundler append(char[] charArray) { + if (charArray == null) { + return append("null"); + } + else { + return append(new String(charArray)); + } + } + + public StringBundler append(double d) { + return append(Double.toString(d)); + } + + public StringBundler append(float f) { + return append(Float.toString(f)); + } + + public StringBundler append(int i) { + return append(Integer.toString(i)); + } + + public StringBundler append(long l) { + return append(Long.toString(l)); + } + + public StringBundler append(Object obj) { + return append(String.valueOf(obj)); + } + + public StringBundler append(String s) { + if (s == null) { + s = StringPool.NULL; + } + + if (s.length() == 0) { + return this; + } + + if (_arrayIndex >= _array.length) { + expandCapacity(_array.length * 2); + } + + _array[_arrayIndex++] = s; + + return this; + } + + public StringBundler append(String[] stringArray) { + if ((stringArray == null) || (stringArray.length == 0)) { + return this; + } + + if ((_array.length - _arrayIndex) < stringArray.length) { + expandCapacity((_array.length + stringArray.length) * 2); + } + + for (int i = 0; i < stringArray.length; i++) { + String s = stringArray[i]; + + if ((s != null) && (s.length() > 0)) { + _array[_arrayIndex++] = s; + } + } + + return this; + } + + public StringBundler append(StringBundler sb) { + if ((sb == null) || (sb._arrayIndex == 0)) { + return this; + } + + if ((_array.length - _arrayIndex) < sb._arrayIndex) { + expandCapacity((_array.length + sb._arrayIndex) * 2); + } + + System.arraycopy(sb._array, 0, _array, _arrayIndex, sb._arrayIndex); + + _arrayIndex += sb._arrayIndex; + + return this; + } + + public int capacity() { + return _array.length; + } + + public int index() { + return _arrayIndex; + } + + public int length() { + int length = 0; + + for (int i = 0; i < _arrayIndex; i++) { + length += _array[i].length(); + } + + return length; + } + + public void setIndex(int newIndex) { + if (newIndex < 0) { + throw new ArrayIndexOutOfBoundsException(newIndex); + } + + if (newIndex > _array.length) { + String[] newArray = new String[newIndex]; + + System.arraycopy(_array, 0, newArray, 0, _arrayIndex); + + _array = newArray; + } + + if (_arrayIndex < newIndex) { + for (int i = _arrayIndex; i < newIndex; i++) { + _array[i] = StringPool.BLANK; + } + } + + if (_arrayIndex > newIndex) { + for (int i = newIndex; i < _arrayIndex; i++) { + _array[i] = null; + } + } + + _arrayIndex = newIndex; + } + + public void setStringAt(String s, int index) { + if ((index < 0) || (index >= _arrayIndex)) { + throw new ArrayIndexOutOfBoundsException(index); + } + + _array[index] = s; + } + + public String stringAt(int index) { + if ((index < 0) || (index >= _arrayIndex)) { + throw new ArrayIndexOutOfBoundsException(index); + } + + return _array[index]; + } + + public String toString() { + if (_arrayIndex == 0) { + return StringPool.BLANK; + } + + if (_arrayIndex == 1) { + return _array[0]; + } + + if (_arrayIndex == 2) { + return _array[0].concat(_array[1]); + } + + int length = 0; + + for (int i = 0; i < _arrayIndex; i++) { + length += _array[i].length(); + } + + if ((_unsafeStringConstructor != null) && + (length >= UNSAFE_CREATE_THRESHOLD)) { + + return unsafeCreate(_array, _arrayIndex, length); + } + else if (_arrayIndex == 3) { + return _array[0].concat(_array[1]).concat(_array[2]); + } + else { + return safeCreate(_array, _arrayIndex, length); + } + } + + public void writeTo(Writer writer) throws IOException { + for (int i = 0; i < _arrayIndex; i++) { + writer.write(_array[i]); + } + } + + protected void expandCapacity(int newCapacity) { + String[] newArray = new String[newCapacity]; + + System.arraycopy(_array, 0, newArray, 0, _arrayIndex); + + _array = newArray; + } + + protected String safeCreate(String[] array, int index, int length) { + StringBuilder sb = new StringBuilder(length); + + for (int i = 0; i < index; i++) { + sb.append(array[i]); + } + + return sb.toString(); + } + + protected String unsafeCreate(String[] array, int index, int length) { + char[] charArray = new char[length]; + + int offset = 0; + + for (int i = 0; i < index; i++) { + String s = array[i]; + + s.getChars(0, s.length(), charArray, offset); + + offset += s.length(); + } + + try { + return _unsafeStringConstructor.newInstance(0, length, charArray); + } + catch (Exception e) { + throw new IllegalStateException(e); + } + } + + private static final int _DEFAULT_ARRAY_CAPACITY = 16; + + private static final String _FALSE = "false"; + + private static final String _TRUE = "true"; + + private static Log _log = LogFactoryUtil.getLog(StringBundler.class); + + private static Constructor _unsafeStringConstructor; + + static { + if (UNSAFE_CREATE_THRESHOLD > 0) { + try { + _unsafeStringConstructor = String.class.getDeclaredConstructor( + int.class, int.class, char[].class); + + _unsafeStringConstructor.setAccessible(true); + } + catch (Exception e) { + _log.error(e, e); + } + } + } + + private String[] _array; + private int _arrayIndex; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/StringPool.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/StringPool.java new file mode 100644 index 000000000..34ab945c2 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/StringPool.java @@ -0,0 +1,166 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +/** + * @author Brian Wing Shun Chan + */ +public class StringPool { + + public static final String AMPERSAND = "&"; + + public static final String AMPERSAND_ENCODED = "&"; + + public static final String APOSTROPHE = "'"; + + public static final String AT = "@"; + + public static final String BACK_SLASH = "\\"; + + public static final String BETWEEN = "BETWEEN"; + + public static final String BLANK = ""; + + public static final String CARET = "^"; + + public static final String CDATA_CLOSE = "]]>"; + + public static final String CDATA_OPEN = ""; + + public static final String GREATER_THAN_OR_EQUAL = ">="; + + public static final String INVERTED_EXCLAMATION = "\u00A1"; + + public static final String INVERTED_QUESTION = "\u00BF"; + + public static final String IS_NOT_NULL = "IS NOT NULL"; + + public static final String IS_NULL = "IS NULL"; + + public static final String ISO_8859_1 = "ISO-8859-1"; + + public static final String LESS_THAN = "<"; + + public static final String LESS_THAN_OR_EQUAL = "<="; + + public static final String LIKE = "LIKE"; + + public static final String MINUS = "-"; + + public static final String NBSP = " "; + + public static final String NEW_LINE = "\n"; + + public static final String NOT_EQUAL = "!="; + + public static final String NOT_LIKE = "NOT LIKE"; + + public static final String NULL = "null"; + + public static final String OPEN_BRACKET = "["; + + public static final String OPEN_CURLY_BRACE = "{"; + + public static final String OPEN_PARENTHESIS = "("; + + public static final String PERCENT = "%"; + + public static final String PERIOD = "."; + + public static final String PIPE = "|"; + + public static final String PLUS = "+"; + + public static final String POUND = "#"; + + public static final String QUESTION = "?"; + + public static final String QUOTE = "\""; + + public static final String RETURN = "\r"; + + public static final String RETURN_NEW_LINE = "\r\n"; + + public static final String SEMICOLON = ";"; + + public static final String SLASH = FORWARD_SLASH; + + public static final String SPACE = " "; + + public static final String STAR = "*"; + + public static final String TAB = "\t"; + + public static final String TILDE = "~"; + + public static final String TRUE = "true"; + + public static final String UNDERLINE = "_"; + + public static final String UTC = "UTC"; + + public static final String UTF8 = "UTF-8"; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/StringUtil.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/StringUtil.java new file mode 100644 index 000000000..718ec45f5 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/StringUtil.java @@ -0,0 +1,1737 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader; +import com.liferay.portal.kernel.io.unsync.UnsyncStringReader; +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import java.net.URL; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Enumeration; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author Brian Wing Shun Chan + * @author Sandeep Soni + * @author Ganesh Ram + */ +public class StringUtil { + + public static String add(String s, String add) { + return add(s, add, StringPool.COMMA); + } + + public static String add(String s, String add, String delimiter) { + return add(s, add, delimiter, false); + } + + public static String add( + String s, String add, String delimiter, boolean allowDuplicates) { + + if ((add == null) || (delimiter == null)) { + return null; + } + + if (s == null) { + s = StringPool.BLANK; + } + + if (allowDuplicates || !contains(s, add, delimiter)) { + StringBundler sb = new StringBundler(); + + sb.append(s); + + if (Validator.isNull(s) || s.endsWith(delimiter)) { + sb.append(add); + sb.append(delimiter); + } + else { + sb.append(delimiter); + sb.append(add); + sb.append(delimiter); + } + + s = sb.toString(); + } + + return s; + } + + public static String bytesToHexString(byte[] bytes) { + StringBuilder sb = new StringBuilder(bytes.length * 2); + + for (int i = 0; i < bytes.length; i++) { + String hex = Integer.toHexString( + 0x0100 + (bytes[i] & 0x00FF)).substring(1); + + if (hex.length() < 2) { + sb.append("0"); + } + + sb.append(hex); + } + + return sb.toString(); + } + + public static boolean contains(String s, String text) { + return contains(s, text, StringPool.COMMA); + } + + public static boolean contains(String s, String text, String delimiter) { + if ((s == null) || (text == null) || (delimiter == null)) { + return false; + } + + if (!s.endsWith(delimiter)) { + s = s.concat(delimiter); + } + + String dtd = delimiter.concat(text).concat(delimiter); + + int pos = s.indexOf(dtd); + + if (pos == -1) { + String td = text.concat(delimiter); + + if (s.startsWith(td)) { + return true; + } + + return false; + } + + return true; + } + + public static int count(String s, String text) { + if ((s == null) || (text == null)) { + return 0; + } + + int count = 0; + + int pos = s.indexOf(text); + + while (pos != -1) { + pos = s.indexOf(text, pos + text.length()); + + count++; + } + + return count; + } + + public static boolean endsWith(String s, char end) { + return endsWith(s, (new Character(end)).toString()); + } + + public static boolean endsWith(String s, String end) { + if ((s == null) || (end == null)) { + return false; + } + + if (end.length() > s.length()) { + return false; + } + + String temp = s.substring(s.length() - end.length(), s.length()); + + if (temp.equalsIgnoreCase(end)) { + return true; + } + else { + return false; + } + } + + public static String extractChars(String s) { + if (s == null) { + return StringPool.BLANK; + } + + StringBuilder sb = new StringBuilder(); + + char[] chars = s.toCharArray(); + + for (int i = 0; i < chars.length; i++) { + if (Validator.isChar(chars[i])) { + sb.append(chars[i]); + } + } + + return sb.toString(); + } + + public static String extractDigits(String s) { + if (s == null) { + return StringPool.BLANK; + } + + StringBuilder sb = new StringBuilder(); + + char[] chars = s.toCharArray(); + + for (int i = 0; i < chars.length; i++) { + if (Validator.isDigit(chars[i])) { + sb.append(chars[i]); + } + } + + return sb.toString(); + } + + public static String extractFirst(String s, String delimiter) { + if (s == null) { + return null; + } + else { + String[] array = split(s, delimiter); + + if (array.length > 0) { + return array[0]; + } + else { + return null; + } + } + } + + public static String extractLast(String s, String delimiter) { + if (s == null) { + return null; + } + else { + String[] array = split(s, delimiter); + + if (array.length > 0) { + return array[array.length - 1]; + } + else { + return null; + } + } + } + + /** + * @deprecated + */ + public static String highlight(String s, String keywords) { + return highlight(s, keywords, "", ""); + } + + /** + * @deprecated + */ + public static String highlight( + String s, String keywords, String highlight1, String highlight2) { + + if (Validator.isNull(s) || Validator.isNull(keywords)) { + return s; + } + + Pattern pattern = Pattern.compile( + Pattern.quote(keywords), Pattern.CASE_INSENSITIVE); + + return _highlight(s, pattern, highlight1, highlight2); + } + + public static String highlight(String s, String[] queryTerms) { + return highlight( + s, queryTerms, "", ""); + } + + public static String highlight( + String s, String[] queryTerms, String highlight1, String highlight2) { + + if (Validator.isNull(s) || Validator.isNull(queryTerms)) { + return s; + } + + StringBundler sb = null; + + if (queryTerms.length == 0) { + sb = new StringBundler(); + } + else { + sb = new StringBundler(2 * queryTerms.length - 1); + } + + for (int i = 0; i < queryTerms.length; i++) { + sb.append(Pattern.quote(queryTerms[i].trim())); + + if ((i + 1) < queryTerms.length) { + sb.append(StringPool.PIPE); + } + } + + int flags = + Pattern.CANON_EQ | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE; + + Pattern pattern = Pattern.compile(sb.toString(), flags); + + return _highlight(s, pattern, highlight1, highlight2); + } + + public static String insert(String s, String insert, int offset) { + if (s == null) { + return null; + } + + if (insert == null) { + return s; + } + + if (offset > s.length()) { + offset = s.length(); + } + + StringBuilder sb = new StringBuilder(s); + + sb.insert(offset, insert); + + return sb.toString(); + } + + public static String lowerCase(String s) { + if (s == null) { + return null; + } + else { + return s.toLowerCase(); + } + } + + public static boolean matches(String s, String pattern) { + String[] array = pattern.split("\\*"); + + for (int i = 0; i < array.length; i++) { + int pos = s.indexOf(array[i]); + + if (pos == -1) { + return false; + } + + s = s.substring(pos + array[i].length()); + } + + return true; + } + + public static String merge(boolean[] array) { + return merge(array, StringPool.COMMA); + } + + public static String merge(boolean[] array, String delimiter) { + if (array == null) { + return null; + } + + StringBundler sb = null; + + if (array.length == 0) { + sb = new StringBundler(); + } + else { + sb = new StringBundler(2 * array.length - 1); + } + + for (int i = 0; i < array.length; i++) { + sb.append(String.valueOf(array[i]).trim()); + + if ((i + 1) != array.length) { + sb.append(delimiter); + } + } + + return sb.toString(); + } + + public static String merge(Collection col) { + return merge(col, StringPool.COMMA); + } + + public static String merge(Collection col, String delimiter) { + if (col == null) { + return null; + } + + return merge(col.toArray(new Object[col.size()]), delimiter); + } + + public static String merge(double[] array) { + return merge(array, StringPool.COMMA); + } + + public static String merge(double[] array, String delimiter) { + if (array == null) { + return null; + } + + StringBundler sb = null; + + if (array.length == 0) { + sb = new StringBundler(); + } + else { + sb = new StringBundler(2 * array.length - 1); + } + + for (int i = 0; i < array.length; i++) { + sb.append(String.valueOf(array[i]).trim()); + + if ((i + 1) != array.length) { + sb.append(delimiter); + } + } + + return sb.toString(); + } + + public static String merge(float[] array) { + return merge(array, StringPool.COMMA); + } + + public static String merge(float[] array, String delimiter) { + if (array == null) { + return null; + } + + StringBundler sb = null; + + if (array.length == 0) { + sb = new StringBundler(); + } + else { + sb = new StringBundler(2 * array.length - 1); + } + + for (int i = 0; i < array.length; i++) { + sb.append(String.valueOf(array[i]).trim()); + + if ((i + 1) != array.length) { + sb.append(delimiter); + } + } + + return sb.toString(); + } + + public static String merge(int[] array) { + return merge(array, StringPool.COMMA); + } + + public static String merge(int[] array, String delimiter) { + if (array == null) { + return null; + } + + StringBundler sb = null; + + if (array.length == 0){ + sb = new StringBundler(); + } + else { + sb = new StringBundler(2 * array.length - 1); + } + + for (int i = 0; i < array.length; i++) { + sb.append(String.valueOf(array[i]).trim()); + + if ((i + 1) != array.length) { + sb.append(delimiter); + } + } + + return sb.toString(); + } + + public static String merge(long[] array) { + return merge(array, StringPool.COMMA); + } + + public static String merge(long[] array, String delimiter) { + if (array == null) { + return null; + } + + StringBundler sb = null; + + if (array.length == 0) { + sb = new StringBundler(); + } + else { + sb = new StringBundler(2 * array.length - 1); + } + + for (int i = 0; i < array.length; i++) { + sb.append(String.valueOf(array[i]).trim()); + + if ((i + 1) != array.length) { + sb.append(delimiter); + } + } + + return sb.toString(); + } + + public static String merge(Object[] array) { + return merge(array, StringPool.COMMA); + } + + public static String merge(Object[] array, String delimiter) { + if (array == null) { + return null; + } + + StringBundler sb = null; + + if (array.length == 0) { + sb = new StringBundler(); + } + else { + sb = new StringBundler(2 * array.length - 1); + } + + for (int i = 0; i < array.length; i++) { + sb.append(String.valueOf(array[i]).trim()); + + if ((i + 1) != array.length) { + sb.append(delimiter); + } + } + + return sb.toString(); + } + + public static String merge(short[] array) { + return merge(array, StringPool.COMMA); + } + + public static String merge(short[] array, String delimiter) { + if (array == null) { + return null; + } + + StringBundler sb = null; + + if (array.length == 0) { + sb = new StringBundler(); + } + else { + sb = new StringBundler(2 * array.length - 1); + } + + for (int i = 0; i < array.length; i++) { + sb.append(String.valueOf(array[i]).trim()); + + if ((i + 1) != array.length) { + sb.append(delimiter); + } + } + + return sb.toString(); + } + + public static String randomize(String s) { + return Randomizer.getInstance().randomize(s); + } + + public static String read(ClassLoader classLoader, String name) + throws IOException { + + return read(classLoader, name, false); + } + + public static String read(ClassLoader classLoader, String name, boolean all) + throws IOException { + + if (all) { + StringBundler sb = new StringBundler(); + + Enumeration enu = classLoader.getResources(name); + + while (enu.hasMoreElements()) { + URL url = enu.nextElement(); + + InputStream is = url.openStream(); + + if (is == null) { + throw new IOException( + "Unable to open resource at " + url.toString()); + } + + String s = read(is); + + if (s != null) { + sb.append(s); + sb.append(StringPool.NEW_LINE); + } + + is.close(); + } + + return sb.toString().trim(); + } + else { + InputStream is = classLoader.getResourceAsStream(name); + + if (is == null) { + throw new IOException( + "Unable to open resource in class loader " + name); + } + + String s = read(is); + + is.close(); + + return s; + } + } + + public static String read(InputStream is) throws IOException { + StringBundler sb = new StringBundler(); + + UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader( + new InputStreamReader(is)); + + String line = null; + + while ((line = unsyncBufferedReader.readLine()) != null) { + sb.append(line); + sb.append(CharPool.NEW_LINE); + } + + unsyncBufferedReader.close(); + + return sb.toString().trim(); + } + + public static void readLines(InputStream is, Collection lines) + throws IOException { + + UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader( + new InputStreamReader(is)); + + String line = null; + + while ((line = unsyncBufferedReader.readLine()) != null) { + lines.add(line); + } + + unsyncBufferedReader.close(); + } + + public static String remove(String s, String remove) { + return remove(s, remove, StringPool.COMMA); + } + + public static String remove(String s, String remove, String delimiter) { + if ((s == null) || (remove == null) || (delimiter == null)) { + return null; + } + + if (Validator.isNotNull(s) && !s.endsWith(delimiter)) { + s += delimiter; + } + + String drd = delimiter.concat(remove).concat(delimiter); + + String rd = remove.concat(delimiter); + + while (contains(s, remove, delimiter)) { + int pos = s.indexOf(drd); + + if (pos == -1) { + if (s.startsWith(rd)) { + int x = remove.length() + delimiter.length(); + int y = s.length(); + + s = s.substring(x, y); + } + } + else { + int x = pos + remove.length() + delimiter.length(); + int y = s.length(); + + String temp = s.substring(0, pos); + + s = temp.concat(s.substring(x, y)); + } + } + + return s; + } + + public static String replace(String s, char oldSub, char newSub) { + if (s == null) { + return null; + } + + return s.replace(oldSub, newSub); + } + + public static String replace(String s, char oldSub, String newSub) { + if ((s == null) || (newSub == null)) { + return null; + } + + // The number 5 is arbitrary and is used as extra padding to reduce + // buffer expansion + + StringBuilder sb = new StringBuilder(s.length() + 5 * newSub.length()); + + char[] chars = s.toCharArray(); + + for (char c : chars) { + if (c == oldSub) { + sb.append(newSub); + } + else { + sb.append(c); + } + } + + return sb.toString(); + } + + public static String replace(String s, String oldSub, String newSub) { + return replace(s, oldSub, newSub, 0); + } + + public static String replace( + String s, String oldSub, String newSub, int fromIndex) { + + if ((s == null) || (oldSub == null) || (newSub == null)) { + return null; + } + + if (oldSub.equals(StringPool.BLANK)) { + return s; + } + + int y = s.indexOf(oldSub, fromIndex); + + if (y >= 0) { + StringBundler sb = new StringBundler(); + + int length = oldSub.length(); + int x = 0; + + while (x <= y) { + sb.append(s.substring(x, y)); + sb.append(newSub); + + x = y + length; + y = s.indexOf(oldSub, x); + } + + sb.append(s.substring(x)); + + return sb.toString(); + } + else { + return s; + } + } + + public static String replace( + String s, String begin, String end, Map values) { + + StringBundler sb = replaceToStringBundler(s, begin, end, values); + + return sb.toString(); + } + + public static String replace(String s, String[] oldSubs, String[] newSubs) { + if ((s == null) || (oldSubs == null) || (newSubs == null)) { + return null; + } + + if (oldSubs.length != newSubs.length) { + return s; + } + + for (int i = 0; i < oldSubs.length; i++) { + s = replace(s, oldSubs[i], newSubs[i]); + } + + return s; + } + + public static String replace( + String s, String[] oldSubs, String[] newSubs, boolean exactMatch) { + + if ((s == null) || (oldSubs == null) || (newSubs == null)) { + return null; + } + + if (oldSubs.length != newSubs.length) { + return s; + } + + if (!exactMatch) { + replace(s, oldSubs, newSubs); + } + else { + for (int i = 0; i < oldSubs.length; i++) { + s = s.replaceAll("\\b" + oldSubs[i] + "\\b" , newSubs[i]); + } + } + + return s; + } + + public static String replaceFirst(String s, char oldSub, char newSub) { + if (s == null) { + return null; + } + + return replaceFirst(s, String.valueOf(oldSub), String.valueOf(newSub)); + } + + public static String replaceFirst(String s, char oldSub, String newSub) { + if ((s == null) || (newSub == null)) { + return null; + } + + return replaceFirst(s, String.valueOf(oldSub), newSub); + } + + public static String replaceFirst(String s, String oldSub, String newSub) { + if ((s == null) || (oldSub == null) || (newSub == null)) { + return null; + } + + if (oldSub.equals(newSub)) { + return s; + } + + int y = s.indexOf(oldSub); + + if (y >= 0) { + return s.substring(0, y).concat(newSub).concat( + s.substring(y + oldSub.length())); + } + else { + return s; + } + } + + public static String replaceFirst( + String s, String[] oldSubs, String[] newSubs) { + + if ((s == null) || (oldSubs == null) || (newSubs == null)) { + return null; + } + + if (oldSubs.length != newSubs.length) { + return s; + } + + for (int i = 0; i < oldSubs.length; i++) { + s = replaceFirst(s, oldSubs[i], newSubs[i]); + } + + return s; + } + + public static String replaceLast(String s, char oldSub, char newSub) { + if (s == null) { + return null; + } + + return replaceLast(s, String.valueOf(oldSub), String.valueOf(newSub)); + } + + public static String replaceLast(String s, char oldSub, String newSub) { + if ((s == null) || (newSub == null)) { + return null; + } + + return replaceLast(s, String.valueOf(oldSub), newSub); + } + + public static String replaceLast(String s, String oldSub, String newSub) { + if ((s == null) || (oldSub == null) || (newSub == null)) { + return null; + } + + if (oldSub.equals(newSub)) { + return s; + } + + int y = s.lastIndexOf(oldSub); + + if (y >= 0) { + return s.substring(0, y).concat(newSub).concat( + s.substring(y + oldSub.length())); + } + else { + return s; + } + } + + public static String replaceLast( + String s, String[] oldSubs, String[] newSubs) { + + if ((s == null) || (oldSubs == null) || (newSubs == null)) { + return null; + } + + if (oldSubs.length != newSubs.length) { + return s; + } + + for (int i = 0; i < oldSubs.length; i++) { + s = replaceLast(s, oldSubs[i], newSubs[i]); + } + + return s; + } + + public static StringBundler replaceToStringBundler( + String s, String begin, String end, Map values) { + + if ((s == null) || (begin == null) || (end == null) || + (values == null) || (values.size() == 0)) { + + return new StringBundler(s); + } + + StringBundler sb = new StringBundler(values.size() * 2 + 1); + + int pos = 0; + + while (true) { + int x = s.indexOf(begin, pos); + int y = s.indexOf(end, x + begin.length()); + + if ((x == -1) || (y == -1)) { + sb.append(s.substring(pos, s.length())); + + break; + } + else { + sb.append(s.substring(pos, x)); + + String oldValue = s.substring(x + begin.length(), y); + + String newValue = values.get(oldValue); + + if (newValue == null) { + newValue = oldValue; + } + + sb.append(newValue); + + pos = y + end.length(); + } + } + + return sb; + } + + public static StringBundler replaceWithStringBundler( + String s, String begin, String end, Map values) { + + if ((s == null) || (begin == null) || (end == null) || + (values == null) || (values.size() == 0)) { + + return new StringBundler(s); + } + + int size = values.size() + 1; + + for (StringBundler valueSB : values.values()) { + size += valueSB.index(); + } + + StringBundler sb = new StringBundler(size); + + int pos = 0; + + while (true) { + int x = s.indexOf(begin, pos); + int y = s.indexOf(end, x + begin.length()); + + if ((x == -1) || (y == -1)) { + sb.append(s.substring(pos, s.length())); + + break; + } + else { + sb.append(s.substring(pos, x)); + + String oldValue = s.substring(x + begin.length(), y); + + StringBundler newValue = values.get(oldValue); + + if (newValue == null) { + sb.append(oldValue); + } + else { + sb.append(newValue); + } + + pos = y + end.length(); + } + } + + return sb; + } + + public static String reverse(String s) { + if (s == null) { + return null; + } + + char[] chars = s.toCharArray(); + char[] reverse = new char[chars.length]; + + for (int i = 0; i < chars.length; i++) { + reverse[i] = chars[chars.length - i - 1]; + } + + return new String(reverse); + } + + public static String safePath(String path) { + return replace(path, StringPool.DOUBLE_SLASH, StringPool.SLASH); + } + + public static String shorten(String s) { + return shorten(s, 20); + } + + public static String shorten(String s, int length) { + return shorten(s, length, "..."); + } + + public static String shorten(String s, int length, String suffix) { + if ((s == null) || (suffix == null)) { + return null; + } + + if (s.length() > length) { + for (int j = length; j >= 0; j--) { + if (Character.isWhitespace(s.charAt(j))) { + length = j; + + break; + } + } + + String temp = s.substring(0, length); + + s = temp.concat(suffix); + } + + return s; + } + + public static String shorten(String s, String suffix) { + return shorten(s, 20, suffix); + } + + public static String[] split(String s) { + return split(s, StringPool.COMMA); + } + + public static boolean[] split(String s, boolean x) { + return split(s, StringPool.COMMA, x); + } + + public static double[] split(String s, double x) { + return split(s, StringPool.COMMA, x); + } + + public static float[] split(String s, float x) { + return split(s, StringPool.COMMA, x); + } + + public static int[] split(String s, int x) { + return split(s, StringPool.COMMA, x); + } + + public static long[] split(String s, long x) { + return split(s, StringPool.COMMA, x); + } + + public static short[] split(String s, short x) { + return split(s, StringPool.COMMA, x); + } + + public static String[] split(String s, String delimiter) { + if ((Validator.isNull(s)) || (delimiter == null) || + (delimiter.equals(StringPool.BLANK))) { + + return new String[0]; + } + + s = s.trim(); + + if (s.equals(delimiter)) { + return new String[0]; + } + + List nodeValues = new ArrayList(); + + if (delimiter.equals(StringPool.NEW_LINE) || + delimiter.equals(StringPool.RETURN)) { + + try { + UnsyncBufferedReader unsyncBufferedReader = + new UnsyncBufferedReader(new UnsyncStringReader(s)); + + String line = null; + + while ((line = unsyncBufferedReader.readLine()) != null) { + nodeValues.add(line); + } + + unsyncBufferedReader.close(); + } + catch (IOException ioe) { + _log.error(ioe.getMessage()); + } + } + else { + int offset = 0; + int pos = s.indexOf(delimiter, offset); + + while (pos != -1) { + nodeValues.add(s.substring(offset, pos)); + + offset = pos + delimiter.length(); + pos = s.indexOf(delimiter, offset); + } + + if (offset < s.length()) { + nodeValues.add(s.substring(offset)); + } + } + + return nodeValues.toArray(new String[nodeValues.size()]); + } + + public static boolean[] split(String s, String delimiter, boolean x) { + String[] array = split(s, delimiter); + boolean[] newArray = new boolean[array.length]; + + for (int i = 0; i < array.length; i++) { + boolean value = x; + + try { + value = Boolean.valueOf(array[i]).booleanValue(); + } + catch (Exception e) { + } + + newArray[i] = value; + } + + return newArray; + } + + public static double[] split(String s, String delimiter, double x) { + String[] array = split(s, delimiter); + double[] newArray = new double[array.length]; + + for (int i = 0; i < array.length; i++) { + double value = x; + + try { + value = Double.parseDouble(array[i]); + } + catch (Exception e) { + } + + newArray[i] = value; + } + + return newArray; + } + + public static float[] split(String s, String delimiter, float x) { + String[] array = split(s, delimiter); + float[] newArray = new float[array.length]; + + for (int i = 0; i < array.length; i++) { + float value = x; + + try { + value = Float.parseFloat(array[i]); + } + catch (Exception e) { + } + + newArray[i] = value; + } + + return newArray; + } + + public static int[] split(String s, String delimiter, int x) { + String[] array = split(s, delimiter); + int[] newArray = new int[array.length]; + + for (int i = 0; i < array.length; i++) { + int value = x; + + try { + value = Integer.parseInt(array[i]); + } + catch (Exception e) { + } + + newArray[i] = value; + } + + return newArray; + } + + public static long[] split(String s, String delimiter, long x) { + String[] array = split(s, delimiter); + long[] newArray = new long[array.length]; + + for (int i = 0; i < array.length; i++) { + long value = x; + + try { + value = Long.parseLong(array[i]); + } + catch (Exception e) { + } + + newArray[i] = value; + } + + return newArray; + } + + public static short[] split(String s, String delimiter, short x) { + String[] array = split(s, delimiter); + short[] newArray = new short[array.length]; + + for (int i = 0; i < array.length; i++) { + short value = x; + + try { + value = Short.parseShort(array[i]); + } + catch (Exception e) { + } + + newArray[i] = value; + } + + return newArray; + } + + public static boolean startsWith(String s, char begin) { + return startsWith(s, (new Character(begin)).toString()); + } + + public static boolean startsWith(String s, String start) { + if ((s == null) || (start == null)) { + return false; + } + + if (start.length() > s.length()) { + return false; + } + + String temp = s.substring(0, start.length()); + + if (temp.equalsIgnoreCase(start)) { + return true; + } + else { + return false; + } + } + + /** + * Return the number of starting letters that s1 and s2 have in common + * before they deviate. + * + * @return the number of starting letters that s1 and s2 have in common + * before they deviate + */ + public static int startsWithWeight(String s1, String s2) { + if ((s1 == null) || (s2 == null)) { + return 0; + } + + char[] chars1 = s1.toCharArray(); + char[] chars2 = s2.toCharArray(); + + int i = 0; + + for (; (i < chars1.length) && (i < chars2.length); i++) { + if (chars1[i] != chars2[i]) { + break; + } + } + + return i; + } + + public static String strip(String s, char remove) { + if (s == null) { + return null; + } + + int x = s.indexOf(remove); + + if (x < 0) { + return s; + } + + int y = 0; + + StringBuilder sb = new StringBuilder(s.length()); + + while (x >= 0) { + sb.append(s.subSequence(y, x)); + + y = x + 1; + + x = s.indexOf(remove, y); + } + + sb.append(s.substring(y)); + + return sb.toString(); + } + + public static String stripBetween(String s, String begin, String end) { + if ((s == null) || (begin == null) || (end == null)) { + return s; + } + + StringBuilder sb = new StringBuilder(s.length()); + + int pos = 0; + + while (true) { + int x = s.indexOf(begin, pos); + int y = s.indexOf(end, x + begin.length()); + + if ((x == -1) || (y == -1)) { + sb.append(s.substring(pos, s.length())); + + break; + } + else { + sb.append(s.substring(pos, x)); + + pos = y + end.length(); + } + } + + return sb.toString(); + } + + public static String toCharCode(String s) { + StringBundler sb = new StringBundler(s.length()); + + for (int i = 0; i < s.length(); i++) { + sb.append(s.codePointAt(i)); + } + + return sb.toString(); + } + + public static String toHexString(int i) { + char[] buffer = new char[8]; + + int index = 8; + + do { + buffer[--index] = _HEX_DIGITS[i & 15]; + + i >>>= 4; + } + while (i != 0); + + return new String(buffer, index, 8 - index); + } + + public static String toHexString(long l) { + char[] buffer = new char[16]; + + int index = 16; + + do { + buffer[--index] = _HEX_DIGITS[(int) (l & 15)]; + + l >>>= 4; + } + while (l != 0); + + return new String(buffer, index, 16 - index); + } + + public static String toHexString(Object obj) { + if (obj instanceof Integer) { + return toHexString(((Integer)obj).intValue()); + } + else if (obj instanceof Long) { + return toHexString(((Long)obj).longValue()); + } + else { + return String.valueOf(obj); + } + } + + public static String trim(String s) { + return trim(s, null); + } + + public static String trim(String s, char c) { + return trim(s, new char[] {c}); + } + + public static String trim(String s, char[] exceptions) { + if (s == null) { + return null; + } + + char[] chars = s.toCharArray(); + + int len = chars.length; + + int x = 0; + int y = chars.length; + + for (int i = 0; i < len; i++) { + char c = chars[i]; + + if (_isTrimable(c, exceptions)) { + x = i + 1; + } + else { + break; + } + } + + for (int i = len - 1; i >= 0; i--) { + char c = chars[i]; + + if (_isTrimable(c, exceptions)) { + y = i; + } + else { + break; + } + } + + if ((x != 0) || (y != len)) { + return s.substring(x, y); + } + else { + return s; + } + } + + public static String trimLeading(String s) { + return trimLeading(s, null); + } + + public static String trimLeading(String s, char c) { + return trimLeading(s, new char[] {c}); + } + + public static String trimLeading(String s, char[] exceptions) { + if (s == null) { + return null; + } + + char[] chars = s.toCharArray(); + + int len = chars.length; + + int x = 0; + int y = chars.length; + + for (int i = 0; i < len; i++) { + char c = chars[i]; + + if (_isTrimable(c, exceptions)) { + x = i + 1; + } + else { + break; + } + } + + if ((x != 0) || (y != len)) { + return s.substring(x, y); + } + else { + return s; + } + } + + public static String trimTrailing(String s) { + return trimTrailing(s, null); + } + + public static String trimTrailing(String s, char c) { + return trimTrailing(s, new char[] {c}); + } + + public static String trimTrailing(String s, char[] exceptions) { + if (s == null) { + return null; + } + + char[] chars = s.toCharArray(); + + int len = chars.length; + + int x = 0; + int y = chars.length; + + for (int i = len - 1; i >= 0; i--) { + char c = chars[i]; + + if (_isTrimable(c, exceptions)) { + y = i; + } + else { + break; + } + } + + if ((x != 0) || (y != len)) { + return s.substring(x, y); + } + else { + return s; + } + } + + public static String unquote(String s) { + if (Validator.isNull(s)) { + return s; + } + + if ((s.charAt(0) == CharPool.APOSTROPHE) && + (s.charAt(s.length() - 1) == CharPool.APOSTROPHE)) { + + return s.substring(1, s.length() - 1); + } + else if ((s.charAt(0) == CharPool.QUOTE) && + (s.charAt(s.length() - 1) == CharPool.QUOTE)) { + + return s.substring(1, s.length() - 1); + } + + return s; + } + + public static String upperCase(String s) { + if (s == null) { + return null; + } + else { + return s.toUpperCase(); + } + } + + public static String upperCaseFirstLetter(String s) { + char[] chars = s.toCharArray(); + + if ((chars[0] >= 97) && (chars[0] <= 122)) { + chars[0] = (char)(chars[0] - 32); + } + + return new String(chars); + } + + public static String valueOf(Object obj) { + return String.valueOf(obj); + } + + public static String wrap(String text) { + return wrap(text, 80, StringPool.NEW_LINE); + } + + public static String wrap(String text, int width, String lineSeparator) { + try { + return _wrap(text, width, lineSeparator); + } + catch (IOException ioe) { + _log.error(ioe.getMessage()); + + return text; + } + } + + private static String _highlight( + String s, Pattern pattern, String highlight1, String highlight2) { + + StringTokenizer st = new StringTokenizer(s); + + StringBundler sb = null; + + if (st.countTokens() == 0) { + sb = new StringBundler(); + } + else { + sb = new StringBundler(2 * st.countTokens() - 1); + } + + while (st.hasMoreTokens()) { + String token = st.nextToken(); + + Matcher matcher = pattern.matcher(token); + + if (matcher.find()) { + StringBuffer hightlighted = new StringBuffer(); + + do { + matcher.appendReplacement( + hightlighted, highlight1 + matcher.group() + + highlight2); + } + while (matcher.find()); + + matcher.appendTail(hightlighted); + + sb.append(hightlighted); + } + else { + sb.append(token); + } + + if (st.hasMoreTokens()) { + sb.append(StringPool.SPACE); + } + } + + return sb.toString(); + } + + private static boolean _isTrimable(char c, char[] exceptions) { + if ((exceptions != null) && (exceptions.length > 0)) { + for (int i = 0; i < exceptions.length; i++) { + if (c == exceptions[i]) { + return false; + } + } + } + + return Character.isWhitespace(c); + } + + private static String _wrap(String text, int width, String lineSeparator) + throws IOException { + + if (text == null) { + return null; + } + + StringBundler sb = new StringBundler(); + + UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader( + new UnsyncStringReader(text)); + + String s = StringPool.BLANK; + + while ((s = unsyncBufferedReader.readLine()) != null) { + if (s.length() == 0) { + sb.append(lineSeparator); + + continue; + } + + int lineLength = 0; + + String[] tokens = s.split(StringPool.SPACE); + + for (String token : tokens) { + if ((lineLength + token.length() + 1) > width) { + if (lineLength > 0) { + sb.append(lineSeparator); + } + + if (token.length() > width) { + int pos = token.indexOf(CharPool.OPEN_PARENTHESIS); + + if (pos != -1) { + sb.append(token.substring(0, pos + 1)); + sb.append(lineSeparator); + + token = token.substring(pos + 1); + + sb.append(token); + + lineLength = token.length(); + } + else { + sb.append(token); + + lineLength = token.length(); + } + } + else { + sb.append(token); + + lineLength = token.length(); + } + } + else { + if (lineLength > 0) { + sb.append(StringPool.SPACE); + + lineLength++; + } + + sb.append(token); + + lineLength += token.length(); + } + } + + sb.append(lineSeparator); + } + + return sb.toString(); + } + + private static final char[] _HEX_DIGITS = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', + 'e', 'f' + }; + + private static Log _log = LogFactoryUtil.getLog(StringUtil.class); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/TranslatedList.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/TranslatedList.java new file mode 100644 index 000000000..7d8d4ce82 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/TranslatedList.java @@ -0,0 +1,116 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * @author Brian Wing Shun Chan + */ +public abstract class TranslatedList extends ListWrapper { + + public TranslatedList(List newList, List oldList) { + super(newList); + + _oldList = oldList; + } + + public boolean add(E o) { + _oldList.add(toOldObject(o)); + + return super.add(o); + } + + public void add(int index, E element) { + _oldList.add(index, toOldObject(element)); + + super.add(index, element); + } + + public boolean addAll(Collection c) { + for (E o : c) { + _oldList.add(toOldObject(o)); + } + + return super.addAll(c); + } + + public boolean addAll(int index, Collection c) { + for (E o : c) { + _oldList.add(index++, toOldObject(o)); + } + + return super.addAll(c); + } + + public boolean remove(Object o) { + _oldList.remove(toOldObject((E)o)); + + return super.remove(o); + } + + public E remove(int index) { + _oldList.remove(index); + + return super.remove(index); + } + + public boolean removeAll(Collection c) { + List tempList = new ArrayList(); + + for (Object o : c) { + tempList.add(toOldObject((E)o)); + } + + _oldList.removeAll(tempList); + + return super.removeAll(c); + } + + public boolean retainAll(Collection c) { + List tempList = new ArrayList(); + + for (Object o : c) { + tempList.add(toOldObject((E)o)); + } + + _oldList.retainAll(tempList); + + return super.retainAll(c); + } + + public E set(int index, E element) { + _oldList.set(index, toOldObject(element)); + + return super.set(index, element); + } + + public List subList(int fromIndex, int toIndex) { + List newList = super.subList(fromIndex, toIndex); + List oldList = _oldList.subList(fromIndex, toIndex); + + return newInstance(newList, oldList); + } + + protected abstract TranslatedList newInstance( + List newList, List oldList); + + protected abstract F toOldObject(E o); + + private List _oldList; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/UnicodeFormatter.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/UnicodeFormatter.java new file mode 100644 index 000000000..48cd3b527 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/UnicodeFormatter.java @@ -0,0 +1,123 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; + +/** + * @author Brian Wing Shun Chan + */ +public class UnicodeFormatter { + + public static String byteToHex(byte b) { + char[] array = {_HEX_DIGITS[(b >> 4) & 0x0f], _HEX_DIGITS[b & 0x0f]}; + + return new String(array); + } + + public static String charToHex(char c) { + byte hi = (byte)(c >>> 8); + byte lo = (byte)(c & 0xff); + + char[] array = { + _HEX_DIGITS[(hi >> 4) & 0x0f], _HEX_DIGITS[hi & 0x0f], + _HEX_DIGITS[(lo >> 4) & 0x0f], _HEX_DIGITS[lo & 0x0f] + }; + + return new String(array); + } + + public static String parseString(String hexString) { + StringBuilder sb = new StringBuilder(); + + char[] array = hexString.toCharArray(); + + if ((array.length % 6) != 0) { + _log.error("String is not in hex format"); + + return hexString; + } + + for (int i = 2; i < hexString.length(); i = i + 6) { + String s = hexString.substring(i, i + 4); + + try { + char c = (char)Integer.parseInt(s, 16); + + sb.append(c); + } + catch (Exception e) { + _log.error(e, e); + + return hexString; + } + } + + return sb.toString(); + } + + public static String toString(char[] array) { + StringBuilder sb = new StringBuilder(array.length * 6); + + char[] hexes = new char[4]; + + for (int i = 0; i < array.length; i++) { + sb.append(_UNICODE_PREFIX); + sb.append(_charToHex(array[i], hexes)); + } + + return sb.toString(); + } + + public static String toString(String s) { + if (s == null) { + return null; + } + + StringBuilder sb = new StringBuilder(s.length() * 6); + + char[] hexes = new char[4]; + + for (int i = 0; i < s.length(); i++) { + sb.append(_UNICODE_PREFIX); + sb.append(_charToHex(s.charAt(i), hexes)); + } + + return sb.toString(); + } + + private static char[] _charToHex(char c, char[] hexes) { + byte hi = (byte)(c >>> 8); + byte lo = (byte)(c & 0xff); + + hexes[0] = _HEX_DIGITS[(hi >> 4) & 0x0f]; + hexes[1] = _HEX_DIGITS[hi & 0x0f]; + hexes[2] = _HEX_DIGITS[(lo >> 4) & 0x0f]; + hexes[3] = _HEX_DIGITS[lo & 0x0f]; + + return hexes; + } + + private static final char[] _HEX_DIGITS = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', + 'e', 'f' + }; + + private static final String _UNICODE_PREFIX = "\\u"; + + private static Log _log = LogFactoryUtil.getLog(UnicodeFormatter.class); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/Validator.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/Validator.java new file mode 100644 index 000000000..5179e7bca --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/util/Validator.java @@ -0,0 +1,656 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.util; + +// import com.liferay.portal.kernel.log.Log; +// import com.liferay.portal.kernel.log.LogFactoryUtil; + +import java.net.MalformedURLException; +import java.net.URL; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author Brian Wing Shun Chan + * @author Alysa Carver + */ +public class Validator { + + public static boolean equals(boolean boolean1, boolean boolean2) { + if (boolean1 == boolean2) { + return true; + } + else { + return false; + } + } + + public static boolean equals(byte byte1, byte byte2) { + if (byte1 == byte2) { + return true; + } + else { + return false; + } + } + + public static boolean equals(char char1, char char2) { + if (char1 == char2) { + return true; + } + else { + return false; + } + } + + public static boolean equals(double double1, double double2) { + if (Double.compare(double1, double2) == 0) { + return true; + } + else { + return false; + } + } + + public static boolean equals(float float1, float float2) { + if (Float.compare(float1, float2) == 0) { + return true; + } + else { + return false; + } + } + + public static boolean equals(int int1, int int2) { + if (int1 == int2) { + return true; + } + else { + return false; + } + } + + public static boolean equals(long long1, long long2) { + if (long1 == long2) { + return true; + } + else { + return false; + } + } + + public static boolean equals(Object obj1, Object obj2) { + if ((obj1 == null) && (obj2 == null)) { + return true; + } + else if ((obj1 == null) || (obj2 == null)) { + return false; + } + else { + return obj1.equals(obj2); + } + } + + public static boolean equals(short short1, short short2) { + if (short1 == short2) { + return true; + } + else { + return false; + } + } + + public static boolean isAddress(String address) { + if (isNull(address)) { + return false; + } + + String[] tokens = address.split(StringPool.AT); + + if (tokens.length != 2) { + return false; + } + + for (String token : tokens) { + for (char c : token.toCharArray()) { + if (Character.isWhitespace(c)) { + return false; + } + } + } + + return true; + } + + public static boolean isAscii(char c) { + int i = c; + + if ((i >= 32) && (i <= 126)) { + return true; + } + else { + return false; + } + } + + /** + * Returns true if c is a letter between a-z and A-Z. + * + * @return true if c is a letter between a-z and A-Z + */ + public static boolean isChar(char c) { + int x = c; + + if ((x >= _CHAR_BEGIN) && (x <= _CHAR_END)) { + return true; + } + + return false; + } + + /** + * Returns true if s is a string of letters that are between + * a-z and A-Z. + * + * @return true if s is a string of letters that are between + * a-z and A-Z + */ + public static boolean isChar(String s) { + if (isNull(s)) { + return false; + } + + for (char c : s.toCharArray()) { + if (!isChar(c)) { + return false; + } + } + + return true; + } + + public static boolean isDate(int month, int day, int year) { + return isGregorianDate(month, day, year); + } + + /** + * Returns true if c is a digit between 0 and 9. + * + * @return true if c is a digit between 0 and 9 + */ + public static boolean isDigit(char c) { + int x = c; + + if ((x >= _DIGIT_BEGIN) && (x <= _DIGIT_END)) { + return true; + } + + return false; + } + + /** + * Returns true if s is a string of letters that are between 0 + * and 9. + * + * @return true if s is a string of letters that are between 0 + * and 9 + */ + public static boolean isDigit(String s) { + if (isNull(s)) { + return false; + } + + for (char c : s.toCharArray()) { + if (!isDigit(c)) { + return false; + } + } + + return true; + } + + public static boolean isDomain(String domainName) { + + // See RFC-1034 (section 3), RFC-1123 (section 2.1), and RFC-952 + // (section B. Lexical grammar) + + if (isNull(domainName)) { + return false; + } + + if (domainName.length() > 255) { + return false; + } + + String[] domainNameArray = StringUtil.split( + domainName, StringPool.PERIOD); + + for (String domainNamePart : domainNameArray) { + char[] domainNamePartCharArray = domainNamePart.toCharArray(); + + for (int i = 0; i < domainNamePartCharArray.length; i++) { + char c = domainNamePartCharArray[i]; + + if ((i == 0) && (c == CharPool.DASH)) { + return false; + } + else if ((i == (domainNamePartCharArray.length - 1)) && + (c == CharPool.DASH)) { + + return false; + } + else if ((!isChar(c)) && (!isDigit(c)) && + (c != CharPool.DASH)) { + + return false; + } + } + } + + return true; + } + + public static boolean isEmailAddress(String emailAddress) { + Boolean valid = null; + + try { + valid = (Boolean)PortalClassInvoker.invoke( + true, isValidMethodKey, emailAddress); + } + catch (Exception e) { + // if (_log.isWarnEnabled()) { + // _log.warn(e); + // } + } + + if (valid == null) { + return false; + } + else { + return valid.booleanValue(); + } + } + + public static boolean isEmailAddressSpecialChar(char c) { + + // LEP-1445 + + for (int i = 0; i < _EMAIL_ADDRESS_SPECIAL_CHAR.length; i++) { + if (c == _EMAIL_ADDRESS_SPECIAL_CHAR[i]) { + return true; + } + } + + return false; + } + + public static boolean isGregorianDate(int month, int day, int year) { + if ((month < 0) || (month > 11)) { + return false; + } + + int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + + if (month == 1) { + int febMax = 28; + + if (((year % 4) == 0) && ((year % 100) != 0) || + ((year % 400) == 0)) { + + febMax = 29; + } + + if ((day < 1) || (day > febMax)) { + return false; + } + } + else if ((day < 1) || (day > months[month])) { + return false; + } + + return true; + } + + public static boolean isHex(String s) { + if (isNull(s)) { + return false; + } + + return true; + } + + public static boolean isHTML(String s) { + if (isNull(s)) { + return false; + } + + if (((s.indexOf("") != -1) || (s.indexOf("") != -1)) && + ((s.indexOf("") != -1) || (s.indexOf("") != -1))) { + + return true; + } + + return false; + } + + public static boolean isIPAddress(String ipAddress) { + Matcher matcher = _ipAddressPattern.matcher(ipAddress); + + return matcher.matches(); + } + + public static boolean isJulianDate(int month, int day, int year) { + if ((month < 0) || (month > 11)) { + return false; + } + + int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + + if (month == 1) { + int febMax = 28; + + if ((year % 4) == 0) { + febMax = 29; + } + + if ((day < 1) || (day > febMax)) { + return false; + } + } + else if ((day < 1) || (day > months[month])) { + return false; + } + + return true; + } + + public static boolean isLUHN(String number) { + if (number == null) { + return false; + } + + number = StringUtil.reverse(number); + + int total = 0; + + for (int i = 0; i < number.length(); i++) { + int x = 0; + + if (((i + 1) % 2) == 0) { + x = Integer.parseInt(number.substring(i, i + 1)) * 2; + + if (x >= 10) { + String s = String.valueOf(x); + + x = Integer.parseInt(s.substring(0, 1)) + + Integer.parseInt(s.substring(1, 2)); + } + } + else { + x = Integer.parseInt(number.substring(i, i + 1)); + } + + total = total + x; + } + + if ((total % 10) == 0) { + return true; + } + else { + return false; + } + } + + public static boolean isName(String name) { + if (isNull(name)) { + return false; + } + + for (char c : name.trim().toCharArray()) { + if (((!isChar(c)) && + (!Character.isWhitespace(c))) || (c == CharPool.COMMA)) { + + return false; + } + } + + return true; + } + + public static boolean isNotNull(Long l) { + return !isNull(l); + } + + public static boolean isNotNull(Object obj) { + return !isNull(obj); + } + + public static boolean isNotNull(Object[] array) { + return !isNull(array); + } + + public static boolean isNotNull(String s) { + return !isNull(s); + } + + public static boolean isNull(Long l) { + if ((l == null) || (l.longValue() == 0)) { + return true; + } + else { + return false; + } + } + + public static boolean isNull(Object obj) { + if (obj instanceof Long) { + return isNull((Long)obj); + } + else if (obj instanceof String) { + return isNull((String)obj); + } + else if (obj == null) { + return true; + } + else { + return false; + } + } + + public static boolean isNull(Object[] array) { + if ((array == null) || (array.length == 0)) { + return true; + } + else { + return false; + } + } + + public static boolean isNull(String s) { + if (s == null) { + return true; + } + + int counter = 0; + + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + + if (c == CharPool.SPACE) { + continue; + } + else if (counter > 3) { + return false; + } + + if (counter == 0) { + if (c != CharPool.LOWER_CASE_N) { + return false; + } + } + else if (counter == 1) { + if (c != CharPool.LOWER_CASE_U) { + return false; + } + } + else if ((counter == 2) || (counter == 3)) { + if (c != CharPool.LOWER_CASE_L) { + return false; + } + } + + counter++; + } + + if ((counter == 0) || (counter == 4)) { + return true; + } + + return false; + } + + public static boolean isNumber(String number) { + if (isNull(number)) { + return false; + } + + for (char c : number.toCharArray()) { + if (!isDigit(c)) { + return false; + } + } + + return true; + } + + public static boolean isPassword(String password) { + if (isNull(password)) { + return false; + } + + if (password.length() < 4) { + return false; + } + + for (char c : password.toCharArray()) { + if (!isChar(c) && !isDigit(c)) { + return false; + } + } + + return true; + } + + public static boolean isPhoneNumber(String phoneNumber) { + return isNumber(StringUtil.extractDigits(phoneNumber)); + } + + public static boolean isUrl(String url) { + if (Validator.isNotNull(url)) { + try { + new URL(url); + + return true; + } + catch (MalformedURLException murle) { + } + } + + return false; + } + + public static boolean isVariableName(String variableName) { + if (isNull(variableName)) { + return false; + } + + Matcher matcher = _variableNamePattern.matcher(variableName); + + if (matcher.matches()) { + return true; + } + else { + return false; + } + } + + public static boolean isVariableTerm(String s) { + if (s.startsWith(_VARIABLE_TERM_BEGIN) && + s.endsWith(_VARIABLE_TERM_END)) { + + return true; + } + else { + return false; + } + } + + public static boolean isWhitespace(char c) { + int i = c; + + if ((i == 0) || Character.isWhitespace(c)) { + return true; + } + else { + return false; + } + } + + public static boolean isXml(String s) { + if (s.startsWith(_XML_BEGIN) || s.startsWith(_XML_EMPTY)) { + return true; + } + else { + return false; + } + } + + private static final int _CHAR_BEGIN = 65; + + private static final int _CHAR_END = 122; + + private static final int _DIGIT_BEGIN = 48; + + private static final int _DIGIT_END = 57; + + private static final char[] _EMAIL_ADDRESS_SPECIAL_CHAR = new char[] { + '.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^', + '_', '`', '{', '|', '}', '~' + }; + + private static final String _VARIABLE_TERM_BEGIN = "[$"; + + private static final String _VARIABLE_TERM_END = "$]"; + + private static final String _XML_BEGIN = ""; + +// private static Log _log = LogFactoryUtil.getLog(Validator.class); + + private static Pattern _ipAddressPattern = Pattern.compile( + "\\b" + + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." + + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." + + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." + + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])" + + "\\b"); + private static MethodKey isValidMethodKey = new MethodKey( + "com.liferay.util.mail.InternetAddressUtil", "isValid", String.class); + private static Pattern _variableNamePattern = Pattern.compile( + "[_a-zA-Z]+[_a-zA-Z0-9]*"); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Attribute.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Attribute.java new file mode 100644 index 000000000..8d19acd8b --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Attribute.java @@ -0,0 +1,42 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +/** + * @author Brian Wing Shun Chan + */ +public interface Attribute extends Node { + + public Object getData(); + + public Namespace getNamespace(); + + public String getNamespacePrefix(); + + public String getNamespaceURI(); + + public QName getQName(); + + public String getQualifiedName(); + + public String getValue(); + + public void setData(Object data); + + public void setNamespace(Namespace namespace); + + public void setValue(String value); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Branch.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Branch.java new file mode 100644 index 000000000..5530ff352 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Branch.java @@ -0,0 +1,78 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +import java.util.Iterator; +import java.util.List; + +/** + * @author Brian Wing Shun Chan + */ +public interface Branch extends Node { + + public void add(Comment comment); + + public void add(Element element); + + public void add(Node node); + + public void add(ProcessingInstruction processingInstruction); + + public Element addElement(QName qName); + + public Element addElement(String name); + + public Element addElement(String qualifiedName, String namespaceURI); + + public void appendContent(Branch branch); + + public void clearContent(); + + public List content(); + + public Element elementByID(String elementID); + + public int indexOf(Node node); + + public Node node(int index); + + public int nodeCount(); + + public Iterator nodeIterator(); + + public void normalize(); + + public ProcessingInstruction processingInstruction(String target); + + public List processingInstructions(); + + public List processingInstructions(String target); + + public boolean remove(Comment comment); + + public boolean remove(Element element); + + public boolean remove(Node node); + + public boolean remove(ProcessingInstruction processingInstruction); + + public boolean removeProcessingInstruction(String target); + + public void setContent(List content); + + public void setProcessingInstructions( + List processingInstructions); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/CDATA.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/CDATA.java new file mode 100644 index 000000000..f1806bb87 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/CDATA.java @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +/** + * @author Brian Wing Shun Chan + */ +public interface CDATA extends Node { +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Comment.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Comment.java new file mode 100644 index 000000000..4d4bcc303 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Comment.java @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +/** + * @author Brian Wing Shun Chan + */ +public interface Comment extends Node { +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Document.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Document.java new file mode 100644 index 000000000..a194d4b79 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Document.java @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +import java.io.IOException; + +/** + * @author Brian Wing Shun Chan + */ +public interface Document extends Branch { + + public Document addComment(String comment); + + public Document addDocType(String name, String publicId, String systemId); + + public String formattedString() throws IOException; + + public String formattedString(String indent) throws IOException; + + public String formattedString(String indent, boolean expandEmptyElements) + throws IOException; + + public Element getRootElement(); + + public String getXMLEncoding(); + + public void setRootElement(Element rootElement); + + public void setXMLEncoding(String encoding); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/DocumentException.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/DocumentException.java new file mode 100644 index 000000000..9f3253f2f --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/DocumentException.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +/** + * @author Brian Wing Shun Chan + */ +public class DocumentException extends Exception { + + public DocumentException() { + super(); + } + + public DocumentException(String msg) { + super(msg); + } + + public DocumentException(Throwable cause) { + super(cause); + } + + public DocumentException(String msg, Throwable cause) { + super(msg, cause); + } + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Element.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Element.java new file mode 100644 index 000000000..d1dcdc280 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Element.java @@ -0,0 +1,161 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +/** + * @author Brian Wing Shun Chan + */ +public interface Element extends Branch { + + public void add(Attribute attribute); + + public void add(CDATA cdata); + + public void add(Entity entity); + + public void add(Namespace namespace); + + public void add(Text text); + + public Element addAttribute(QName qName, String value); + + public Element addAttribute(String name, String value); + + public Element addCDATA(String cdata); + + public Element addComment(String comment); + + public Element addEntity(String name, String text); + + public List additionalNamespaces(); + + public Element addNamespace(String prefix, String uri); + + public Element addProcessingInstruction( + String target, Map data); + + public Element addProcessingInstruction(String target, String data); + + public Element addText(String text); + + public void appendAttributes(Element element); + + public Attribute attribute(int index); + + public Attribute attribute(QName qName); + + public Attribute attribute(String name); + + public int attributeCount(); + + public Iterator attributeIterator(); + + public List attributes(); + + public String attributeValue(QName qName); + + public String attributeValue(QName qName, String defaultValue); + + public String attributeValue(String name); + + public String attributeValue(String name, String defaultValue); + + public Element createCopy(); + + public Element createCopy(QName qName); + + public Element createCopy(String name); + + public List declaredNamespaces(); + + public Element element(QName qName); + + public Element element(String name); + + public Iterator elementIterator(); + + public Iterator elementIterator(QName qName); + + public Iterator elementIterator(String name); + + public List elements(); + + public List elements(QName qName); + + public List elements(String name); + + public String elementText(QName qName); + + public String elementText(String name); + + public String elementTextTrim(QName qName); + + public String elementTextTrim(String name); + + public Object getData(); + + public Namespace getNamespace(); + + public Namespace getNamespaceForPrefix(String prefix); + + public Namespace getNamespaceForURI(String uri); + + public String getNamespacePrefix(); + + public List getNamespacesForURI(String uri); + + public String getNamespaceURI(); + + public QName getQName(); + + public QName getQName(String qualifiedName); + + public String getQualifiedName(); + + public String getStringValue(); + + public String getText(); + + public String getTextTrim(); + + public Node getXPathResult(int index); + + public boolean hasMixedContent(); + + public boolean isRootElement(); + + public boolean isTextOnly(); + + public boolean remove(Attribute attribute); + + public boolean remove(CDATA cdata); + + public boolean remove(Entity entity); + + public boolean remove(Namespace namespace); + + public boolean remove(Text text); + + public void setAttributes(List attributes); + + public void setData(Object data); + + public void setQName(QName qName); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Entity.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Entity.java new file mode 100644 index 000000000..b05de8866 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Entity.java @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +/** + * @author Brian Wing Shun Chan + */ +public interface Entity extends Node { +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Namespace.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Namespace.java new file mode 100644 index 000000000..f77613d01 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Namespace.java @@ -0,0 +1,30 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +/** + * @author Brian Wing Shun Chan + */ +public interface Namespace extends Node { + + public short getNodeType(); + + public String getPrefix(); + + public String getURI(); + + public String getXPathNameStep(); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Node.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Node.java new file mode 100644 index 000000000..3e67c196e --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Node.java @@ -0,0 +1,83 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +import java.io.IOException; +import java.io.Serializable; +import java.io.Writer; + +import java.util.List; + +/** + * @author Brian Wing Shun Chan + */ +public interface Node extends Serializable { + + public String asXML(); + + public Node asXPathResult(Element parent); + + public Node detach(); + + public Document getDocument(); + + public String getName(); + + public Element getParent(); + + public String getPath(); + + public String getPath(Element context); + + public String getStringValue(); + + public String getText(); + + public String getUniquePath(); + + public String getUniquePath(Element context); + + public boolean hasContent(); + + public boolean isReadOnly(); + + public boolean matches(String xpathExpression); + + public Number numberValueOf(String xpathExpression); + + public List selectNodes(String xpathExpression); + + public List selectNodes( + String xpathExpression, String comparisonXPathExpression); + + public List selectNodes( + String xpathExpression, String comparisonXPathExpression, + boolean removeDuplicates); + + public Object selectObject(String xpathExpression); + + public Node selectSingleNode(String xpathExpression); + + public void setName(String name); + + public void setText(String text); + + public boolean supportsParent(); + + public String valueOf(String xpathExpression); + + public void write(Writer writer) throws IOException; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/ProcessingInstruction.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/ProcessingInstruction.java new file mode 100644 index 000000000..11a4a90f7 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/ProcessingInstruction.java @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +import java.util.Map; + +/** + * @author Brian Wing Shun Chan + */ +public interface ProcessingInstruction extends Node { + + public String getTarget(); + + public String getText(); + + public String getValue(String name); + + public Map getValues(); + + public boolean removeValue(String name); + + public void setTarget(String target); + + public void setValue(String name, String value); + + public void setValues(Map data); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/QName.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/QName.java new file mode 100644 index 000000000..08622ddbf --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/QName.java @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +import java.io.Serializable; + +/** + * @author Brian Wing Shun Chan + */ +public interface QName extends Serializable { + + public String getLocalPart(); + + public String getName(); + + public Namespace getNamespace(); + + public String getNamespacePrefix(); + + public String getNamespaceURI(); + + public String getQualifiedName(); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/SAXReader.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/SAXReader.java new file mode 100644 index 000000000..631aea0dd --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/SAXReader.java @@ -0,0 +1,108 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +import java.io.File; +import java.io.InputStream; +import java.io.Reader; + +import java.net.MalformedURLException; +import java.net.URL; + +import java.util.List; +import java.util.Map; + +/** + * @author Brian Wing Shun Chan + */ +public interface SAXReader { + + public Attribute createAttribute( + Element element, QName qName, String value); + + public Attribute createAttribute( + Element element, String name, String value); + + public Document createDocument(); + + public Document createDocument(Element rootElement); + + public Document createDocument(String encoding); + + public Element createElement(QName qName); + + public Element createElement(String name); + + public Entity createEntity(String name, String text); + + public ProcessingInstruction createProcessingInstruction( + String target, Map data); + + public ProcessingInstruction createProcessingInstruction( + String target, String data); + + public Namespace createNamespace(String uri); + + public Namespace createNamespace(String prefix, String uri); + + public QName createQName(String localName); + + public QName createQName(String localName, Namespace namespace); + + public Text createText(String text); + + public XPath createXPath(String xpathExpression); + + public Document read(File file) throws DocumentException; + + public Document read(File file, boolean validate) + throws DocumentException; + + public Document read(InputStream is) throws DocumentException; + + public Document read(InputStream is, boolean validate) + throws DocumentException; + + public Document read(Reader reader) throws DocumentException; + + public Document read(Reader reader, boolean validate) + throws DocumentException; + + public Document read(String xml) throws DocumentException; + + public Document read(String xml, boolean validate) + throws DocumentException; + + public Document read(URL url) throws DocumentException; + + public Document read(URL url, boolean validate) throws DocumentException; + + public Document readURL(String url) + throws DocumentException, MalformedURLException; + + public Document readURL(String url, boolean validate) + throws DocumentException, MalformedURLException; + + public List selectNodes( + String xpathFilterExpression, List nodes); + + public List selectNodes(String xpathFilterExpression, Node node); + + public void sort(List nodes, String xpathExpression); + + public void sort( + List nodes, String xpathExpression, boolean distinct); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/SAXReaderUtil.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/SAXReaderUtil.java new file mode 100644 index 000000000..00f20a5b5 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/SAXReaderUtil.java @@ -0,0 +1,199 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +import java.io.File; +import java.io.InputStream; +import java.io.Reader; + +import java.net.MalformedURLException; +import java.net.URL; + +import java.util.List; +import java.util.Map; + +/** + * @author Brian Wing Shun Chan + */ +public class SAXReaderUtil { + + public static Attribute createAttribute( + Element element, QName qName, String value) { + + return getSAXReader().createAttribute(element, qName, value); + } + + public static Attribute createAttribute( + Element element, String name, String value) { + + return getSAXReader().createAttribute(element, name, value); + } + + public static Document createDocument() { + return getSAXReader().createDocument(); + } + + public static Document createDocument(Element rootElement) { + return getSAXReader().createDocument(rootElement); + } + + public static Document createDocument(String encoding) { + return getSAXReader().createDocument(encoding); + } + + public static Element createElement(QName qName) { + return getSAXReader().createElement(qName); + } + + public static Element createElement(String name) { + return getSAXReader().createElement(name); + } + + public static Entity createEntity(String name, String text) { + return getSAXReader().createEntity(name, text); + } + + public static Namespace createNamespace(String uri) { + return getSAXReader().createNamespace(uri); + } + + public static Namespace createNamespace(String prefix, String uri) { + return getSAXReader().createNamespace(prefix, uri); + } + + public static ProcessingInstruction createProcessingInstruction( + String target, Map data) { + + return getSAXReader().createProcessingInstruction(target, data); + } + + public static ProcessingInstruction createProcessingInstruction( + String target, String data) { + + return getSAXReader().createProcessingInstruction(target, data); + } + + public static QName createQName(String localName) { + return getSAXReader().createQName(localName); + } + + public static QName createQName(String localName, Namespace namespace) { + return getSAXReader().createQName(localName, namespace); + } + + public static Text createText(String text) { + return getSAXReader().createText(text); + } + + public static XPath createXPath(String xpathExpression) { + return getSAXReader().createXPath(xpathExpression); + } + + public static SAXReader getSAXReader() { + return _saxReader; + } + + public static Document read(File file) throws DocumentException { + return getSAXReader().read(file); + } + + public static Document read(File file, boolean validate) + throws DocumentException { + + return getSAXReader().read(file, validate); + } + + public static Document read(InputStream is) throws DocumentException { + return getSAXReader().read(is); + } + + public static Document read(InputStream is, boolean validate) + throws DocumentException { + + return getSAXReader().read(is, validate); + } + + public static Document read(Reader reader) throws DocumentException { + return getSAXReader().read(reader); + } + + public static Document read(Reader reader, boolean validate) + throws DocumentException { + + return getSAXReader().read(reader, validate); + } + + public static Document read(String xml) throws DocumentException { + return getSAXReader().read(xml); + } + + public static Document read(String xml, boolean validate) + throws DocumentException { + + return getSAXReader().read(xml, validate); + } + + public static Document read(URL url) throws DocumentException { + return getSAXReader().read(url); + } + + public static Document read(URL url, boolean validate) + throws DocumentException { + + return getSAXReader().read(url, validate); + } + + public static Document readURL(String url) + throws DocumentException, MalformedURLException { + + return getSAXReader().readURL(url); + } + + public static Document readURL(String url, boolean validate) + throws DocumentException, MalformedURLException { + + return getSAXReader().readURL(url, validate); + } + + public static List selectNodes( + String xpathFilterExpression, List nodes) { + + return getSAXReader().selectNodes(xpathFilterExpression, nodes); + } + + public static List selectNodes( + String xpathFilterExpression, Node node) { + + return getSAXReader().selectNodes(xpathFilterExpression, node); + } + + public static void sort(List nodes, String xpathExpression) { + + getSAXReader().sort(nodes, xpathExpression); + } + + public static void sort( + List nodes, String xpathExpression, boolean distinct) { + + getSAXReader().sort(nodes, xpathExpression, distinct); + } + + public void setSAXReader(SAXReader saxReader) { + _saxReader = saxReader; + } + + private static SAXReader _saxReader; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Text.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Text.java new file mode 100644 index 000000000..20c5b5892 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/Text.java @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +/** + * @author Brian Wing Shun Chan + */ +public interface Text extends Node { +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/XPath.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/XPath.java new file mode 100644 index 000000000..c5c576cde --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/kernel/xml/XPath.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.kernel.xml; + +import java.io.Serializable; + +import java.util.List; + +/** + * @author Brian Wing Shun Chan + */ +public interface XPath extends Serializable { + + public boolean booleanValueOf(Object context); + + public Object evaluate(Object context); + + public String getText(); + + public boolean matches(Node node); + + public Number numberValueOf(Object context); + + public List selectNodes(Object context); + + public List selectNodes(Object context, XPath sortXPath); + + public List selectNodes( + Object context, XPath sortXPath, boolean distinct); + + public Node selectSingleNode(Object context); + + public void sort(List nodes); + + public void sort(List nodes, boolean distinct); + + public String valueOf(Object context); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/util/EntityResolver.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/util/EntityResolver.java new file mode 100644 index 000000000..7943c466f --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/util/EntityResolver.java @@ -0,0 +1,480 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.util; + +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; +import com.liferay.portal.kernel.util.KeyValuePair; + +import java.io.InputStream; + +import org.xml.sax.InputSource; + +/** + * @author Brian Wing Shun Chan + */ +public class EntityResolver implements org.xml.sax.EntityResolver { + + public InputSource resolveEntity(String publicId, String systemId) { + ClassLoader classLoader = getClass().getClassLoader(); + + if (_log.isDebugEnabled()) { + _log.debug("Resolving entity " + publicId + " " + systemId); + } + + if (publicId != null) { + for (int i = 0; i < _PUBLIC_IDS.length; i++) { + KeyValuePair kvp = _PUBLIC_IDS[i]; + + if (publicId.equals(kvp.getKey())) { + InputStream is = classLoader.getResourceAsStream( + _DEFINITIONS_PATH + kvp.getValue()); + + if (_log.isDebugEnabled()) { + _log.debug("Entity found for public id " + systemId); + } + + return new InputSource(is); + } + } + } + else if (systemId != null) { + for (int i = 0; i < _SYSTEM_IDS.length; i++) { + KeyValuePair kvp = _SYSTEM_IDS[i]; + + if (systemId.equals(kvp.getKey())) { + InputStream is = classLoader.getResourceAsStream( + _DEFINITIONS_PATH + kvp.getValue()); + + if (_log.isDebugEnabled()) { + _log.debug("Entity found for system id " + systemId); + } + + return new InputSource(is); + } + } + } + + if (_log.isDebugEnabled()) { + _log.debug("No entity found for " + publicId + " " + systemId); + } + + return null; + } + + private static String _DEFINITIONS_PATH = "com/liferay/portal/definitions/"; + + private static KeyValuePair[] _PUBLIC_IDS = { + new KeyValuePair( + "datatypes", + "datatypes.dtd" + ), + + new KeyValuePair( + "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN", + "facelet-taglib_1_0.dtd" + ), + + new KeyValuePair( + "-//Hibernate/Hibernate Mapping DTD 3.0//EN", + "hibernate-mapping-3.0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Display 2.0.0//EN", + "liferay-display_2_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Display 3.5.0//EN", + "liferay-display_3_5_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Display 4.0.0//EN", + "liferay-display_4_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Display 5.0.0//EN", + "liferay-display_5_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Display 5.1.0//EN", + "liferay-display_5_1_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Display 5.2.0//EN", + "liferay-display_5_2_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Display 6.0.0//EN", + "liferay-display_6_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Friendly URL Routes 6.0.0//EN", + "liferay-friendly-url-routes_6_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Hook 5.1.0//EN", + "liferay-hook_5_1_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Hook 5.2.0//EN", + "liferay-hook_5_2_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Hook 6.0.0//EN", + "liferay-hook_6_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Layout Templates 3.6.0//EN", + "liferay-layout-templates_3_6_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Layout Templates 4.0.0//EN", + "liferay-layout-templates_4_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Layout Templates 4.3.0//EN", + "liferay-layout-templates_4_3_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Layout Templates 5.0.0//EN", + "liferay-layout-templates_5_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Layout Templates 5.1.0//EN", + "liferay-layout-templates_5_1_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Layout Templates 5.2.0//EN", + "liferay-layout-templates_5_2_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Layout Templates 6.0.0//EN", + "liferay-layout-templates_6_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Look and Feel 3.5.0//EN", + "liferay-look-and-feel_3_5_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Look and Feel 4.0.0//EN", + "liferay-look-and-feel_4_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Look and Feel 4.3.0//EN", + "liferay-look-and-feel_4_3_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Look and Feel 5.0.0//EN", + "liferay-look-and-feel_5_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Look and Feel 5.1.0//EN", + "liferay-look-and-feel_5_1_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Look and Feel 5.2.0//EN", + "liferay-look-and-feel_5_2_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Look and Feel 6.0.0//EN", + "liferay-look-and-feel_6_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Plugin Package 4.3.0//EN", + "liferay-plugin-package_4_3_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Plugin Package 5.0.0//EN", + "liferay-plugin-package_5_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Plugin Package 5.1.0//EN", + "liferay-plugin-package_5_1_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Plugin Package 5.2.0//EN", + "liferay-plugin-package_5_2_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Plugin Package 6.0.0//EN", + "liferay-plugin-package_6_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Plugin Repository 4.3.0//EN", + "liferay-plugin-repository_4_3_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Plugin Repository 5.0.0//EN", + "liferay-plugin-repository_5_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Plugin Repository 5.1.0//EN", + "liferay-plugin-repository_5_1_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Plugin Repository 5.2.0//EN", + "liferay-plugin-repository_5_2_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Plugin Repository 6.0.0//EN", + "liferay-plugin-repository_6_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Portlet Application 3.5.0//EN", + "liferay-portlet-app_3_5_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Portlet Application 4.0.0//EN", + "liferay-portlet-app_4_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Portlet Application 4.1.0//EN", + "liferay-portlet-app_4_1_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Portlet Application 4.2.0//EN", + "liferay-portlet-app_4_2_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Portlet Application 4.3.0//EN", + "liferay-portlet-app_4_3_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Portlet Application 4.3.1//EN", + "liferay-portlet-app_4_3_1.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Portlet Application 4.3.2//EN", + "liferay-portlet-app_4_3_2.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Portlet Application 4.3.3//EN", + "liferay-portlet-app_4_3_3.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Portlet Application 4.3.6//EN", + "liferay-portlet-app_4_3_6.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Portlet Application 4.4.0//EN", + "liferay-portlet-app_4_4_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Portlet Application 5.0.0//EN", + "liferay-portlet-app_5_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Portlet Application 5.1.0//EN", + "liferay-portlet-app_5_1_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Portlet Application 5.2.0//EN", + "liferay-portlet-app_5_2_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Portlet Application 6.0.0//EN", + "liferay-portlet-app_6_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Service Builder 3.5.0//EN", + "liferay-service-builder_3_5_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Service Builder 3.6.1//EN", + "liferay-service-builder_3_6_1.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Service Builder 4.0.0//EN", + "liferay-service-builder_4_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Service Builder 4.2.0//EN", + "liferay-service-builder_4_2_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Service Builder 4.3.0//EN", + "liferay-service-builder_4_3_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Service Builder 4.3.3//EN", + "liferay-service-builder_4_3_3.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Service Builder 4.4.0//EN", + "liferay-service-builder_4_4_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Service Builder 5.0.0//EN", + "liferay-service-builder_5_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Service Builder 5.1.0//EN", + "liferay-service-builder_5_1_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Service Builder 5.2.0//EN", + "liferay-service-builder_5_2_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Service Builder 6.0.0//EN", + "liferay-service-builder_6_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Theme Loader 4.3.0//EN", + "liferay-theme-loader_4_3_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Theme Loader 5.0.0//EN", + "liferay-theme-loader_5_0_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Theme Loader 5.1.0//EN", + "liferay-theme-loader_5_1_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Theme Loader 5.2.0//EN", + "liferay-theme-loader_5_2_0.dtd" + ), + + new KeyValuePair( + "-//Liferay//DTD Theme Loader 6.0.0//EN", + "liferay-theme-loader_6_0_0.dtd" + ), + + new KeyValuePair( + "-//MuleSource //DTD mule-configuration XML V1.0//EN", + "mule-configuration.dtd" + ), + + new KeyValuePair( + "-//SPRING//DTD BEAN//EN", + "spring-beans.dtd" + ), + + new KeyValuePair( + "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN", + "struts-config_1_2.dtd" + ), + + new KeyValuePair( + "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN", + "tiles-config_1_1.dtd" + ), + + new KeyValuePair( + "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN", + "web-app_2_3.dtd" + ), + + new KeyValuePair( + "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN", + "web-facesconfig_1_0.dtd" + ), + + new KeyValuePair( + "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN", + "web-facesconfig_1_1.dtd" + ), + + new KeyValuePair( + "-//W3C//DTD XMLSCHEMA 200102//EN", + "XMLSchema.dtd" + ) + }; + + private static KeyValuePair[] _SYSTEM_IDS = { + new KeyValuePair( + "http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd", + "portlet-app_1_0.xsd" + ), + + new KeyValuePair( + "http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd", + "portlet-app_2_0.xsd" + ), + + new KeyValuePair( + "http://www.w3.org/2001/xml.xsd", + "xml.xsd" + ) + }; + + private static Log _log = LogFactoryUtil.getLog(EntityResolver.class); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/AttributeImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/AttributeImpl.java new file mode 100644 index 000000000..9ddd4ff63 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/AttributeImpl.java @@ -0,0 +1,109 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.xml.Attribute; +import com.liferay.portal.kernel.xml.Namespace; +import com.liferay.portal.kernel.xml.QName; + +/** + * @author Brian Wing Shun Chan + */ +public class AttributeImpl extends NodeImpl implements Attribute { + + public AttributeImpl(org.dom4j.Attribute attribute) { + super(attribute); + + _attribute = attribute; + } + + public Object getData() { + return _attribute.getData(); + } + + public boolean equals(Object obj) { + org.dom4j.Attribute attribute = + ((AttributeImpl)obj).getWrappedAttribute(); + + return _attribute.equals(attribute); + } + + public Namespace getNamespace() { + org.dom4j.Namespace namespace = _attribute.getNamespace(); + + if (namespace == null) { + return null; + } + else { + return new NamespaceImpl(namespace); + } + } + + public String getNamespacePrefix() { + return _attribute.getNamespacePrefix(); + } + + public String getNamespaceURI() { + return _attribute.getNamespaceURI(); + } + + public QName getQName() { + org.dom4j.QName qName = _attribute.getQName(); + + if (qName == null) { + return null; + } + else { + return new QNameImpl(qName); + } + } + + public String getQualifiedName() { + return _attribute.getQualifiedName(); + } + + public String getValue() { + return _attribute.getValue(); + } + + public org.dom4j.Attribute getWrappedAttribute() { + return _attribute; + } + + public int hashCode() { + return _attribute.hashCode(); + } + + public void setData(Object data) { + _attribute.setData(data); + } + + public void setNamespace(Namespace namespace) { + NamespaceImpl namespaceImpl = (NamespaceImpl)namespace; + + _attribute.setNamespace(namespaceImpl.getWrappedNamespace()); + } + + public void setValue(String value) { + _attribute.setValue(value); + } + + public String toString() { + return _attribute.toString(); + } + + private org.dom4j.Attribute _attribute; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/BranchImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/BranchImpl.java new file mode 100644 index 000000000..5fcd48f29 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/BranchImpl.java @@ -0,0 +1,213 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.xml.Branch; +import com.liferay.portal.kernel.xml.Comment; +import com.liferay.portal.kernel.xml.Element; +import com.liferay.portal.kernel.xml.Node; +import com.liferay.portal.kernel.xml.ProcessingInstruction; +import com.liferay.portal.kernel.xml.QName; + +import java.util.Iterator; +import java.util.List; + +/** + * @author Brian Wing Shun Chan + */ +public class BranchImpl extends NodeImpl implements Branch { + + public BranchImpl(org.dom4j.Branch branch) { + super(branch); + + _branch = branch; + } + + public void add(Comment comment) { + CommentImpl commentImpl = (CommentImpl)comment; + + _branch.add(commentImpl.getWrappedComment()); + } + + public void add(Element element) { + ElementImpl elementImpl = (ElementImpl)element; + + _branch.add(elementImpl.getWrappedElement()); + } + + public void add(Node node) { + NodeImpl nodeImpl = (NodeImpl)node; + + _branch.add(nodeImpl.getWrappedNode()); + } + + public void add(ProcessingInstruction processingInstruction) { + ProcessingInstructionImpl processingInstructionImpl = + (ProcessingInstructionImpl)processingInstruction; + + _branch.add( + processingInstructionImpl.getWrappedProcessingInstruction()); + } + + public Element addElement(QName qName) { + QNameImpl qNameImpl = (QNameImpl)qName; + + return new ElementImpl(_branch.addElement(qNameImpl.getWrappedQName())); + } + + public Element addElement(String name) { + return new ElementImpl(_branch.addElement(name)); + } + + public Element addElement(String qualifiedName, String namespaceURI) { + return new ElementImpl(_branch.addElement(qualifiedName, namespaceURI)); + } + + public void appendContent(Branch branch) { + BranchImpl branchImpl = (BranchImpl)branch; + + _branch.appendContent(branchImpl.getWrappedBranch()); + } + + public void clearContent() { + _branch.clearContent(); + } + + public List content() { + return SAXReaderImpl.toNewNodes(_branch.content()); + } + + public Element elementByID(String elementID) { + return new ElementImpl(_branch.elementByID(elementID)); + } + + public boolean equals(Object obj) { + org.dom4j.Branch branch = ((BranchImpl)obj).getWrappedBranch(); + + return _branch.equals(branch); + } + + public org.dom4j.Branch getWrappedBranch() { + return _branch; + } + + public int hashCode() { + return _branch.hashCode(); + } + + public int indexOf(Node node) { + NodeImpl nodeImpl = (NodeImpl)node; + + return _branch.indexOf(nodeImpl.getWrappedNode()); + } + + public Node node(int index) { + org.dom4j.Node node = _branch.node(index); + + if (node == null) { + return null; + } + else { + if (node instanceof org.dom4j.Element) { + return new ElementImpl((org.dom4j.Element)node); + } + else { + return new NodeImpl(node); + } + } + } + + public int nodeCount() { + return _branch.nodeCount(); + } + + public Iterator nodeIterator() { + return content().iterator(); + } + + public void normalize() { + _branch.normalize(); + } + + public ProcessingInstruction processingInstruction(String target) { + org.dom4j.ProcessingInstruction processingInstruction = + _branch.processingInstruction(target); + + if (processingInstruction == null) { + return null; + } + else { + return new ProcessingInstructionImpl(processingInstruction); + } + } + + public List processingInstructions() { + return SAXReaderImpl.toNewProcessingInstructions( + _branch.processingInstructions()); + } + + public List processingInstructions(String target) { + return SAXReaderImpl.toNewProcessingInstructions( + _branch.processingInstructions(target)); + } + + public boolean remove(Comment comment) { + CommentImpl commentImpl = (CommentImpl)comment; + + return _branch.remove(commentImpl.getWrappedComment()); + } + + public boolean remove(Element element) { + ElementImpl elementImpl = (ElementImpl)element; + + return _branch.remove(elementImpl.getWrappedElement()); + } + + public boolean remove(Node node) { + NodeImpl nodeImpl = (NodeImpl)node; + + return _branch.remove(nodeImpl.getWrappedNode()); + } + + public boolean remove(ProcessingInstruction processingInstruction) { + ProcessingInstructionImpl processingInstructionImpl = + (ProcessingInstructionImpl)processingInstruction; + + return _branch.remove( + processingInstructionImpl.getWrappedProcessingInstruction()); + } + + public boolean removeProcessingInstruction(String target) { + return _branch.removeProcessingInstruction(target); + } + + public void setContent(List content) { + _branch.setContent(SAXReaderImpl.toOldNodes(content)); + } + + public void setProcessingInstructions( + List processingInstructions) { + + _branch.setProcessingInstructions( + SAXReaderImpl.toOldProcessingInstructions(processingInstructions)); + } + + public String toString() { + return _branch.toString(); + } + + private org.dom4j.Branch _branch; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/CDATAImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/CDATAImpl.java new file mode 100644 index 000000000..829a1c706 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/CDATAImpl.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.xml.CDATA; + +/** + * @author Brian Wing Shun Chan + */ +public class CDATAImpl extends NodeImpl implements CDATA { + + public CDATAImpl(org.dom4j.CDATA cdata) { + super(cdata); + + _cdata = cdata; + } + + public boolean equals(Object obj) { + org.dom4j.CDATA cdata = ((CDATAImpl)obj).getWrappedCDATA(); + + return _cdata.equals(cdata); + } + + public org.dom4j.CDATA getWrappedCDATA() { + return _cdata; + } + + public int hashCode() { + return _cdata.hashCode(); + } + + public String toString() { + return _cdata.toString(); + } + + private org.dom4j.CDATA _cdata; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/CommentImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/CommentImpl.java new file mode 100644 index 000000000..cd0ac1e16 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/CommentImpl.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.xml.Comment; + +/** + * @author Brian Wing Shun Chan + */ +public class CommentImpl extends NodeImpl implements Comment { + + public CommentImpl(org.dom4j.Comment comment) { + super(comment); + + _comment = comment; + } + + public boolean equals(Object obj) { + org.dom4j.Comment comment = ((CommentImpl)obj).getWrappedComment(); + + return _comment.equals(comment); + } + + public org.dom4j.Comment getWrappedComment() { + return _comment; + } + + public int hashCode() { + return _comment.hashCode(); + } + + public String toString() { + return _comment.toString(); + } + + private org.dom4j.Comment _comment; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/DocumentImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/DocumentImpl.java new file mode 100644 index 000000000..a41738d4c --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/DocumentImpl.java @@ -0,0 +1,98 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.xml.Document; +import com.liferay.portal.kernel.xml.Element; +import com.liferay.util.xml.XMLFormatter; + +import java.io.IOException; + +/** + * @author Brian Wing Shun Chan + */ +public class DocumentImpl extends BranchImpl implements Document { + + public DocumentImpl(org.dom4j.Document document) { + super(document); + + _document = document; + } + + public Document addComment(String comment) { + _document.addComment(comment); + + return this; + } + + public Document addDocType(String name, String publicId, String systemId) { + _document.addDocType(name, publicId, systemId); + + return this; + } + + public boolean equals(Object obj) { + org.dom4j.Document document = ((DocumentImpl)obj).getWrappedDocument(); + + return _document.equals(document); + } + + public String formattedString() throws IOException { + return XMLFormatter.toString(_document); + } + + public String formattedString(String indent) throws IOException { + return XMLFormatter.toString(_document, indent); + } + + public String formattedString(String indent, boolean expandEmptyElements) + throws IOException { + + return XMLFormatter.toString(_document, indent, expandEmptyElements); + } + + public Element getRootElement() { + return new ElementImpl(_document.getRootElement()); + } + + public org.dom4j.Document getWrappedDocument() { + return _document; + } + + public String getXMLEncoding() { + return _document.getXMLEncoding(); + } + + public int hashCode() { + return _document.hashCode(); + } + + public void setRootElement(Element rootElement) { + ElementImpl rootElementImpl = (ElementImpl)rootElement; + + _document.setRootElement(rootElementImpl.getWrappedElement()); + } + + public void setXMLEncoding(String encoding) { + _document.setXMLEncoding(encoding); + } + + public String toString() { + return _document.toString(); + } + + private org.dom4j.Document _document; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/ElementImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/ElementImpl.java new file mode 100644 index 000000000..523932f93 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/ElementImpl.java @@ -0,0 +1,451 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.util.StringUtil; +import com.liferay.portal.kernel.xml.Attribute; +import com.liferay.portal.kernel.xml.CDATA; +import com.liferay.portal.kernel.xml.Element; +import com.liferay.portal.kernel.xml.Entity; +import com.liferay.portal.kernel.xml.Namespace; +import com.liferay.portal.kernel.xml.Node; +import com.liferay.portal.kernel.xml.QName; +import com.liferay.portal.kernel.xml.Text; + +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +/** + * @author Brian Wing Shun Chan + */ +public class ElementImpl extends BranchImpl implements Element { + + public ElementImpl(org.dom4j.Element element) { + super(element); + + _element = element; + } + + public void add(Attribute attribute) { + AttributeImpl attributeImpl = (AttributeImpl)attribute; + + _element.add(attributeImpl.getWrappedAttribute()); + } + + public void add(CDATA cdata) { + CDATAImpl cdataImpl = (CDATAImpl)cdata; + + _element.add(cdataImpl.getWrappedCDATA()); + } + + public void add(Entity entity) { + EntityImpl entityImpl = (EntityImpl)entity; + + _element.add(entityImpl.getWrappedEntity()); + } + + public void add(Namespace namespace) { + NamespaceImpl namespaceImpl = (NamespaceImpl)namespace; + + _element.add(namespaceImpl.getWrappedNamespace()); + } + + public void add(Text text) { + TextImpl textImpl = (TextImpl)text; + + _element.add(textImpl.getWrappedText()); + } + + public Element addAttribute(QName qName, String value) { + QNameImpl qNameImpl = (QNameImpl)qName; + + return new ElementImpl( + _element.addAttribute(qNameImpl.getWrappedQName(), value)); + } + + public Element addAttribute(String name, String value) { + return new ElementImpl(_element.addAttribute(name, value)); + } + + public Element addCDATA(String cdata) { + cdata = StringUtil.replace(cdata, "]]>", "]]]]>"); + + return new ElementImpl(_element.addCDATA(cdata)); + } + + public Element addComment(String comment) { + return new ElementImpl(_element.addComment(comment)); + } + + public Element addEntity(String name, String text) { + return new ElementImpl(_element.addEntity(name, text)); + } + + public Element addNamespace(String prefix, String uri) { + return new ElementImpl(_element.addNamespace(prefix, uri)); + } + + public Element addProcessingInstruction( + String target, Map data) { + + return new ElementImpl(_element.addProcessingInstruction(target, data)); + } + + public Element addProcessingInstruction(String target, String data) { + return new ElementImpl(_element.addProcessingInstruction(target, data)); + } + + public Element addText(String text) { + return new ElementImpl(_element.addText(text)); + } + + public List additionalNamespaces() { + return SAXReaderImpl.toNewNamespaces(_element.additionalNamespaces()); + } + + public void appendAttributes(Element element) { + ElementImpl elementImpl = (ElementImpl)element; + + _element.appendAttributes(elementImpl.getWrappedElement()); + } + + public Attribute attribute(int index) { + org.dom4j.Attribute attribute = _element.attribute(index); + + if (attribute == null) { + return null; + } + else { + return new AttributeImpl(attribute); + } + } + + public Attribute attribute(QName qName) { + QNameImpl qNameImpl = (QNameImpl)qName; + + org.dom4j.Attribute attribute = _element.attribute( + qNameImpl.getWrappedQName()); + + if (attribute == null) { + return null; + } + else { + return new AttributeImpl(attribute); + } + } + + public Attribute attribute(String name) { + org.dom4j.Attribute attribute = _element.attribute(name); + + if (attribute == null) { + return null; + } + else { + return new AttributeImpl(attribute); + } + } + + public int attributeCount() { + return _element.attributeCount(); + } + + public Iterator attributeIterator() { + return attributes().iterator(); + } + + public String attributeValue(QName qName) { + QNameImpl qNameImpl = (QNameImpl)qName; + + return _element.attributeValue(qNameImpl.getWrappedQName()); + } + + public String attributeValue(QName qName, String defaultValue) { + QNameImpl qNameImpl = (QNameImpl)qName; + + return _element.attributeValue( + qNameImpl.getWrappedQName(), defaultValue); + } + + public String attributeValue(String name) { + return _element.attributeValue(name); + } + + public String attributeValue(String name, String defaultValue) { + return _element.attributeValue(name, defaultValue); + } + + public List attributes() { + return SAXReaderImpl.toNewAttributes(_element.attributes()); + } + + public Element createCopy() { + return new ElementImpl(_element.createCopy()); + } + + public Element createCopy(QName qName) { + QNameImpl qNameImpl = (QNameImpl)qName; + + return new ElementImpl( + _element.createCopy(qNameImpl.getWrappedQName())); + } + + public Element createCopy(String name) { + return new ElementImpl(_element.createCopy(name)); + } + + public List declaredNamespaces() { + return SAXReaderImpl.toNewNamespaces(_element.declaredNamespaces()); + } + + public Element element(QName qName) { + QNameImpl qNameImpl = (QNameImpl)qName; + + org.dom4j.Element element = _element.element( + qNameImpl.getWrappedQName()); + + if (element == null) { + return null; + } + else { + return new ElementImpl(element); + } + } + + public Element element(String name) { + org.dom4j.Element element = _element.element(name); + + if (element == null) { + return null; + } + else { + return new ElementImpl(element); + } + } + + public Iterator elementIterator() { + return elements().iterator(); + } + + public Iterator elementIterator(QName qName) { + return elements(qName).iterator(); + } + + public Iterator elementIterator(String name) { + return elements(name).iterator(); + } + + public String elementText(QName qName) { + QNameImpl qNameImpl = (QNameImpl)qName; + + return _element.elementText(qNameImpl.getWrappedQName()); + } + + public String elementText(String name) { + return _element.elementText(name); + } + + public String elementTextTrim(QName qName) { + QNameImpl qNameImpl = (QNameImpl)qName; + + return _element.elementTextTrim(qNameImpl.getWrappedQName()); + } + + public String elementTextTrim(String name) { + return _element.elementTextTrim(name); + } + + public List elements() { + return SAXReaderImpl.toNewElements(_element.elements()); + } + + public List elements(QName qName) { + QNameImpl qNameImpl = (QNameImpl)qName; + + return SAXReaderImpl.toNewElements( + _element.elements(qNameImpl.getWrappedQName())); + } + + public List elements(String name) { + return SAXReaderImpl.toNewElements(_element.elements(name)); + } + + public boolean equals(Object obj) { + org.dom4j.Element element = ((ElementImpl)obj).getWrappedElement(); + + return _element.equals(element); + } + + public Object getData() { + return _element.getData(); + } + + public Namespace getNamespace() { + org.dom4j.Namespace namespace = _element.getNamespace(); + + if (namespace == null) { + return null; + } + else { + return new NamespaceImpl(namespace); + } + } + + public Namespace getNamespaceForPrefix(String prefix) { + org.dom4j.Namespace namespace = _element.getNamespaceForPrefix(prefix); + + if (namespace == null) { + return null; + } + else { + return new NamespaceImpl(namespace); + } + } + + public Namespace getNamespaceForURI(String uri) { + org.dom4j.Namespace namespace = _element.getNamespaceForURI(uri); + + if (namespace == null) { + return null; + } + else { + return new NamespaceImpl(namespace); + } + } + + public String getNamespacePrefix() { + return _element.getNamespacePrefix(); + } + + public String getNamespaceURI() { + return _element.getNamespaceURI(); + } + + public List getNamespacesForURI(String uri) { + return SAXReaderImpl.toNewNamespaces(_element.getNamespacesForURI(uri)); + } + + public QName getQName() { + org.dom4j.QName qName = _element.getQName(); + + if (qName == null) { + return null; + } + else { + return new QNameImpl(qName); + } + } + + public QName getQName(String qualifiedName) { + org.dom4j.QName qName = _element.getQName(qualifiedName); + + if (qName == null) { + return null; + } + else { + return new QNameImpl(qName); + } + } + + public String getQualifiedName() { + return _element.getQualifiedName(); + } + + public String getTextTrim() { + return _element.getTextTrim(); + } + + public org.dom4j.Element getWrappedElement() { + return _element; + } + + public Node getXPathResult(int index) { + org.dom4j.Node node = _element.getXPathResult(index); + + if (node == null) { + return null; + } + else { + return new NodeImpl(node); + } + } + + public int hashCode() { + return _element.hashCode(); + } + + public boolean hasMixedContent() { + return _element.hasMixedContent(); + } + + public boolean isRootElement() { + return _element.isRootElement(); + } + + public boolean isTextOnly() { + return _element.isTextOnly(); + } + + public boolean remove(Attribute attribute) { + AttributeImpl attributeImpl = (AttributeImpl)attribute; + + return _element.remove(attributeImpl.getWrappedAttribute()); + } + + public boolean remove(CDATA cdata) { + CDATAImpl cdataImpl = (CDATAImpl)cdata; + + return _element.remove(cdataImpl.getWrappedCDATA()); + } + + public boolean remove(Entity entity) { + EntityImpl entityImpl = (EntityImpl)entity; + + return _element.remove(entityImpl.getWrappedEntity()); + } + + public boolean remove(Namespace namespace) { + NamespaceImpl namespaceImpl = (NamespaceImpl)namespace; + + return _element.remove(namespaceImpl.getWrappedNamespace()); + } + + public boolean remove(Text text) { + TextImpl textImpl = (TextImpl)text; + + return _element.remove(textImpl.getWrappedText()); + } + + public void setAttributes(List attributes) { + _element.setAttributes(SAXReaderImpl.toOldAttributes(attributes)); + } + + public void setData(Object data) { + _element.setData(data); + } + + public void setQName(QName qName) { + QNameImpl qNameImpl = (QNameImpl)qName; + + _element.setQName(qNameImpl.getWrappedQName()); + } + + public String toString() { + return _element.toString(); + } + + private org.dom4j.Element _element; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/EntityImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/EntityImpl.java new file mode 100644 index 000000000..d145d0fa6 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/EntityImpl.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.xml.Entity; + +/** + * @author Brian Wing Shun Chan + */ +public class EntityImpl extends NodeImpl implements Entity { + + public EntityImpl(org.dom4j.Entity entity) { + super(entity); + + _entity = entity; + } + + public boolean equals(Object obj) { + org.dom4j.Entity entity = ((EntityImpl)obj).getWrappedEntity(); + + return _entity.equals(entity); + } + + public org.dom4j.Entity getWrappedEntity() { + return _entity; + } + + public int hashCode() { + return _entity.hashCode(); + } + + public String toString() { + return _entity.toString(); + } + + private org.dom4j.Entity _entity; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/NamespaceImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/NamespaceImpl.java new file mode 100644 index 000000000..73ca69f34 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/NamespaceImpl.java @@ -0,0 +1,67 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.xml.Namespace; + +/** + * @author Brian Wing Shun Chan + */ +public class NamespaceImpl extends NodeImpl implements Namespace { + + public NamespaceImpl(org.dom4j.Namespace namespace) { + super(namespace); + + _namespace = namespace; + } + + public boolean equals(Object obj) { + org.dom4j.Namespace namespace = + ((NamespaceImpl)obj).getWrappedNamespace(); + + return _namespace.equals(namespace); + } + + public short getNodeType() { + return _namespace.getNodeType(); + } + + public String getPrefix() { + return _namespace.getPrefix(); + } + + public String getURI() { + return _namespace.getURI(); + } + + public org.dom4j.Namespace getWrappedNamespace() { + return _namespace; + } + + public String getXPathNameStep() { + return _namespace.getXPathNameStep(); + } + + public int hashCode() { + return _namespace.hashCode(); + } + + public String toString() { + return _namespace.toString(); + } + + private org.dom4j.Namespace _namespace; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/NodeImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/NodeImpl.java new file mode 100644 index 000000000..62006e0ca --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/NodeImpl.java @@ -0,0 +1,228 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.xml.Document; +import com.liferay.portal.kernel.xml.Element; +import com.liferay.portal.kernel.xml.Node; + +import java.io.IOException; +import java.io.Writer; + +import java.util.List; + +/** + * @author Brian Wing Shun Chan + */ +public class NodeImpl implements Node { + + public NodeImpl(org.dom4j.Node node) { + _node = node; + } + + public String asXML() { + return _node.asXML(); + } + + public Node asXPathResult(Element parent) { + ElementImpl parentImpl = (ElementImpl)parent; + + org.dom4j.Node node = _node.asXPathResult( + parentImpl.getWrappedElement()); + + if (node == null) { + return null; + } + if (node instanceof org.dom4j.Element) { + return new ElementImpl((org.dom4j.Element)node); + } + else { + return new NodeImpl(node); + } + } + + public Node detach() { + org.dom4j.Node node = _node.detach(); + + if (node == null) { + return null; + } + if (node instanceof org.dom4j.Element) { + return new ElementImpl((org.dom4j.Element)node); + } + else { + return new NodeImpl(node); + } + } + + public boolean equals(Object obj) { + org.dom4j.Node node = ((NodeImpl)obj).getWrappedNode(); + + return _node.equals(node); + } + + public Document getDocument() { + org.dom4j.Document document = _node.getDocument(); + + if (document == null) { + return null; + } + else { + return new DocumentImpl(document); + } + } + + public String getName() { + return _node.getName(); + } + + public Element getParent() { + org.dom4j.Element element = _node.getParent(); + + if (element == null) { + return null; + } + else { + return new ElementImpl(element); + } + } + + public String getPath() { + return _node.getPath(); + } + + public String getPath(Element context) { + ElementImpl contextImpl = (ElementImpl)context; + + return _node.getPath(contextImpl.getWrappedElement()); + } + + public String getStringValue() { + return _node.getStringValue(); + } + + public String getText() { + return _node.getText(); + } + + public String getUniquePath() { + return _node.getUniquePath(); + } + + public String getUniquePath(Element context) { + ElementImpl contextImpl = (ElementImpl)context; + + return _node.getUniquePath(contextImpl.getWrappedElement()); + } + + public org.dom4j.Node getWrappedNode() { + return _node; + } + + public boolean hasContent() { + return _node.hasContent(); + } + + public int hashCode() { + return _node.hashCode(); + } + + public boolean isReadOnly() { + return _node.isReadOnly(); + } + + public boolean matches(String xpathExpression) { + return _node.matches(xpathExpression); + } + + public Number numberValueOf(String xpathExpression) { + return _node.numberValueOf(xpathExpression); + } + + public List selectNodes(String xpathExpression) { + return SAXReaderImpl.toNewNodes(_node.selectNodes(xpathExpression)); + } + + public List selectNodes( + String xpathExpression, String comparisonXPathExpression) { + + return SAXReaderImpl.toNewNodes( + _node.selectNodes(xpathExpression, comparisonXPathExpression)); + } + + public List selectNodes( + String xpathExpression, String comparisonXPathExpression, + boolean removeDuplicates) { + + return SAXReaderImpl.toNewNodes( + _node.selectNodes( + xpathExpression, comparisonXPathExpression, removeDuplicates)); + } + + public Object selectObject(String xpathExpression) { + Object obj = _node.selectObject(xpathExpression); + + if (obj == null) { + return null; + } + else if (obj instanceof List) { + return SAXReaderImpl.toNewNodes((List)obj); + } + else { + return obj; + } + } + + public Node selectSingleNode(String xpathExpression) { + org.dom4j.Node node = _node.selectSingleNode(xpathExpression); + + if (node == null) { + return null; + } + if (node instanceof org.dom4j.Element) { + return new ElementImpl((org.dom4j.Element)node); + } + else { + return new NodeImpl(node); + } + } + + public void setName(String name) { + _node.setName(name); + } + + public void setText(String text) { + _node.setText(text); + } + + public boolean supportsParent() { + return _node.supportsParent(); + } + + public String toString() { + return _node.toString(); + } + + public String valueOf(String xpathExpression) { + return _node.valueOf(xpathExpression); + } + + public void write(Writer writer) throws IOException { + _node.write(writer); + } + + private org.dom4j.Node _node; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/NodeList.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/NodeList.java new file mode 100644 index 000000000..7c3f81894 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/NodeList.java @@ -0,0 +1,121 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.util.TranslatedList; +import com.liferay.portal.kernel.xml.Attribute; +import com.liferay.portal.kernel.xml.Branch; +import com.liferay.portal.kernel.xml.CDATA; +import com.liferay.portal.kernel.xml.Comment; +import com.liferay.portal.kernel.xml.Document; +import com.liferay.portal.kernel.xml.Element; +import com.liferay.portal.kernel.xml.Entity; +import com.liferay.portal.kernel.xml.Namespace; +import com.liferay.portal.kernel.xml.Node; +import com.liferay.portal.kernel.xml.ProcessingInstruction; +import com.liferay.portal.kernel.xml.QName; +import com.liferay.portal.kernel.xml.Text; +import com.liferay.portal.kernel.xml.XPath; + +import java.util.List; + +/** + * @author Brian Wing Shun Chan + */ +public class NodeList extends TranslatedList { + + public NodeList(List newList, List oldList) { + super(newList, oldList); + } + + protected TranslatedList newInstance( + List newList, List oldList) { + + return new NodeList(newList, oldList); + } + + protected F toOldObject(E o) { + if (o instanceof Attribute) { + AttributeImpl attributeImpl = (AttributeImpl)o; + + return (F)attributeImpl.getWrappedAttribute(); + } + else if (o instanceof CDATA) { + CDATAImpl cdataImpl = (CDATAImpl)o; + + return (F)cdataImpl.getWrappedCDATA(); + } + else if (o instanceof Comment) { + CommentImpl commentImpl = (CommentImpl)o; + + return (F)commentImpl.getWrappedComment(); + } + else if (o instanceof Document) { + DocumentImpl documentImpl = (DocumentImpl)o; + + return (F)documentImpl.getWrappedDocument(); + } + else if (o instanceof Element) { + ElementImpl elementImpl = (ElementImpl)o; + + return (F)elementImpl.getWrappedElement(); + } + else if (o instanceof Entity) { + EntityImpl entityImpl = (EntityImpl)o; + + return (F)entityImpl.getWrappedEntity(); + } + else if (o instanceof Namespace) { + NamespaceImpl namespaceImpl = (NamespaceImpl)o; + + return (F)namespaceImpl.getWrappedNamespace(); + } + else if (o instanceof ProcessingInstruction) { + ProcessingInstructionImpl processingInstructionImpl = + (ProcessingInstructionImpl)o; + + return + (F)processingInstructionImpl.getWrappedProcessingInstruction(); + } + else if (o instanceof QName) { + QNameImpl qNameImpl = (QNameImpl)o; + + return (F)qNameImpl.getWrappedQName(); + } + else if (o instanceof Text) { + TextImpl textImpl = (TextImpl)o; + + return (F)textImpl.getWrappedText(); + } + else if (o instanceof XPath) { + XPathImpl xPathImpl = (XPathImpl)o; + + return (F)xPathImpl.getWrappedXPath(); + } + else if (o instanceof Branch) { + BranchImpl branchImpl = (BranchImpl)o; + + return (F)branchImpl.getWrappedBranch(); + } + else if (o instanceof Node) { + NodeImpl nodeImpl = (NodeImpl)o; + + return (F)nodeImpl.getWrappedNode(); + } + + throw new IllegalArgumentException(o.getClass().getName()); + } + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/ProcessingInstructionImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/ProcessingInstructionImpl.java new file mode 100644 index 000000000..ca9c078ba --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/ProcessingInstructionImpl.java @@ -0,0 +1,88 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.xml.ProcessingInstruction; + +import java.util.Map; + +/** + * @author Brian Wing Shun Chan + */ +public class ProcessingInstructionImpl + extends NodeImpl implements ProcessingInstruction { + + public ProcessingInstructionImpl( + org.dom4j.ProcessingInstruction processingInstruction) { + + super(processingInstruction); + + _processingInstruction = processingInstruction; + } + + public boolean equals(Object obj) { + org.dom4j.ProcessingInstruction processingInstruction = + ((ProcessingInstructionImpl)obj).getWrappedProcessingInstruction(); + + return _processingInstruction.equals(processingInstruction); + } + + public String getTarget() { + return _processingInstruction.getTarget(); + } + + public String getText() { + return _processingInstruction.getText(); + } + + public String getValue(String name) { + return _processingInstruction.getValue(name); + } + + public Map getValues() { + return _processingInstruction.getValues(); + } + + public org.dom4j.ProcessingInstruction getWrappedProcessingInstruction() { + return _processingInstruction; + } + + public int hashCode() { + return _processingInstruction.hashCode(); + } + + public boolean removeValue(String name) { + return _processingInstruction.removeValue(name); + } + + public void setTarget(String target) { + _processingInstruction.setTarget(target); + } + + public void setValue(String name, String value) { + _processingInstruction.setValue(name, value); + } + + public void setValues(Map data) { + _processingInstruction.setValues(data); + } + + public String toString() { + return _processingInstruction.toString(); + } + + private org.dom4j.ProcessingInstruction _processingInstruction; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/QNameImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/QNameImpl.java new file mode 100644 index 000000000..3f160f0a2 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/QNameImpl.java @@ -0,0 +1,80 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.xml.Namespace; +import com.liferay.portal.kernel.xml.QName; + +/** + * @author Brian Wing Shun Chan + */ +public class QNameImpl implements QName { + + public QNameImpl(org.dom4j.QName qName) { + _qName = qName; + } + + public boolean equals(Object obj) { + org.dom4j.QName qName = ((QNameImpl)obj).getWrappedQName(); + + return _qName.equals(qName); + } + + public String getLocalPart() { + return getName(); + } + + public String getName() { + return _qName.getName(); + } + + public Namespace getNamespace() { + org.dom4j.Namespace namespace = _qName.getNamespace(); + + if (namespace == null) { + return null; + } + else { + return new NamespaceImpl(namespace); + } + } + + public String getNamespacePrefix() { + return _qName.getNamespacePrefix(); + } + + public String getNamespaceURI() { + return _qName.getNamespaceURI(); + } + + public String getQualifiedName() { + return _qName.getQualifiedName(); + } + + public org.dom4j.QName getWrappedQName() { + return _qName; + } + + public int hashCode() { + return _qName.hashCode(); + } + + public String toString() { + return _qName.toString(); + } + + private org.dom4j.QName _qName; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/SAXReaderImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/SAXReaderImpl.java new file mode 100644 index 000000000..f25e1867d --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/SAXReaderImpl.java @@ -0,0 +1,457 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; +import com.liferay.portal.kernel.xml.Attribute; +import com.liferay.portal.kernel.xml.Document; +import com.liferay.portal.kernel.xml.DocumentException; +import com.liferay.portal.kernel.xml.Element; +import com.liferay.portal.kernel.xml.Entity; +import com.liferay.portal.kernel.xml.Namespace; +import com.liferay.portal.kernel.xml.Node; +import com.liferay.portal.kernel.xml.ProcessingInstruction; +import com.liferay.portal.kernel.xml.QName; +import com.liferay.portal.kernel.xml.SAXReader; +import com.liferay.portal.kernel.xml.Text; +import com.liferay.portal.kernel.xml.XPath; +import com.liferay.portal.util.EntityResolver; +import com.liferay.util.xml.XMLSafeReader; + +import java.io.File; +import java.io.InputStream; +import java.io.Reader; + +import java.net.MalformedURLException; +import java.net.URL; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.apache.xerces.parsers.SAXParser; + +import org.dom4j.DocumentFactory; +import org.dom4j.DocumentHelper; + +/** + * @author Brian Wing Shun Chan + */ +public class SAXReaderImpl implements SAXReader { + + public static SAXReaderImpl getInstance() { + return _instance; + } + + public static List toNewAttributes( + List oldAttributes) { + + List newAttributes = new ArrayList( + oldAttributes.size()); + + for (org.dom4j.Attribute oldAttribute : oldAttributes) { + newAttributes.add(new AttributeImpl(oldAttribute)); + } + + return new NodeList( + newAttributes, oldAttributes); + } + + public static List toNewElements( + List oldElements) { + + List newElements = new ArrayList(oldElements.size()); + + for (org.dom4j.Element oldElement : oldElements) { + newElements.add(new ElementImpl(oldElement)); + } + + return new NodeList( + newElements, oldElements); + } + + public static List toNewNamespaces( + List oldNamespaces) { + + List newNamespaces = new ArrayList( + oldNamespaces.size()); + + for (org.dom4j.Namespace oldNamespace : oldNamespaces) { + newNamespaces.add(new NamespaceImpl(oldNamespace)); + } + + return new NodeList( + newNamespaces, oldNamespaces); + } + + public static List toNewNodes(List oldNodes) { + List newNodes = new ArrayList(oldNodes.size()); + + for (org.dom4j.Node oldNode : oldNodes) { + if (oldNode instanceof org.dom4j.Element) { + newNodes.add(new ElementImpl((org.dom4j.Element)oldNode)); + } + else { + newNodes.add(new NodeImpl(oldNode)); + } + } + + return new NodeList(newNodes, oldNodes); + } + + public static List toNewProcessingInstructions( + List oldProcessingInstructions) { + + List newProcessingInstructions = + new ArrayList( + oldProcessingInstructions.size()); + + for (org.dom4j.ProcessingInstruction oldProcessingInstruction : + oldProcessingInstructions) { + + newProcessingInstructions.add( + new ProcessingInstructionImpl(oldProcessingInstruction)); + } + + return new NodeList + ( + newProcessingInstructions, oldProcessingInstructions); + } + + public static List toOldAttributes( + List newAttributes) { + + List oldAttributes = + new ArrayList(newAttributes.size()); + + for (Attribute newAttribute : newAttributes) { + AttributeImpl newAttributeImpl = (AttributeImpl)newAttribute; + + oldAttributes.add(newAttributeImpl.getWrappedAttribute()); + } + + return oldAttributes; + } + + public static List toOldNodes(List newNodes) { + List oldNodes = new ArrayList( + newNodes.size()); + + for (Node newNode : newNodes) { + NodeImpl newNodeImpl = (NodeImpl)newNode; + + oldNodes.add(newNodeImpl.getWrappedNode()); + } + + return oldNodes; + } + + public static List + toOldProcessingInstructions( + List newProcessingInstructions) { + + List oldProcessingInstructions = + new ArrayList( + newProcessingInstructions.size()); + + for (ProcessingInstruction newProcessingInstruction : + newProcessingInstructions) { + + ProcessingInstructionImpl newProcessingInstructionImpl = + (ProcessingInstructionImpl)newProcessingInstruction; + + oldProcessingInstructions.add( + newProcessingInstructionImpl.getWrappedProcessingInstruction()); + } + + return oldProcessingInstructions; + } + + public Attribute createAttribute( + Element element, QName qName, String value) { + + ElementImpl elementImpl = (ElementImpl)element; + QNameImpl qNameImpl = (QNameImpl)qName; + + DocumentFactory documentFactory = DocumentFactory.getInstance(); + + return new AttributeImpl( + documentFactory.createAttribute( + elementImpl.getWrappedElement(), qNameImpl.getWrappedQName(), + value)); + } + + public Attribute createAttribute( + Element element, String name, String value) { + + ElementImpl elementImpl = (ElementImpl)element; + + DocumentFactory documentFactory = DocumentFactory.getInstance(); + + return new AttributeImpl( + documentFactory.createAttribute( + elementImpl.getWrappedElement(), name, value)); + } + + public Document createDocument() { + return new DocumentImpl(DocumentHelper.createDocument()); + } + + public Document createDocument(Element rootElement) { + ElementImpl rootElementImpl = (ElementImpl)rootElement; + + return new DocumentImpl( + DocumentHelper.createDocument(rootElementImpl.getWrappedElement())); + } + + public Document createDocument(String encoding) { + DocumentFactory documentFactory = DocumentFactory.getInstance(); + + return new DocumentImpl(documentFactory.createDocument(encoding)); + } + + public Element createElement(QName qName) { + QNameImpl qNameImpl = (QNameImpl)qName; + + return new ElementImpl( + DocumentHelper.createElement(qNameImpl.getWrappedQName())); + } + + public Element createElement(String name) { + return new ElementImpl(DocumentHelper.createElement(name)); + } + + public Entity createEntity(String name, String text) { + return new EntityImpl(DocumentHelper.createEntity(name, text)); + } + + public Namespace createNamespace(String uri) { + return new NamespaceImpl(org.dom4j.Namespace.get(uri)); + } + + public Namespace createNamespace(String prefix, String uri) { + return new NamespaceImpl(DocumentHelper.createNamespace(prefix, uri)); + } + + public ProcessingInstruction createProcessingInstruction( + String target, Map data) { + + org.dom4j.ProcessingInstruction processingInstruction = + DocumentHelper.createProcessingInstruction(target, data); + + if (processingInstruction == null) { + return null; + } + else { + return new ProcessingInstructionImpl(processingInstruction); + } + } + + public ProcessingInstruction createProcessingInstruction( + String target, String data) { + + org.dom4j.ProcessingInstruction processingInstruction = + DocumentHelper.createProcessingInstruction(target, data); + + if (processingInstruction == null) { + return null; + } + else { + return new ProcessingInstructionImpl(processingInstruction); + } + } + + public QName createQName(String localName) { + return new QNameImpl(DocumentHelper.createQName(localName)); + } + + public QName createQName(String localName, Namespace namespace) { + NamespaceImpl namespaceImpl = (NamespaceImpl)namespace; + + return new QNameImpl( + DocumentHelper.createQName( + localName, namespaceImpl.getWrappedNamespace())); + } + + public Text createText(String text) { + return new TextImpl(DocumentHelper.createText(text)); + } + + public XPath createXPath(String xpathExpression) { + return new XPathImpl(DocumentHelper.createXPath(xpathExpression)); + } + + public List selectNodes( + String xpathFilterExpression, List nodes) { + + return toNewNodes( + DocumentHelper.selectNodes( + xpathFilterExpression, toOldNodes(nodes))); + } + + public List selectNodes( + String xpathFilterExpression, Node node) { + + NodeImpl nodeImpl = (NodeImpl)node; + + return toNewNodes( + DocumentHelper.selectNodes( + xpathFilterExpression, nodeImpl.getWrappedNode())); + } + + public void sort(List nodes, String xpathExpression) { + DocumentHelper.sort(toOldNodes(nodes), xpathExpression); + } + + public void sort( + List nodes, String xpathExpression, boolean distinct) { + + DocumentHelper.sort(toOldNodes(nodes), xpathExpression, distinct); + } + + public Document read(File file) throws DocumentException { + return read(file, false); + } + + public Document read(File file, boolean validate) + throws DocumentException { + + try { + org.dom4j.io.SAXReader saxReader = getSAXReader(validate); + + return new DocumentImpl(saxReader.read(file)); + } + catch (org.dom4j.DocumentException de) { + throw new DocumentException(de.getMessage(), de); + } + } + + public Document read(InputStream is) throws DocumentException { + return read(is, false); + } + + public Document read(InputStream is, boolean validate) + throws DocumentException { + + try { + org.dom4j.io.SAXReader saxReader = getSAXReader(validate); + + return new DocumentImpl(saxReader.read(is)); + } + catch (org.dom4j.DocumentException de) { + throw new DocumentException(de.getMessage(), de); + } + } + + public Document read(Reader reader) throws DocumentException { + return read(reader, false); + } + + public Document read(Reader reader, boolean validate) + throws DocumentException { + + try { + org.dom4j.io.SAXReader saxReader = getSAXReader(validate); + + return new DocumentImpl(saxReader.read(reader)); + } + catch (org.dom4j.DocumentException de) { + throw new DocumentException(de.getMessage(), de); + } + } + + public Document read(String xml) throws DocumentException { + return read(new XMLSafeReader(xml)); + } + + public Document read(String xml, boolean validate) + throws DocumentException { + + return read(new XMLSafeReader(xml), validate); + } + + public Document read(URL url) throws DocumentException { + return read(url, false); + } + + public Document read(URL url, boolean validate) throws DocumentException { + try { + org.dom4j.io.SAXReader saxReader = getSAXReader(validate); + + return new DocumentImpl(saxReader.read(url)); + } + catch (org.dom4j.DocumentException de) { + throw new DocumentException(de.getMessage(), de); + } + } + + public Document readURL(String url) + throws DocumentException, MalformedURLException { + + return read(new URL(url), false); + } + + public Document readURL(String url, boolean validate) + throws DocumentException, MalformedURLException { + + return read(new URL(url), validate); + } + + protected org.dom4j.io.SAXReader getSAXReader(boolean validate) { + org.dom4j.io.SAXReader reader = null; + + try { + reader = new org.dom4j.io.SAXReader(new SAXParser(), validate); + + reader.setEntityResolver(new EntityResolver()); + + reader.setFeature(_FEATURES_VALIDATION, validate); + reader.setFeature(_FEATURES_VALIDATION_SCHEMA, validate); + reader.setFeature( + _FEATURES_VALIDATION_SCHEMA_FULL_CHECKING, validate); + reader.setFeature(_FEATURES_DYNAMIC, validate); + } + catch (Exception e) { + if (_log.isWarnEnabled()) { + _log.warn( + "XSD validation is diasabled because " + e.getMessage()); + } + + reader = new org.dom4j.io.SAXReader(false); + + reader.setEntityResolver(new EntityResolver()); + } + + return reader; + } + + private static final String _FEATURES_VALIDATION = + "http://xml.org/sax/features/validation"; + + private static final String _FEATURES_VALIDATION_SCHEMA = + "http://apache.org/xml/features/validation/schema"; + + private static final String _FEATURES_VALIDATION_SCHEMA_FULL_CHECKING = + "http://apache.org/xml/features/validation/schema-full-checking"; + + private static final String _FEATURES_DYNAMIC = + "http://apache.org/xml/features/validation/dynamic"; + + private static Log _log = LogFactoryUtil.getLog(SAXReaderImpl.class); + + private static SAXReaderImpl _instance = new SAXReaderImpl(); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/TextImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/TextImpl.java new file mode 100644 index 000000000..82a12c061 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/TextImpl.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.xml.Text; + +/** + * @author Brian Wing Shun Chan + */ +public class TextImpl extends NodeImpl implements Text { + + public TextImpl(org.dom4j.Text text) { + super(text); + + _text = text; + } + + public boolean equals(Object obj) { + org.dom4j.Text text = ((TextImpl)obj).getWrappedText(); + + return _text.equals(text); + } + + public org.dom4j.Text getWrappedText() { + return _text; + } + + public int hashCode() { + return _text.hashCode(); + } + + public String toString() { + return _text.toString(); + } + + private org.dom4j.Text _text; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/XPathImpl.java b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/XPathImpl.java new file mode 100644 index 000000000..54b7b42a1 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/portal/xml/XPathImpl.java @@ -0,0 +1,164 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.portal.xml; + +import com.liferay.portal.kernel.xml.Node; +import com.liferay.portal.kernel.xml.XPath; + +import java.util.List; + +/** + * @author Brian Wing Shun Chan + */ +public class XPathImpl implements XPath { + + public XPathImpl(org.dom4j.XPath xPath) { + _xPath = xPath; + } + + public boolean booleanValueOf(Object context) { + return _xPath.booleanValueOf(toOldContext(context)); + } + + public boolean equals(Object obj) { + org.dom4j.XPath xPath = ((XPathImpl)obj).getWrappedXPath(); + + return _xPath.equals(xPath); + } + + public Object evaluate(Object context) { + return toNewContext(_xPath.evaluate(toOldContext(context))); + } + + public String getText() { + return _xPath.getText(); + } + + public org.dom4j.XPath getWrappedXPath() { + return _xPath; + } + + public int hashCode() { + return _xPath.hashCode(); + } + + public boolean matches(Node node) { + NodeImpl nodeImpl = (NodeImpl)node; + + return _xPath.matches(nodeImpl.getWrappedNode()); + } + + public Number numberValueOf(Object context) { + return _xPath.numberValueOf(toOldContext(context)); + } + + public List selectNodes(Object context) { + return SAXReaderImpl.toNewNodes( + _xPath.selectNodes(toOldContext(context))); + } + + public List selectNodes(Object context, XPath sortXPath) { + XPathImpl sortXPathImpl = (XPathImpl)sortXPath; + + return SAXReaderImpl.toNewNodes( + _xPath.selectNodes( + toOldContext(context), sortXPathImpl.getWrappedXPath())); + } + + public List selectNodes( + Object context, XPath sortXPath, boolean distinct) { + + XPathImpl sortXPathImpl = (XPathImpl)sortXPath; + + return SAXReaderImpl.toNewNodes( + _xPath.selectNodes( + toOldContext(context), sortXPathImpl.getWrappedXPath(), + distinct)); + } + + public Node selectSingleNode(Object context) { + org.dom4j.Node node = _xPath.selectSingleNode(toOldContext(context)); + + if (node == null) { + return null; + } + else { + return new NodeImpl(node); + } + } + + public void sort(List nodes) { + _xPath.sort(SAXReaderImpl.toOldNodes(nodes)); + } + + public void sort(List nodes, boolean distinct) { + _xPath.sort(SAXReaderImpl.toOldNodes(nodes), distinct); + } + + public String toString() { + return _xPath.toString(); + } + + public String valueOf(Object context) { + return _xPath.valueOf(toOldContext(context)); + } + + protected Object toNewContext(Object context) { + if (context == null) { + return null; + } + else if (context instanceof org.dom4j.Document) { + org.dom4j.Document document = (org.dom4j.Document)context; + + return new DocumentImpl(document); + } + else if (context instanceof org.dom4j.Node) { + org.dom4j.Node node = (org.dom4j.Node)context; + + return new NodeImpl(node); + } + else if (context instanceof List) { + return SAXReaderImpl.toNewNodes((List)context); + } + else { + return context; + } + } + + protected Object toOldContext(Object context) { + if (context == null) { + return null; + } + else if (context instanceof DocumentImpl) { + DocumentImpl documentImpl = (DocumentImpl)context; + + return documentImpl.getWrappedDocument(); + } + else if (context instanceof NodeImpl) { + NodeImpl nodeImpl = (NodeImpl)context; + + return nodeImpl.getWrappedNode(); + } + else if (context instanceof List) { + return SAXReaderImpl.toOldNodes((List)context); + } + else { + return context; + } + } + + private org.dom4j.XPath _xPath; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/AttributeComparator.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/AttributeComparator.java new file mode 100644 index 000000000..43bb04cd0 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/AttributeComparator.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml; + +import java.util.Comparator; + +import org.dom4j.Attribute; + +/** + * @author Brian Wing Shun Chan + */ +public class AttributeComparator implements Comparator { + + public int compare(Attribute attr1, Attribute attr2) { + String attr1Value = attr1.getValue(); + String attr2Value = attr2.getValue(); + + return attr1Value.compareTo(attr2Value); + } + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/ElementComparator.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/ElementComparator.java new file mode 100644 index 000000000..a91bdad90 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/ElementComparator.java @@ -0,0 +1,127 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml; + +import java.util.Comparator; +import java.util.List; + +import org.dom4j.Attribute; +import org.dom4j.Element; + +/** + * @author Brian Wing Shun Chan + */ +public class ElementComparator implements Comparator { + + public int compare(Element el1, Element el2) { + String el1Name = el1.getName(); + String el2Name = el2.getName(); + + if (!el1Name.equals(el2Name)) { + return el1Name.compareTo(el2Name); + } + + String el1Text = el1.getTextTrim(); + String el2Text = el2.getTextTrim(); + + if (!el1Text.equals(el2Text)) { + return el1Text.compareTo(el2Text); + } + + List el1Attributes = el1.attributes(); + List el2Attributes = el2.attributes(); + + if (el1Attributes.size() < el2Attributes.size()) { + return -1; + } + else if (el1Attributes.size() > el2Attributes.size()) { + return 1; + } + + for (Attribute attr : el1Attributes) { + int value = _compare( + el2Attributes, attr, new AttributeComparator()); + + if (value != 0) { + return value; + } + } + + List el1Elements = el1.elements(); + List el2Elements = el2.elements(); + + if (el1Elements.size() < el2Elements.size()) { + return -1; + } + else if (el1Elements.size() > el2Elements.size()) { + return 1; + } + + for (Element el : el1Elements) { + int value = _compare(el2Elements, el, new ElementComparator()); + + if (value != 0) { + return value; + } + } + + return 0; + } + + private int _compare( + List list, Attribute obj, Comparator comparator) { + + int firstValue = -1; + + for (int i = 0; i < list.size(); i++) { + Attribute o = list.get(i); + + int value = comparator.compare(obj, o); + + if (i == 0) { + firstValue = value; + } + + if (value == 0) { + return 0; + } + } + + return firstValue; + } + + private int _compare( + List list, Element obj, Comparator comparator) { + + int firstValue = -1; + + for (int i = 0; i < list.size(); i++) { + Element o = list.get(i); + + int value = comparator.compare(obj, o); + + if (i == 0) { + firstValue = value; + } + + if (value == 0) { + return 0; + } + } + + return firstValue; + } + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/ElementIdentifier.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/ElementIdentifier.java new file mode 100644 index 000000000..6ad88d347 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/ElementIdentifier.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml; + +/** + * @author Jorge Ferrer + */ +public class ElementIdentifier { + + public ElementIdentifier(String elementName, String identifierName) { + _elementName = elementName; + _identifierName = identifierName; + } + + public String getElementName() { + return _elementName; + } + + public String getIdentifierName() { + return _identifierName; + } + + private String _identifierName; + private String _elementName; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLConverter.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLConverter.java new file mode 100644 index 000000000..654ebb8a5 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLConverter.java @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml; + +/** + * @author Brian Wing Shun Chan + */ +public class XMLConverter { + + public static org.w3c.dom.Document toW3CDocument( + org.dom4j.Document dom4jDoc) + throws org.dom4j.DocumentException { + + org.dom4j.io.DOMWriter dom4jWriter = new org.dom4j.io.DOMWriter(); + + org.w3c.dom.Document w3cDoc = dom4jWriter.write(dom4jDoc); + + return w3cDoc; + } + + public static org.w3c.dom.Element toW3CElement(org.dom4j.Element dom4jEl) + throws org.dom4j.DocumentException { + + org.dom4j.Document dom4jDoc = + org.dom4j.DocumentFactory.getInstance().createDocument(); + + dom4jDoc.setRootElement(dom4jEl.createCopy()); + + org.w3c.dom.Document w3cDoc = toW3CDocument(dom4jDoc); + + return w3cDoc.getDocumentElement(); + } + + public static javax.xml.namespace.QName toJavaxQName( + org.dom4j.QName dom4jQName) { + + javax.xml.namespace.QName javaxQName = new javax.xml.namespace.QName( + dom4jQName.getNamespaceURI(), dom4jQName.getName(), + dom4jQName.getNamespacePrefix()); + + return javaxQName; + } + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLElementComparator.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLElementComparator.java new file mode 100644 index 000000000..44cccfb86 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLElementComparator.java @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml; + +import com.liferay.util.xml.descriptor.XMLDescriptor; + +import org.dom4j.Element; + +/** + * @author Jorge Ferrer + */ +public class XMLElementComparator extends ElementComparator { + + public XMLElementComparator(XMLDescriptor descriptor) { + _descriptor = descriptor; + } + + public int compare(Element el1, Element el2) { + if (_descriptor.areEqual(el1, el2)) { + return 0; + } + else { + return -1; + } + } + + public boolean canJoinChildren(Element element) { + return _descriptor.canJoinChildren(element); + } + + private XMLDescriptor _descriptor; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLFormatter.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLFormatter.java new file mode 100644 index 000000000..339224f77 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLFormatter.java @@ -0,0 +1,163 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml; + +import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream; +import com.liferay.portal.kernel.io.unsync.UnsyncStringReader; +import com.liferay.portal.kernel.util.CharPool; +import com.liferay.portal.kernel.util.StringPool; +import com.liferay.portal.kernel.util.StringUtil; +import com.liferay.portal.kernel.util.Validator; + +import java.io.IOException; + +import org.dom4j.Branch; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.io.OutputFormat; +import org.dom4j.io.SAXReader; +import org.dom4j.io.XMLWriter; + +/** + * @author Brian Wing Shun Chan + * @author Alan Zimmerman + */ +public class XMLFormatter { + + public static String fixProlog(String xml) { + + // LEP-1921 + + if (xml != null) { + int pos = xml.indexOf(CharPool.LESS_THAN); + + if (pos > 0) { + xml = xml.substring(pos); + } + } + + return xml; + } + + public static String fromCompactSafe(String xml) { + return StringUtil.replace(xml, "[$NEW_LINE$]", StringPool.NEW_LINE); + } + + public static String stripInvalidChars(String xml) { + if (Validator.isNull(xml)) { + return xml; + } + + // Strip characters that are not valid in the 1.0 XML spec + // http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char + + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < xml.length(); i++) { + char c = xml.charAt(i); + + if ((c == 0x9) || (c == 0xA) || (c == 0xD) || + ((c >= 0x20) && (c <= 0xD7FF)) || + ((c >= 0xE000) && (c <= 0xFFFD)) || + ((c >= 0x10000) && (c <= 0x10FFFF))) { + + sb.append(c); + } + } + + return sb.toString(); + } + + public static String toCompactSafe(String xml) { + return StringUtil.replace( + xml, + new String[] { + StringPool.RETURN_NEW_LINE, + StringPool.NEW_LINE, + StringPool.RETURN + }, + new String[] { + "[$NEW_LINE$]", + "[$NEW_LINE$]", + "[$NEW_LINE$]" + }); + } + + public static String toString(Branch branch) throws IOException { + return toString(branch, StringPool.TAB); + } + + public static String toString(Branch branch, String indent) + throws IOException { + + return toString(branch, StringPool.TAB, false); + } + + public static String toString( + Branch branch, String indent, boolean expandEmptyElements) + throws IOException { + + UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = + new UnsyncByteArrayOutputStream(); + + OutputFormat outputFormat = OutputFormat.createPrettyPrint(); + + outputFormat.setExpandEmptyElements(expandEmptyElements); + outputFormat.setIndent(indent); + outputFormat.setLineSeparator(StringPool.NEW_LINE); + + XMLWriter writer = new XMLWriter( + unsyncByteArrayOutputStream, outputFormat); + + writer.write(branch); + + String content = unsyncByteArrayOutputStream.toString(StringPool.UTF8); + + // LEP-4257 + + //content = StringUtil.replace(content, "\n\n\n", "\n\n"); + + if (content.endsWith("\n\n")) { + content = content.substring(0, content.length() - 2); + } + + if (content.endsWith("\n")) { + content = content.substring(0, content.length() - 1); + } + + while (content.indexOf(" \n") != -1) { + content = StringUtil.replace(content, " \n", "\n"); + } + + return content; + } + + public static String toString(String xml) + throws DocumentException, IOException { + + return toString(xml, StringPool.TAB); + } + + public static String toString(String xml, String indent) + throws DocumentException, IOException { + + SAXReader reader = new SAXReader(); + + Document doc = reader.read(new UnsyncStringReader(xml)); + + return toString(doc, indent); + } + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLMerger.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLMerger.java new file mode 100644 index 000000000..89f03487f --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLMerger.java @@ -0,0 +1,184 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml; + +import com.liferay.util.xml.descriptor.XMLDescriptor; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Vector; + +import org.dom4j.Document; +import org.dom4j.Element; + +/** + * @author Brian Wing Shun Chan + * @author Alan Zimmerman + * @author Jorge Ferrer + */ +public class XMLMerger { + + public XMLMerger(XMLDescriptor descriptor) { + _descriptor = descriptor; + } + + public XMLElementComparator getElementComparator() { + return new XMLElementComparator(_descriptor); + } + + public Document merge(Document masterDoc, Document slaveDoc) { + Document mergedDoc = (Document)masterDoc.clone(); + + Element root1 = mergedDoc.getRootElement(); + Element root2 = slaveDoc.getRootElement(); + + List children = root2.elements(); + + for (Element el2 : children) { + Element el2Clone = (Element)el2.clone(); + + el2Clone.detach(); + + root1.add(el2Clone); + } + + organizeXML(mergedDoc); + + return mergedDoc; + } + + public void organizeXML(Document doc) { + Element root = doc.getRootElement(); + + _orderChildren(root, _descriptor.getRootChildrenOrder()); + _mergeDuplicateElements(root, getElementComparator()); + } + + private void _addChildren( + Element first, Collection childrenToJoin) { + + Collection clones = new Vector(); + + Iterator itr = childrenToJoin.iterator(); + + while (itr.hasNext()) { + clones.add((Element)itr.next().clone()); + } + + first.elements().addAll(clones); + + _orderChildren(first, _descriptor.getChildrenOrder(first)); + } + + private boolean _containsObjectEqualTo( + Element example, List list, ElementComparator comparator) { + + Iterator itr = list.iterator(); + + while (itr.hasNext()) { + Element candidate = itr.next(); + + if (comparator.compare(example, candidate) == 0) { + return true; + } + } + + return false; + } + + private Element _findObjectEqualTo( + Element example, List list, ElementComparator comparator) { + + Iterator itr = list.iterator(); + + while (itr.hasNext()) { + Element candidate = itr.next(); + + if (comparator.compare(example, candidate) == 0) { + return candidate; + } + } + + return example; + } + + private void _mergeDuplicateElements( + Element el, ElementComparator comparator) { + + if (el.elements().size() > 0) { + List children = el.elements(); + + List originals = new ArrayList(); + List duplicates = new ArrayList(); + + for (int i = 0; i < children.size(); i++) { + Element child = children.get(i); + + if (_containsObjectEqualTo(child, originals, comparator)) { + if (_descriptor.canJoinChildren(child)) { + Element first = + _findObjectEqualTo(child, originals, comparator); + + Collection childrenToJoin = child.elements(); + + _addChildren(first, childrenToJoin); + } + + duplicates.add(child); + } + else { + originals.add(child); + } + } + + for (Element duplicate : duplicates) { + duplicate.detach(); + } + + Iterator itr = originals.iterator(); + + while (itr.hasNext()) { + Element child = itr.next(); + + _mergeDuplicateElements(child, comparator); + } + } + } + + private void _orderChildren( + Element parent, String[] orderedChildrenNames) { + + if (orderedChildrenNames == null) { + return; + } + + List elements = new ArrayList(); + + for (int i = 0; i < orderedChildrenNames.length; i++) { + elements.addAll(parent.elements(orderedChildrenNames[i])); + } + + for (Element el : elements) { + el.detach(); + + parent.add(el); + } + } + + private XMLDescriptor _descriptor; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLMergerRunner.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLMergerRunner.java new file mode 100644 index 000000000..5818a6228 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLMergerRunner.java @@ -0,0 +1,165 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml; + +import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream; +import com.liferay.portal.kernel.io.unsync.UnsyncStringReader; +import com.liferay.portal.kernel.util.FileUtil; +import com.liferay.portal.kernel.util.StringUtil; +import com.liferay.portal.kernel.util.Validator; +import com.liferay.util.xml.descriptor.XMLDescriptor; + +import java.io.File; +import java.io.IOException; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.io.OutputFormat; +import org.dom4j.io.SAXReader; +import org.dom4j.io.XMLWriter; + +/** + * @author Jorge Ferrer + */ +public class XMLMergerRunner { + + public static void main(String[] args) + throws ClassNotFoundException, DocumentException, + IllegalAccessException, InstantiationException, IOException { + + if ((args != null) && (args.length == 4)) { + XMLMergerRunner runner = new XMLMergerRunner(args[3]); + + runner.mergeAndSave(args[0], args[1], args[2]); + } + else { + throw new IllegalArgumentException(); + } + } + + public XMLMergerRunner(String descriptorClassName) { + if (Validator.isNotNull(descriptorClassName)) { + _descriptorClassName = descriptorClassName; + } + } + + public void mergeAndSave( + String masterFile, String slaveFile, String mergedFile) + throws ClassNotFoundException, DocumentException, + IllegalAccessException, InstantiationException, IOException { + + mergeAndSave( + new File(masterFile), new File(slaveFile), new File(mergedFile)); + } + + public void mergeAndSave(File masterFile, File slaveFile, File mergedFile) + throws ClassNotFoundException, DocumentException, + IllegalAccessException, InstantiationException, IOException { + + String xml1 = FileUtil.read(masterFile); + String xml2 = FileUtil.read(slaveFile); + + String mergedXml = _merge(xml1, xml2); + + FileUtil.write(mergedFile, mergedXml); + } + + private String _documentToString(Document doc, String docType) + throws IOException { + + UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = + new UnsyncByteArrayOutputStream(); + + OutputFormat format = OutputFormat.createPrettyPrint(); + + format.setIndent("\t"); + format.setLineSeparator("\n"); + + XMLWriter writer = new XMLWriter(unsyncByteArrayOutputStream, format); + + writer.write(doc); + + String xml = unsyncByteArrayOutputStream.toString(); + + int pos = xml.indexOf("", pos) + 2); + + xml = StringUtil.replace(xml, header, ""); + xml = header + "\n" + docType + "\n" + xml; + + return xml; + } + + private String _merge(String masterXml, String slaveXml) + throws ClassNotFoundException, DocumentException, + IllegalAccessException, InstantiationException, IOException { + + int pos = masterXml.indexOf("= 0) { + masterDoctype = masterXml.substring( + pos, masterXml.indexOf(">", pos) + 1); + masterXml = StringUtil.replace(masterXml, masterDoctype, ""); + } + + pos = slaveXml.indexOf("= 0) { + slaveDoctype = slaveXml.substring( + pos, slaveXml.indexOf(">", pos) + 1); + slaveXml = StringUtil.replace(slaveXml, slaveDoctype, ""); + } + + String doctype = null; + + if (Validator.isNotNull(masterDoctype)) { + doctype = masterDoctype; + } + else { + doctype = slaveDoctype; + } + + SAXReader reader = new SAXReader(); + + Document masterDoc = reader.read(new UnsyncStringReader(masterXml)); + Document slaveDoc = reader.read(new UnsyncStringReader(slaveXml)); + + XMLDescriptor descriptor = null; + + if (_descriptorClassName.equals(_AUTO_DESCRIPTOR)) { + descriptor = XMLTypeDetector.determineType(doctype, masterDoc); + } + else { + descriptor = (XMLDescriptor)Class.forName( + _descriptorClassName).newInstance(); + } + + XMLMerger merger = new XMLMerger(descriptor); + + Document mergedDoc = merger.merge(masterDoc, slaveDoc); + + return _documentToString(mergedDoc, doctype); + } + + private static final String _AUTO_DESCRIPTOR = "auto"; + + private String _descriptorClassName = _AUTO_DESCRIPTOR; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLMergerTask.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLMergerTask.java new file mode 100644 index 000000000..12f06f88a --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLMergerTask.java @@ -0,0 +1,73 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml; + +import java.io.File; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; + +/** + * @author Jorge Ferrer + */ +public class XMLMergerTask extends Task { + + public void setMasterFile(File masterFile) { + _masterFile = masterFile; + } + + public void setOutputFile(File outputFile) { + _outputFile = outputFile; + } + + public void setSlaveFile(File slaveFile) { + _slaveFile = slaveFile; + } + + public void setType(String type) { + _type = type; + } + + public void execute() throws BuildException { + _validateAttributes(); + + try { + XMLMergerRunner runner = new XMLMergerRunner(_type); + + runner.mergeAndSave(_masterFile, _slaveFile, _outputFile); + } + catch (Exception e) { + throw new BuildException(e); + } + } + + private void _validateAttributes() { + _validateMandatoryAttribute(_masterFile, "masterFile"); + _validateMandatoryAttribute(_slaveFile, "slaveFile"); + _validateMandatoryAttribute(_outputFile, "outputFile"); + } + + private void _validateMandatoryAttribute(File value, String name) { + if (value == null) { + throw new BuildException(name + " is a required attribute"); + } + } + + private File _masterFile; + private File _slaveFile; + private File _outputFile; + private String _type; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLSafeReader.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLSafeReader.java new file mode 100644 index 000000000..5d49a6425 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLSafeReader.java @@ -0,0 +1,28 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml; + +import com.liferay.portal.kernel.io.unsync.UnsyncStringReader; + +/** + * @author Brian Wing Shun Chan + */ +public class XMLSafeReader extends UnsyncStringReader { + + public XMLSafeReader(String xml) { + super(XMLFormatter.fixProlog(xml)); + } + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLTypeDetector.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLTypeDetector.java new file mode 100644 index 000000000..e84389f8f --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/XMLTypeDetector.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml; + +import com.liferay.util.xml.descriptor.PortletAppDescriptor; +import com.liferay.util.xml.descriptor.StrictXMLDescriptor; +import com.liferay.util.xml.descriptor.StrutsConfigDescriptor; +import com.liferay.util.xml.descriptor.TilesDefsDescriptor; +import com.liferay.util.xml.descriptor.WebXML23Descriptor; +import com.liferay.util.xml.descriptor.WebXML24Descriptor; +import com.liferay.util.xml.descriptor.XMLDescriptor; + +import org.dom4j.Document; + +/** + * @author Jorge Ferrer + */ +public class XMLTypeDetector { + + public static final XMLDescriptor[] REGISTERED_DESCRIPTORS = { + new PortletAppDescriptor(), new StrutsConfigDescriptor(), + new TilesDefsDescriptor(), new WebXML23Descriptor(), + new WebXML24Descriptor() + }; + + public static XMLDescriptor determineType(String doctype, Document root) { + for (int i = 0; i < REGISTERED_DESCRIPTORS.length; i++) { + XMLDescriptor descriptor = REGISTERED_DESCRIPTORS[i]; + + if (descriptor.canHandleType(doctype, root)) { + return descriptor; + } + } + + return new StrictXMLDescriptor(); + } + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/FacesXMLDescriptor.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/FacesXMLDescriptor.java new file mode 100644 index 000000000..b812f3f28 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/FacesXMLDescriptor.java @@ -0,0 +1,73 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml.descriptor; + +import com.liferay.util.xml.ElementIdentifier; + +import org.dom4j.Document; + +/** + * @author Jorge Ferrer + */ +public class FacesXMLDescriptor extends SimpleXMLDescriptor { + + public boolean canHandleType(String doctype, Document root) { + if (doctype.indexOf("faces-config") != -1) { + return true; + } + else { + return false; + } + } + + public String[] getRootChildrenOrder() { + return _ROOT_ORDERED_CHILDREN; + } + + public ElementIdentifier[] getElementsIdentifiedByAttribute() { + return _ELEMENTS_IDENTIFIED_BY_ATTR; + } + + public ElementIdentifier[] getElementsIdentifiedByChild() { + return _ELEMENTS_IDENTIFIED_BY_CHILD; + } + + public String[] getUniqueElements() { + return _UNIQUE_ELEMENTS; + } + + public String[] getJoinableElements() { + return _JOINABLE_ELEMENTS; + } + + private static final String[] _ROOT_ORDERED_CHILDREN = { + "application", "factory", "component", "converter", "managed-bean", + "navigation-rule", "referenced-bean", "render-kit", "lifecycle", + "validator" + }; + + private static final ElementIdentifier[] _ELEMENTS_IDENTIFIED_BY_ATTR = { + }; + + private static final ElementIdentifier[] _ELEMENTS_IDENTIFIED_BY_CHILD = { + }; + + private static final String[] _UNIQUE_ELEMENTS = { + }; + + private static final String[] _JOINABLE_ELEMENTS = { + }; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/PortletAppDescriptor.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/PortletAppDescriptor.java new file mode 100644 index 000000000..4f6706be0 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/PortletAppDescriptor.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml.descriptor; + +import com.liferay.util.xml.ElementIdentifier; + +import org.dom4j.Document; + +/** + * @author Jorge Ferrer + */ +public class PortletAppDescriptor extends SimpleXMLDescriptor { + + public boolean canHandleType(String doctype, Document root) { + return root.getRootElement().getName().equals("portlet-app"); + } + + public ElementIdentifier[] getElementsIdentifiedByChild() { + return _ELEMENTS_IDENTIFIED_BY_CHILD; + } + + private static final ElementIdentifier[] _ELEMENTS_IDENTIFIED_BY_CHILD = { + new ElementIdentifier("portlet", "portlet-name"), + }; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/SimpleXMLDescriptor.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/SimpleXMLDescriptor.java new file mode 100644 index 000000000..f92618b39 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/SimpleXMLDescriptor.java @@ -0,0 +1,156 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml.descriptor; + +import com.liferay.util.xml.ElementComparator; +import com.liferay.util.xml.ElementIdentifier; + +import org.dom4j.Document; +import org.dom4j.Element; + +/** + * @author Jorge Ferrer + */ +public abstract class SimpleXMLDescriptor implements XMLDescriptor { + + public boolean areEqual(Element el1, Element el2) { + String name1 = el1.getName(); + String name2 = el2.getName(); + + if ((name1 == null) || !name1.equals(name2)) { + return false; + } + + if (_isIncluded(el1, getUniqueElements())) { + return true; + } + + ElementIdentifier[] elIds = getElementsIdentifiedByAttribute(); + for (int i = 0; i < elIds.length; i++) { + if (name1.equals(elIds[i].getElementName())) { + if (_compareAttribute( + el1, el2, elIds[i].getIdentifierName()) == 0) { + + return true; + } + else { + return false; + } + } + } + + elIds = getElementsIdentifiedByChild(); + for (int i = 0; i < elIds.length; i++) { + if (name1.equals(elIds[i].getElementName())) { + if (_compareChildText( + el1, el2, elIds[i].getIdentifierName()) == 0) { + return true; + } + else { + return false; + } + } + } + + ElementComparator comparator = new ElementComparator(); + + if (comparator.compare(el1, el2) == 0) { + return true; + } + else { + return false; + } + } + + public abstract boolean canHandleType(String doctype, Document root); + + public boolean canJoinChildren(Element element) { + return _isIncluded(element, getJoinableElements()); + } + + public String[] getRootChildrenOrder() { + return new String[0]; + } + + public String[] getChildrenOrder(Element parentElement) { + return new String[0]; + } + + public ElementIdentifier[] getElementsIdentifiedByAttribute() { + return new ElementIdentifier[0]; + } + + public ElementIdentifier[] getElementsIdentifiedByChild() { + return new ElementIdentifier[0]; + } + + public String[] getUniqueElements() { + return new String[0]; + } + + public String[] getJoinableElements() { + return new String[0]; + } + + private int _compareAttribute(Element el1, Element el2, String attrName) { + String name1 = el1.attributeValue(attrName); + String name2 = el2.attributeValue(attrName); + + if ((name1 == null) || (name2 == null)) { + return -1; + } + + return name1.compareTo(name2); + } + + private int _compareChildText(Element el1, Element el2, String childName) { + Element child1 = _getChild(el1, childName); + Element child2 = _getChild(el2, childName); + + if ((child1 == null) || (child2 == null)) { + return -1; + } + + String name1 = child1.getText(); + String name2 = child2.getText(); + + if ((name1 == null) || (name2 == null)) { + return -1; + } + + return name1.compareTo(name2); + } + + private Element _getChild(Element parent, String childName) { + Element child = parent.element(childName); + + /*if (child == null) { + child = parent.element(childName, parent.getNamespace()); + }*/ + + return child; + } + + private boolean _isIncluded(Element element, String[] elemNames) { + for (int i = 0; i < elemNames.length; i++) { + if (element.getName().equals(elemNames[i])) { + return true; + } + } + + return false; + } + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/StrictXMLDescriptor.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/StrictXMLDescriptor.java new file mode 100644 index 000000000..88bd806f7 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/StrictXMLDescriptor.java @@ -0,0 +1,181 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml.descriptor; + +import com.liferay.util.xml.AttributeComparator; +import com.liferay.util.xml.ElementComparator; + +import java.util.Comparator; +import java.util.List; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; + +/** + * @author Jorge Ferrer + */ +public class StrictXMLDescriptor implements XMLDescriptor { + + public boolean areEqual(Element el1, Element el2) { + if (_compare(el1, el2) == 0) { + return true; + } + else { + return false; + } + } + + public boolean canHandleType(String doctype, Document root) { + return false; + } + + public boolean canJoinChildren(Element element) { + return false; + } + + public String[] getRootChildrenOrder() { + return _ROOT_ORDERED_CHILDREN; + } + + public String[] getChildrenOrder(Element parentElement) { + return new String[0]; + } + + private int _compare(Object obj1, Object obj2) { + Element el1 = (Element)obj1; + Element el2 = (Element)obj2; + + String el1Name = el1.getName(); + String el2Name = el2.getName(); + + if (!el1Name.equals(el2Name)) { + return el1Name.compareTo(el2Name); + } + + String el1Text = el1.getTextTrim(); + String el2Text = el2.getTextTrim(); + + if (!el1Text.equals(el2Text)) { + return el1Text.compareTo(el2Text); + } + + int attributeComparison = _compareAttributes(el1, el2); + + if (attributeComparison != 0) { + return attributeComparison; + } + + int childrenComparison = _compareChildren(el1, el2); + + if (childrenComparison != 0) { + return childrenComparison; + } + + return 0; + } + + private int _compareAttributes(Element el1, Element el2) { + List el1Attrs = el1.attributes(); + List el2Attrs = el2.attributes(); + + if (el1Attrs.size() < el2Attrs.size()) { + return -1; + } + else if (el1Attrs.size() > el2Attrs.size()) { + return 1; + } + + for (Attribute attr : el1Attrs) { + int value = _contains(el2Attrs, attr, new AttributeComparator()); + + if (value != 0) { + return value; + } + } + + return -1; + } + + private int _compareChildren(Element el1, Element el2) { + List el1Children = el1.elements(); + List el2Children = el2.elements(); + + if (el1Children.size() < el2Children.size()) { + return -1; + } + else if (el1Children.size() > el2Children.size()) { + return 1; + } + + for (Element el : el1Children) { + int value = _contains(el2Children, el, new ElementComparator()); + + if (value != 0) { + return value; + } + } + + return -1; + } + + private int _contains( + List list, Attribute obj, Comparator comparator) { + + int firstValue = -1; + + for (int i = 0; i < list.size(); i++) { + Attribute o = list.get(i); + + int value = comparator.compare(obj, o); + + if (i == 0) { + firstValue = value; + } + + if (value == 0) { + return 0; + } + } + + return firstValue; + } + + private int _contains( + List list, Element obj, Comparator comparator) { + + int firstValue = -1; + + for (int i = 0; i < list.size(); i++) { + Element o = list.get(i); + + int value = comparator.compare(obj, o); + + if (i == 0) { + firstValue = value; + } + + if (value == 0) { + return 0; + } + } + + return firstValue; + } + + private static final String[] _ROOT_ORDERED_CHILDREN = { + }; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/StrutsConfigDescriptor.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/StrutsConfigDescriptor.java new file mode 100644 index 000000000..f8d641e56 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/StrutsConfigDescriptor.java @@ -0,0 +1,73 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml.descriptor; + +import com.liferay.util.xml.ElementIdentifier; + +import org.dom4j.Document; + +/** + * @author Jorge Ferrer + */ +public class StrutsConfigDescriptor extends SimpleXMLDescriptor { + + public boolean canHandleType(String doctype, Document root) { + if (doctype.indexOf("struts-config") != -1) { + return true; + } + else { + return false; + } + } + + public String[] getRootChildrenOrder() { + return _ROOT_ORDERED_CHILDREN; + } + + public ElementIdentifier[] getElementsIdentifiedByAttribute() { + return _ELEMENTS_IDENTIFIED_BY_ATTR; + } + + public String[] getUniqueElements() { + return _UNIQUE_ELEMENTS; + } + + public String[] getJoinableElements() { + return _JOINABLE_ELEMENTS; + } + + private static final String[] _ROOT_ORDERED_CHILDREN = { + "data-sources", "form-beans", "global-exceptions", "global-forwards", + "action-mappings", "controller", "message-resources", "plug-in" + }; + + private static final ElementIdentifier[] _ELEMENTS_IDENTIFIED_BY_ATTR = { + new ElementIdentifier("forward", "name"), + new ElementIdentifier("action", "path"), + new ElementIdentifier("data-source", "id"), + new ElementIdentifier("form-bean", "name") + }; + + private static final String[] _UNIQUE_ELEMENTS = { + "data-sources", "form-beans", "global-exceptions", "global-forwards", + "action-mappings", "controller" + }; + + private static final String[] _JOINABLE_ELEMENTS = { + "data-sources", "form-beans", "global-exceptions", "global-forwards", + "action-mappings" + }; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/TilesDefsDescriptor.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/TilesDefsDescriptor.java new file mode 100644 index 000000000..f96789838 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/TilesDefsDescriptor.java @@ -0,0 +1,58 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml.descriptor; + +import com.liferay.util.xml.ElementIdentifier; + +import org.dom4j.Document; + +/** + * @author Jorge Ferrer + */ +public class TilesDefsDescriptor extends SimpleXMLDescriptor { + + public boolean canHandleType(String doctype, Document root) { + if (doctype.indexOf("tiles-config") != -1) { + return true; + } + else { + return false; + } + } + + public String[] getRootChildrenOrder() { + return _ROOT_CHILDREN_ORDER; + } + + public ElementIdentifier[] getElementsIdentifiedByAttribute() { + return _ELEMENTS_IDENTIFIED_BY_ATTR; + } + + public String[] getUniqueElements() { + return _UNIQUE_ELEMENTS; + } + + private static final String[] _ROOT_CHILDREN_ORDER ={ + "definition" + }; + + private static final ElementIdentifier[] _ELEMENTS_IDENTIFIED_BY_ATTR = { + new ElementIdentifier("definition", "name") + }; + + private static final String[] _UNIQUE_ELEMENTS = { + }; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/WebXML23Descriptor.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/WebXML23Descriptor.java new file mode 100644 index 000000000..a56d2eb86 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/WebXML23Descriptor.java @@ -0,0 +1,88 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml.descriptor; + +import com.liferay.util.xml.ElementIdentifier; + +import org.dom4j.Document; + +/** + * @author Jorge Ferrer + */ +public class WebXML23Descriptor extends SimpleXMLDescriptor { + + public boolean canHandleType(String doctype, Document root) { + if (doctype.indexOf("web-app") != -1) { + return true; + } + else { + return false; + } + } + + public String[] getRootChildrenOrder() { + return _ROOT_ORDERED_CHILDREN; + } + + public ElementIdentifier[] getElementsIdentifiedByAttribute() { + return _ELEMENTS_IDENTIFIED_BY_ATTR; + } + + public ElementIdentifier[] getElementsIdentifiedByChild() { + return _ELEMENTS_IDENTIFIED_BY_CHILD; + } + + public String[] getUniqueElements() { + return _UNIQUE_ELEMENTS; + } + + public String[] getJoinableElements() { + return _JOINABLE_ELEMENTS; + } + + private static final String[] _ROOT_ORDERED_CHILDREN = { + "icon", "display-name", "description", "distributable", "context-param", + "filter", "filter-mapping", "listener", "servlet", "servlet-mapping", + "session-config", "mime-mapping", "welcome-file-list", "error-page", + "taglib", "resource-env-ref", "resource-ref", "security-constraint", + "login-config", "security-role", "env-entry", "ejb-ref", "ejb-local-ref" + }; + + private static final ElementIdentifier[] _ELEMENTS_IDENTIFIED_BY_ATTR = { + }; + + private static final ElementIdentifier[] _ELEMENTS_IDENTIFIED_BY_CHILD = { + new ElementIdentifier("context-param", "param-name"), + new ElementIdentifier("filter", "filter-name"), + //new ElementIdentifier("filter-mapping", "filter-name"), + new ElementIdentifier("servlet", "servlet-name"), + //new ElementIdentifier("servlet-mapping", "servlet-name"), + new ElementIdentifier("init-param", "param-name"), + new ElementIdentifier("taglib", "taglib-uri"), + new ElementIdentifier("resource-env-ref", "res-env-ref-name"), + new ElementIdentifier("resource-ref", "res-ref-name"), + new ElementIdentifier("ejb-local-ref", "ejb-ref-name") + }; + + private static final String[] _UNIQUE_ELEMENTS = { + "icon", "display-name", "description", "distributable", + "session-config", "welcome-file-list", "login-config" + }; + + private static final String[] _JOINABLE_ELEMENTS = { + "welcome-file-list" + }; + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/WebXML24Descriptor.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/WebXML24Descriptor.java new file mode 100644 index 000000000..518896395 --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/WebXML24Descriptor.java @@ -0,0 +1,111 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml.descriptor; + +import com.liferay.util.xml.ElementIdentifier; + +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.Element; + +/** + * @author Jorge Ferrer + * @author Brian Wing Shun Chan + */ +public class WebXML24Descriptor extends SimpleXMLDescriptor { + + public WebXML24Descriptor() { + _orderedChildren.put( + "jsp-config", new String[] {"taglib", "jsp-property-group"}); + } + + public boolean canHandleType(String doctype, Document root) { + if (doctype.indexOf("web-app") != -1) { + return true; + } + else { + return false; + } + } + + public String[] getRootChildrenOrder() { + return _ROOT_ORDERED_CHILDREN; + } + + public String[] getChildrenOrder(Element parentElement) { + String parentName = parentElement.getQName().getName(); + + if (_orderedChildren.containsKey(parentName)) { + return _orderedChildren.get(parentName); + } + + return new String[0]; + } + + public ElementIdentifier[] getElementsIdentifiedByAttribute() { + return _ELEMENTS_IDENTIFIED_BY_ATTR; + } + + public ElementIdentifier[] getElementsIdentifiedByChild() { + return _ELEMENTS_IDENTIFIED_BY_CHILD; + } + + public String[] getUniqueElements() { + return _UNIQUE_ELEMENTS; + } + + public String[] getJoinableElements() { + return _JOINABLE_ELEMENTS; + } + + private static final String[] _ROOT_ORDERED_CHILDREN = { + "icon", "display-name", "description", "distributable", "context-param", + "filter", "filter-mapping", "listener", "servlet", "servlet-mapping", + "session-config", "mime-mapping", "welcome-file-list", "error-page", + "jsp-config", "resource-env-ref", "resource-ref", "security-constraint", + "login-config", "security-role", "env-entry", "ejb-ref", "ejb-local-ref" + }; + + private static final ElementIdentifier[] _ELEMENTS_IDENTIFIED_BY_ATTR = { + }; + + private static final ElementIdentifier[] _ELEMENTS_IDENTIFIED_BY_CHILD = { + new ElementIdentifier("context-param", "param-name"), + new ElementIdentifier("filter", "filter-name"), + //new ElementIdentifier("filter-mapping", "filter-name"), + new ElementIdentifier("servlet", "servlet-name"), + //new ElementIdentifier("servlet-mapping", "servlet-name"), + new ElementIdentifier("init-param", "param-name"), + new ElementIdentifier("taglib", "taglib-uri"), + new ElementIdentifier("resource-env-ref", "res-env-ref-name"), + new ElementIdentifier("resource-ref", "res-ref-name"), + new ElementIdentifier("ejb-local-ref", "ejb-ref-name") + }; + + private static final String[] _UNIQUE_ELEMENTS = { + "icon", "display-name", "description", "distributable", + "session-config", "welcome-file-list", "jsp-config", "login-config" + }; + + private static final String[] _JOINABLE_ELEMENTS = { + "welcome-file-list", "jsp-config" + }; + + private Map _orderedChildren = + new HashMap(); + +} \ No newline at end of file diff --git a/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/XMLDescriptor.java b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/XMLDescriptor.java new file mode 100644 index 000000000..372b9febb --- /dev/null +++ b/tools-ng/common/webxml-mergetool/src/com/liferay/util/xml/descriptor/XMLDescriptor.java @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +package com.liferay.util.xml.descriptor; + +import org.dom4j.Document; +import org.dom4j.Element; + +/** + * @author Jorge Ferrer + */ +public interface XMLDescriptor { + + public boolean areEqual(Element el1, Element el2); + + public boolean canHandleType(String doctype, Document root); + + public boolean canJoinChildren(Element element); + + public String[] getRootChildrenOrder(); + + public String[] getChildrenOrder(Element parentElement); + +} \ No newline at end of file diff --git a/tools-ng/common/xsl/build-template.xsl b/tools-ng/common/xsl/build-template.xsl index c61140027..dda7a525c 100755 --- a/tools-ng/common/xsl/build-template.xsl +++ b/tools-ng/common/xsl/build-template.xsl @@ -1224,6 +1224,9 @@ + var builder = new WebXMLBuilder(originalWebXML, customWebXML, mergedWebXML); + diff --git a/tools-ng/ecdc/scriptlib/build-ccm.xml b/tools-ng/ecdc/scriptlib/build-ccm.xml index 5025c403e..ad4dfe696 100644 --- a/tools-ng/ecdc/scriptlib/build-ccm.xml +++ b/tools-ng/ecdc/scriptlib/build-ccm.xml @@ -104,8 +104,7 @@ - - +