Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.lib.meta;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.openjpa.lib.util.ClassUtil;
import org.apache.openjpa.lib.util.Files;
import org.apache.openjpa.lib.util.J2DoPrivHelper;
import org.apache.openjpa.lib.util.Localizer;
import serp.bytecode.lowlevel.ConstantPoolTable;
/**
* Parser used to resolve arguments into java classes.
* Interprets command-line args as either class names, .class files or
* resources, .java files or resources, or metadata files or resources
* conforming to the common format defined by {@link CFMetaDataParser}.
* Transforms the information in these args into {@link Class} instances.
* Note that when parsing .java files, only the main class in the file
* is detected. Other classes defined in the file, such as inner classes,
* are not added to the returned classes list.
*
* @author Abe White
*/
public class ClassArgParser {
private static final int TOKEN_EOF = -1;
private static final int TOKEN_NONE = 0;
private static final int TOKEN_PACKAGE = 1;
private static final int TOKEN_CLASS = 2;
private static final int TOKEN_PACKAGE_NOATTR = 3;
private static final int TOKEN_CLASS_NOATTR = 4;
private static final Localizer _loc = Localizer.forPackage
(ClassArgParser.class);
private ClassLoader _loader = null;
private char[] _packageAttr = "name".toCharArray();
private char[] _classAttr = "name".toCharArray();
private char[][] _beginElements = { { 'p' }, { 'c' } };
private char[][] _endElements = { "ackage".toCharArray(),
"lass".toCharArray() };
/**
* The class loader with which to load parsed classes.
*/
public ClassLoader getClassLoader() {
return _loader;
}
/**
* The class loader with which to load parsed classes.
*/
public void setClassLoader(ClassLoader loader) {
_loader = loader;
}
/**
* Set the the relevant metadata file structure so that metadata files
* containing class names can be parsed. Null attribute names indicate
* that the text content of the element contains the data.
*/
public void setMetaDataStructure(String packageElementName,
String packageAttributeName, String[] classElementNames,
String classAttributeName) {
// calculate how many chars deep we have to go to identify each element
// name as unique. this is extremely inefficient for large N, but
// should never be called for more than a few elements
char[] buf = new char[classElementNames.length + 1];
int charIdx = 0;
for (; true; charIdx++) {
for (int i = 0; i < buf.length; i++) {
if (i == 0) {
if (charIdx == packageElementName.length())
throw new UnsupportedOperationException(_loc.get
("cant-diff-elems").getMessage());
buf[i] = packageElementName.charAt(charIdx);
} else {
if (charIdx == classElementNames[i - 1].length())
throw new UnsupportedOperationException(_loc.get
("cant-diff-elems").getMessage());
buf[i] = classElementNames[i - 1].charAt(charIdx);
}
}
if (charsUnique(buf))
break;
}
_packageAttr = (packageAttributeName == null) ? null
: packageAttributeName.toCharArray();
_classAttr = (classAttributeName == null) ? null
: classAttributeName.toCharArray();
_beginElements = new char[classElementNames.length + 1][];
_endElements = new char[classElementNames.length + 1][];
_beginElements[0] = packageElementName.substring(0, charIdx + 1).
toCharArray();
_endElements[0] = packageElementName.substring(charIdx + 1).
toCharArray();
for (int i = 0; i < classElementNames.length; i++) {
_beginElements[i + 1] = classElementNames[i].
substring(0, charIdx + 1).toCharArray();
_endElements[i + 1] = classElementNames[i].
substring(charIdx + 1).toCharArray();
}
}
/**
* Return true if all characters in given buffer are unique.
*/
private static boolean charsUnique(char[] buf) {
for (int i = buf.length - 1; i >= 0; i--)
for (int j = 0; j < i; j++)
if (buf[j] == buf[i])
return false;
return true;
}
/**
* Return the {@link Class} representation of the class(es) named in the
* given arg.
*
* @param arg a class name, .java file, .class file, or metadata
* file naming the type(s) to act on
*/
public Class>[] parseTypes(String arg) {
String[] names = parseTypeNames(arg);
Class>[] objs = new Class[names.length];
for (int i = 0; i < names.length; i++)
objs[i] = ClassUtil.toClass(names[i], _loader);
return objs;
}
/**
* Return the {@link Class} representation of the class(es) named in the
* given metadatas.
*/
public Class>[] parseTypes(MetaDataIterator itr) {
String[] names = parseTypeNames(itr);
Class>[] objs = new Class[names.length];
for (int i = 0; i < names.length; i++)
objs[i] = ClassUtil.toClass(names[i], _loader);
return objs;
}
/**
* Return a mapping of each metadata resource to an array of its
* contained classes.
*/
public Map