001: /*
002: Copyright (c) 2007, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.ws.wsdl;
030:
031: import java.io.File;
032: import java.io.IOException;
033: import java.util.HashSet;
034: import java.util.Set;
035:
036: import org.jibx.binding.classes.ClassCache;
037: import org.jibx.binding.generator.ClassSourceWrapper;
038: import org.jibx.binding.generator.IClassSourceLocator;
039: import org.jibx.binding.model.ClassWrapper;
040: import org.jibx.binding.model.IClass;
041: import org.jibx.runtime.JiBXException;
042:
043: import com.thoughtworks.qdox.JavaDocBuilder;
044: import com.thoughtworks.qdox.model.JavaClass;
045:
046: /**
047: * Locator that supports both class file lookup and source file lookup.
048: */
049: public class ClassSourceLocator implements IClassSourceLocator {
050: /** Paths for source lookup. */
051: private final String[] m_sourcePaths;
052:
053: /** Source file parser. */
054: private final JavaDocBuilder m_builder;
055:
056: /** Set of classes parsed. */
057: private final Set m_lookupSet;
058:
059: /**
060: * Constructor.
061: *
062: * @param paths source lookup paths (may be empty, but not
063: * <code>null</code>)
064: */
065: public ClassSourceLocator(String[] paths) {
066: m_sourcePaths = paths;
067: m_builder = new JavaDocBuilder();
068: m_lookupSet = new HashSet();
069: }
070:
071: /**
072: * Get the source code information for a class.
073: *
074: * @param name fully-qualified class name (using '$' as inner class marker)
075: * @return source code information, <code>null</code> if not available
076: */
077: public JavaClass getSourceInfo(String name) {
078: int split = name.lastIndexOf('$');
079: if (split >= 0) {
080: JavaClass outer = getSourceInfo(name.substring(0, split));
081: if (outer == null) {
082: return null;
083: } else {
084: return outer.getNestedClassByName(name
085: .substring(split + 1));
086: }
087: } else if (m_lookupSet.contains(name)) {
088: return m_builder.getClassByName(name);
089: } else {
090: for (int i = 0; i < m_sourcePaths.length; i++) {
091: StringBuffer buff = new StringBuffer();
092: buff.append(m_sourcePaths[i]);
093: int length = buff.length();
094: if (length > 0
095: || buff.charAt(length - 1) != File.separatorChar) {
096: buff.append(File.separatorChar);
097: }
098: buff.append(name.replace('.', File.separatorChar));
099: buff.append(".java");
100: File file = new File(buff.toString());
101: if (file.exists()) {
102: try {
103: m_builder.addSource(file);
104: m_lookupSet.add(name);
105: return m_builder.getClassByName(name);
106: } catch (IOException e) {
107: throw new IllegalStateException(
108: "Unable to access source file "
109: + buff.toString(), e);
110: }
111: }
112: }
113: return null;
114: }
115: }
116:
117: /**
118: * Get the information for a class.
119: *
120: * @param name fully-qualified class name (using '$' as inner class marker)
121: * @return class information
122: */
123: public IClass getClassInfo(String name) {
124: try {
125: return new ClassSourceWrapper(this , ClassCache
126: .getClassFile(name));
127: } catch (JiBXException e) {
128: throw new IllegalStateException("Class not found " + name);
129: }
130: }
131: }
|