📄 xmlschemaencoder.java
字号:
// info objects properly, double check
// if (typeName.startsWith(prefix)
// || ((typeName.indexOf(':') == -1)
// && prefix.equals(r.getWFS().getData().getDefaultNameSpace()
// .getPrefix()))) {
// retBuffer.append(typeName + ",");
// }
}
retBuffer.deleteCharAt(retBuffer.length() - 1);
retBuffer.append("\"/>");
return retBuffer;
}
/**
* Internal method to print just the requested types. They should all be
* in the same namespace, that handling should be done before. This will
* not do any namespace handling, just prints up either what's in the
* schema file, or if it's not there then generates the types from their
* FeatureTypes. Also appends the global element so that the types can
* substitute as features.
*
* @param requestedTypes The requested table names.
* @param gs DOCUMENT ME!
*
* @return A string of the types printed.
*
* @throws WFSException DOCUMENT ME!
*
* @task REVISIT: We need a way to make sure the extension bases are
* correct. should likely add a field to the info.xml in the
* featureTypes folder, that optionally references an extension base
* (should it be same namespace? we could also probably just do an
* import on the extension base). This function then would see if
* the typeInfo has an extension base, and would add or import the
* file appropriately, and put the correct substitution group in
* this function.
*/
private String generateSpecifiedTypes(FeatureTypeInfo[] infos) {
//TypeRepository repository = TypeRepository.getInstance();
String tempResponse = new String();
String generatedType = new String();
Set validTypes = new HashSet();
// Loop through requested tables to add element types
for (int i = 0; i < infos.length; i++) {
FeatureTypeInfo ftInfo = (FeatureTypeInfo) infos[i];
if (!validTypes.contains(ftInfo)) {
File schemaFile = ftInfo.getSchemaFile();
try {
//Hack here, schemaFile should not be null, but it is
//when a fType is first created, since we only add the
//schemaFile param to dto on a load. This should be
//fixed, maybe even have the schema file persist, or at
//the very least be present right after creation.
if ((schemaFile != null) && schemaFile.exists() && schemaFile.canRead()) {
generatedType = writeFile(schemaFile);
} else {
FeatureType ft2 = ftInfo.getFeatureType();
String gType2 = generateFromSchema(ft2);
if ((gType2 != null) && (gType2 != "")) {
generatedType = gType2;
}
}
} catch (IOException e) {
generatedType = "";
}
if (!generatedType.equals("")) {
tempResponse = tempResponse + generatedType;
validTypes.add(ftInfo);
}
}
}
// Loop through requested tables again to add elements
// NOT VERY EFFICIENT - PERHAPS THE MYSQL ABSTRACTION CAN FIX THIS;
// STORE IN HASH?
for (Iterator i = validTypes.iterator(); i.hasNext();) {
// Print element representation of table
tempResponse = tempResponse + printElement((FeatureTypeInfo) i.next());
}
tempResponse = tempResponse + "\n\n";
return tempResponse;
}
/**
* Transforms a FeatureTypeInfo into gml, with no headers.
*
* @param schema the schema to transform.
*
* @return DOCUMENT ME!
*
* @task REVISIT: when this class changes to writing directly to out this
* can just take a writer and write directly to it.
*/
private String generateFromSchema(FeatureType schema)
throws IOException {
try {
StringWriter writer = new StringWriter();
FeatureTypeTransformer t = new FeatureTypeTransformer();
t.setIndentation(4);
t.setOmitXMLDeclaration(true);
t.transform(schema, writer);
return writer.getBuffer().toString();
} catch (TransformerException te) {
LOGGER.log( Level.WARNING, "Error generating schema from feature type", te );
throw (IOException) new IOException("problem transforming type").initCause(te);
}
}
/**
* Internal method to print XML element information for table.
*
* @param type The table name.
*
* @return The element part of the response.
*/
private static String printElement(FeatureTypeInfo type) {
return "\n <xs:element name='" + type.getTypeName() + "' type='"
+ type.getNameSpace().getPrefix() + ":" + type.getSchemaName()
+ "' substitutionGroup='gml:_Feature'/>";
}
/**
* Adds a feature type object to the final output buffer
*
* @param inputFileName The name of the feature type.
*
* @return The string representation of the file containing the schema.
*
* @throws WFSException For io problems reading the file.
*/
public String writeFile(File inputFile) throws IOException {
LOGGER.finest("writing file " + inputFile);
String finalOutput = new String();
try {
// File inputFile = new File(inputFileName);
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] fileBuffer = new byte[inputStream.available()];
int bytesRead;
while ((bytesRead = inputStream.read(fileBuffer)) != -1) {
String tempOutput = new String(fileBuffer);
finalOutput = finalOutput + tempOutput;
}
} catch (IOException e) {
//REVISIT: should things fail if there are featureTypes that
//don't have schemas in the right place? Because as it is now
//a describe all will choke if there is one ft with no schema.xml
throw (IOException) new IOException("problem writing featureType information "
+ " from " + inputFile).initCause(e);
}
return finalOutput;
}
/**
* Checks that the collection of featureTypeNames all have the same prefix.
* Used to determine if their schemas are all in the same namespace or if
* imports need to be done.
*
* @param infos list of feature type info objects..
*
* @return true if all the types in the collection have the same prefix.
*
*/
public boolean allSameType(FeatureTypeInfo[] infos) {
boolean sameType = true;
if (infos.length == 0) {
return false;
}
FeatureTypeInfo first = infos[0];
for (int i = 0; i < infos.length; i++) {
FeatureTypeInfo ftInfo = infos[i];
if (!first.getNameSpace().equals(ftInfo.getNameSpace())) {
return false;
}
}
return sameType;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -