previous thread:
http://groups.google.com/group/protobuf/browse_thread/thread/4bf8bca8c88e82ba/dbaa2803984f3934?lnk=gstq=debugString()#dbaa2803984f3934
On May 18, 6:05 pm, Ben Wright compuware...@gmail.com wrote:
There has been some back and forth previously about java language
support for generating a proto file from a Descriptor.
The suggested implementation was to either wrap or port the
DebugString capability from decriptor.cc
I recently ported the DebugString capability to java - it's a bit
rough as it's a by-hand port from the c code, but I also fixed some
bugs in the way the file was generated. For instance, extended types
were not output with correct parenthesis around them and options that
were messages did not have appropriate {} around them - these could
easily be due to differences in the C and Java code though. I'd be
glad to elaborate further for anyone interested, but for now I'm
simply providing the ported code as static functions in a java file.
Notably, if integrated with the java library these functions should
probably not be in a static file, they should be included with their
appropriate descriptor types.
If it is decided that this functionality should be part of the Java
library, I would be willing to do the leg work and get these functions
in better shape and update the appropriate files. I can pass them
along in an issue report for a committer to check in.
Notably, if you're looking for someone to work on the Java library /
outstanding issues at all, I'm looking for something to do in my
mythical free time. I could help with c too, but I'm much stronger in
Java.
PS: This is all based on release 2.4.1
=== DebugString.java ===
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.MethodDescriptor;
import com.google.protobuf.Descriptors.ServiceDescriptor;
import com.google.protobuf.Message;
import com.google.protobuf.TextFormat;
/**
* This class provides .proto file generation from Java Proto
Descriptors.
* brDerived from descriptor.cc in Protobuf 2.4.1
* br
* brbOriginal Copyright Notice from descriptor.cc:/b
* br
* br Protocol Buffers - Google's data interchange format
* br Copyright 2008 Google Inc. All rights reserved.
* brhttp://code.google.com/p/protobuf/
* br
* br Redistribution and use in source and binary forms, with or
without
* br modification, are permitted provided that the following
conditions are
* br met:
* br
* br * Redistributions of source code must retain the above
copyright
* br notice, this list of conditions and the following disclaimer.
* br * Redistributions in binary form must reproduce the above
* br copyright notice, this list of conditions and the following
disclaimer
* br in the documentation and/or other materials provided with the
* br distribution.
* br * Neither the name of Google Inc. nor the names of its
* br contributors may be used to endorse or promote products
derived from
* br this software without specific prior written permission.
* br
* br THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS
* br AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT
* br LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR
* br A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT
* br OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,
* br SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT
* br LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE,
* br DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY
* br THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT
* br (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE
* br OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
* br
* br Author: ken...@google.com (Kenton Varda)
* br Based on original Protocol Buffers design by
* br Sanjay Ghemawat, Jeff Dean, and others.
*
* @author Benjamin Wright (compuware...@gmail.com)
*/
public class DebugString {
private static void SubstituteAndAppend(StringBuilder contents,
String replace, Object... objects ) {
for(int i = 0; i objects.length; i++) {
final String old = $+i;
replace = replace.replace(old,
String.valueOf(objects[i]));
}
contents.append(replace);
}